Course list http://www.c-jump.com/bcc/
This handout provides overview of a possible OpenGL client application structure using GLEW and FreeGLUT. The overview does not cover shader code.
Each function is documented in a pseudocode format with a list of "actions" on the left-hand side, and a list of input/output parameters on the right-hand side.
The actions represent both OpenGL API calls and application functions, including member functions of C++ classes. For example,
// Function named action1 takes no paramenters and returns no values
action1
// Function named action2 takes some input paramenters:
action2 <- param1, param2, param3
// Function named action3 takes no input paramenters but returns a value
action3 -> result
// Function named action4 takes input paramenters and returns a value
action4 <- param1, param2, param3
-> result
// Function named action5 is a member function and requires an object
object.action5 <- param1, param2, param3
-> result
// Objects are created like this:
constructor <- param1, param2, param3
-> object
// Values can be assigned directly
object.member = value
// Repetitions are documewnted as
for each model {
action1 <- ...
action2 <- ...
...
}//for
/*initialize glut...*/
glewInit() -> GLEW_OK
/*compile shaders...*/
/*configure light parameters...*/
for each model {
constructor -> C++ Model object
object.load_OBJ_file <- OBJ filename
object.upload_bitmap_texture <- BMP file path
object.upload_2_server
}//for each model
constructor <- projection parameters
-> C++ Camera object
glutMainLoop
// A model is constructed as follows:
glGenVertexArrays -> vao
glGenBuffers -> vbo // for GL_ARRAY_BUFFER
glGenBuffers -> index_vbo // for GL_ELEMENT_ARRAY_BUFFER
glGetUniformLocation <- shader.program_ID
-> umM // model matrix ID in shader
glGetUniformLocation <- shader.program_ID
-> umR // rotation matrix ID in shader to rotate normals
// Description: load object geometry from Wavefront OBJ file
// invoked once per model
for vertices[], normals[], UVs[], and colors[] {
glGetAttribLocation <- shader.program_ID
-> vPosition/vNormal/vTexCoord/vColor
}//for
for indices[] {
glBindVertexArray <- vao
glBindBuffer <- GL_ELEMENT_ARRAY_BUFFER, index_vbo
glBufferData <- GL_ELEMENT_ARRAY_BUFFER, size, indices[], GL_STATIC_DRAW
}//for indices[]
// Description: load the bitmap from file and upload it to GPU
// Invoked once per model
// PRE-CONDITION: UVs[] already loaded by Model::load_OBJ_file()
load_bmp_file <- filename
-> width, height, size, bitmap_data
glGenTextures <- 1
-> tex_buffer_ID
// Load the bitmap onto the GPU
glTexImage2D <- GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, bitmap_data
glTexParameterf <- GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT
glTexParameterf <- GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT
glTexParameterf <- GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR
glTexParameterf <- GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR
glGenerateMipmap <- tex_buffer_ID
// Get the ID of the variable "texture" from the fragment shader
glGetUniformLocation <- shader.program_ID
-> texture
// Description: upload geometry data to OpenGL
// Invoked once per model
glBindVertexArray <- vao
glBindBuffer <- GL_ARRAY_BUFFER, vbo
glBufferData <- GL_ARRAY_BUFFER, sizeBuffer, NULL, GL_STATIC_DRAW
for vertices[], normals[], UVs[], and colors[] {
glBufferSubData <- GL_ARRAY_BUFFER, offset, total_vertices, vertices
glVertexAttribPointer <- ...
}//for
glClear <- GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
glPolygonMode <- GL_FRONT_AND_BACK, GL_FILL
for each model {
model.matrix = translate * rotate * scale
camera.set_uniform_view_perspective <- model.shader.program_ID
light->set_shader_light_position <- model.shader.program_ID
object.render
}//for
glutSwapBuffers
// Description: draw the model
// Invoked for each frame
// PRE_CONDITION: geometry data must be already loaded by upload_2_server()
// PREPARE TO RENDER -- attach/enable stuff
glBindVertexArray <- vao
glUseProgram <- shader.program_ID
for vertices[], normals[], and colors[]
glEnableVertexAttribArray <- vPosition/vNormal/vColor
for UVs[]
glEnableVertexAttribArray <- vTexCoord // for UVs texture coordinates array
glEnable <- GL_TEXTURE_2D
glBindTexture <- GL_TEXTURE_2D, tex_buffer_ID
glActiveTexture <- GL_TEXTURE0
glUniform1i <- texture
for model matrix
glUniformMatrix4fv <- umM, 1, GL_FALSE, &mM[0][0]
for rotation matrix to rotate normals
glUniformMatrix4fv <- umR, 1, GL_FALSE, &mR[0][0]
/* load light uniforms into shader */
// RENDER -- everything is ready
if indices[]
glDrawElements <- GL_TRIANGLES, total_indices, GL_UNSIGNED_INT, NULL
else
glDrawArrays <- GL_TRIANGLES, 0, total_vertices
// UNPREPARE -- detach/disable stuff
for vertices[], normals[], and colors[]
glDisableVertexAttribArray <- vPosition/vNormal/vColor
for UVs[] {
glDisableVertexAttribArray <- vTexCoord
glDisable <- GL_TEXTURE_2D
}//for
|
|