lab1-moglclassShader 코드 실행하고 실행 및 코드분석 리포트 (init() & draw() 함수 중심으로, 장수 제한 없음) (Due by 9/23 -> 9/30까지 연장)
3.circle
lab1-moglclassShader 코드 실행하고 실행 및 코드분석 리포트 (init() & draw() 함수 중심으로, 장수 제한 없음) (Due by 9/23 -> 9/30까지 연장)
3.circle
lecture4
lecture3
lecture2
void display( void )
{
glClear( GL_COLOR_BUFFER_BIT ); // clear the window
// circle
glBindVertexArray(vao);
if (g_colormode == 0)
glVertexAttrib3f((GLuint)1, 1, 0, 0); // set constant color attribute (red)
else if (g_colormode == 1)
glVertexAttrib3f((GLuint)1, 0, 1, 0); // set constant color attribute (green)
else if (g_colormode == 2)
glVertexAttrib3f((GLuint)1, 0, 0, 1); // set constant color attribute (blue)
// set drawmode
if (g_wiremode)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // draw wireframe
else
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // draw filled
// primitive type
if (g_primitivemode == GL_POINTS) // 0
{
glPointSize(MOGL_THICKNESS);
glDrawArrays(GL_POINTS, 0, MOGL_STEP); // draw points
glPointSize(1);
}
else if (g_primitivemode == GL_LINES) // 1
{
glLineWidth(MOGL_THICKNESS);
glDrawArrays(GL_LINES, 0, MOGL_STEP); // draw lines
glLineWidth(1);
}
else if (g_primitivemode == GL_LINE_LOOP) // 2
{
glLineWidth(MOGL_THICKNESS);
glDrawArrays(GL_LINE_LOOP, 0, MOGL_STEP); // draw line loop
glLineWidth(1);
}
else if (g_primitivemode == GL_LINE_STRIP) // 3
{
glLineWidth(MOGL_THICKNESS);
glDrawArrays(GL_LINE_STRIP, 0, MOGL_STEP); // draw line strip
glLineWidth(1);
}
else if (g_primitivemode == GL_TRIANGLES) // 4
{
glDrawArrays(GL_TRIANGLES, 0, MOGL_STEP); // draw triangles
}
else if (g_primitivemode == GL_TRIANGLE_STRIP) // 5
{
glDrawArrays(GL_TRIANGLE_STRIP, 0, MOGL_STEP); // draw triangle strip
}
else if (g_primitivemode == GL_TRIANGLE_FAN) // 6
{
glDrawArrays(GL_TRIANGLE_FAN, 0, MOGL_STEP); // draw triangle fan
}
else if (g_primitivemode == GL_POLYGON) // 9
{
glDrawArrays(GL_POLYGON, 0, MOGL_STEP); // draw polygon
}
glBindVertexArray(0);
glDisableVertexAttribArray(0);
glFlush();
}
glVertexAttribPointer — define an array of generic vertex attribute data
https://www.opengl.org/sdk/docs/man/html/glVertexAttribPointer.xhtml
void glVertexAttribPointer( |
GLuint index, |
GLint size, | |
GLenum type, | |
GLboolean normalized, | |
GLsizei stride, | |
const GLvoid * pointer) ; |
index
size
GL_BGRA
is accepted by glVertexAttribPointer
. The initial value is 4.
type
GL_BYTE
, GL_UNSIGNED_BYTE
, GL_SHORT
, GL_UNSIGNED_SHORT
, GL_INT
, and GL_UNSIGNED_INT
are accepted by glVertexAttribPointer
and glVertexAttribIPointer
. Additionally GL_HALF_FLOAT
, GL_FLOAT
, GL_DOUBLE
, GL_FIXED
, GL_INT_2_10_10_10_REV
, GL_UNSIGNED_INT_2_10_10_10_REV
and GL_UNSIGNED_INT_10F_11F_11F_REV
are accepted by glVertexAttribPointer
. GL_DOUBLE
is also accepted by glVertexAttribLPointer
and is the only token accepted by the type
parameter for that function. The initial value is GL_FLOAT
.
normalized
glVertexAttribPointer
, specifies whether fixed-point data values should be normalized (GL_TRUE
) or converted directly as fixed-point values (GL_FALSE
) when they are accessed.
stride
stride
is 0, the generic vertex attributes are understood to be tightly packed in the array. The initial value is 0.
pointer
GL_ARRAY_BUFFER
target. The initial value is 0.
1. include & lib 를 받아서 실습 디렉토리 (예: lab) 안에 저장한다. freeglut-MSVC-3.0.0-2.mp.zip & glew-2.0.0-win32.zip & glm-0.9.7.6.zip
include lib bin (freeglut.dll, glew32.lib, glew32ms.dll)
2. 새로운 OpenGL 프로젝트 (예: lab0, etc)를 실습 디렉토리 아래에서 생성한다.
3. source code 를 넣어준다.
lab1-moglclassShader (moglclass.h, shaders.cpp, shaders.h, vertexBufferObject.cpp, vertexBufferObject.h) colorTriangle.cpp, simple2.vs, simple2.fs
4. 각 OpenGL 프로젝트마다 include 와 lib 디렉토리 설정을 하고 lib 를 넣어준 후 빌드 (즉, compile & link) 한다.
5. 실행한다.
6. 다른 코드를 실행해 본다.
MY FIRST OPENGL SHADER
lab0-ShaderTriangle-uniform (fragment shader에 color값을 uniform variable을 사용하여 보내기)
////////////////////////////////////////////////////////////////////////////////////////////////////
simple.vs
////////////////////////////////////////////////////////////////////////////////////////////////////
#version 150
in vec4 vPosition;
void
main()
{
gl_Position = vPosition;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
simple.fs
////////////////////////////////////////////////////////////////////////////////////////////////////
#version 150
out vec4 fColor;
void
main()
{
fColor = vec4( 1.0, 0.0, 0.0, 1.0 );
}
////////////////////////////////////////////////////////////////////////////////////////////////////
triangle.cpp
////////////////////////////////////////////////////////////////////////////////////////////////////
#include <cmath>
#include <iostream>
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <GL/freeglut_ext.h>
const float vertexPositions[] = {
-0.75f, -0.75f, 0.0f, 1.0f,
0.75f, -0.75f, 0.0f, 1.0f,
0.75f, 0.75f, 0.0f, 1.0f,
};
GLuint program;
GLuint vao;
GLuint buffer;
// Create a NULL-terminated string by reading the provided file
static char* readShaderSource(const char* shaderFile)
{
FILE* fp = fopen(shaderFile, “r”);
if ( fp == NULL ) { return NULL; }
fseek(fp, 0L, SEEK_END);
long size = ftell(fp);
fseek(fp, 0L, SEEK_SET);
char* buf = new char[size + 1];
fread(buf, 1, size, fp);
buf[size] = ”;
fclose(fp);
return buf;
}
// Create a GLSL program object from vertex and fragment shader files
GLuint InitShader(const char* vShaderFile, const char* fShaderFile)
{
struct Shader {
const char* filename;
GLenum type;
GLchar* source;
} shaders[2] = {
{ vShaderFile, GL_VERTEX_SHADER, NULL },
{ fShaderFile, GL_FRAGMENT_SHADER, NULL }
};
GLuint program = glCreateProgram();
for ( int i = 0; i < 2; ++i ) {
Shader& s = shaders[i];
s.source = readShaderSource( s.filename );
if ( shaders[i].source == NULL ) {
std::cerr << “Failed to read ” << s.filename << std::endl;
exit( EXIT_FAILURE );
}
GLuint shader = glCreateShader( s.type );
glShaderSource( shader, 1, (const GLchar**) &s.source, NULL );
glCompileShader( shader );
GLint compiled;
glGetShaderiv( shader, GL_COMPILE_STATUS, &compiled );
if ( !compiled ) {
std::cerr << s.filename << ” failed to compile:” << std::endl;
GLint logSize;
glGetShaderiv( shader, GL_INFO_LOG_LENGTH, &logSize );
char* logMsg = new char[logSize];
glGetShaderInfoLog( shader, logSize, NULL, logMsg );
std::cerr << logMsg << std::endl;
delete [] logMsg;
exit( EXIT_FAILURE );
}
delete [] s.source;
glAttachShader( program, shader );
}
/* link and error check */
glLinkProgram(program);
GLint linked;
glGetProgramiv( program, GL_LINK_STATUS, &linked );
if ( !linked ) {
std::cerr << “Shader program failed to link” << std::endl;
GLint logSize;
glGetProgramiv( program, GL_INFO_LOG_LENGTH, &logSize);
char* logMsg = new char[logSize];
glGetProgramInfoLog( program, logSize, NULL, logMsg );
std::cerr << logMsg << std::endl;
delete [] logMsg;
exit( EXIT_FAILURE );
}
/* use program object */
glUseProgram(program);
return program;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void init( void )
{
// Load shaders and use the resulting shader program
program = InitShader( “simple.vs”, “simple.fs” );
glUseProgram(program);
// VAO
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
// VBO
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexPositions), vertexPositions, GL_STATIC_DRAW);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
glClearColor( 1.0, 1.0, 1.0, 1.0 ); // white background
}
void display( void )
{
glClear( GL_COLOR_BUFFER_BIT ); // clear the window
glDrawArrays(GL_TRIANGLES, 0, 3); // draw triangle
glFlush();
}
void keyboard( unsigned char key, int x, int y )
{
switch ( key ) {
case 27:
exit( EXIT_SUCCESS );
break;
}
}
int main( int argc, char **argv )
{
glutInit(&argc, argv);
glutInitDisplayMode( GLUT_RGBA );
glutInitWindowSize( 512, 512 );
glutCreateWindow(argv[0]);
glewInit();
init();
glutDisplayFunc( display );
glutKeyboardFunc( keyboard );
glutMainLoop();
return 0;
}