Course list http://www.c-jump.com/bcc/
In the previous lab we finished drawing of the three triangles:
To make this drawing, the program uploaded to the GPU nine vertices and nine colors (3 vertices and 3 colors for each triangle):
GLfloat vertices[] = {-0.5f, -0.5f, 0.0f, 0.0f, -0.5f, 0.0f, -0.25f, 0.0f, 0.0f, -0.25f, 0.0f, 0.0f, 0.25f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f, 0.25f, 0.0f, 0.0f }; GLfloat colors[] = {1.0f, 0.75f, 0.0f, 1.0f, 1.0f, 0.63f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.75f, 0.0f, 1.0f, 1.0f, 0.63f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.75f, 0.0f, 1.0f, 1.0f, 0.63f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f }
At the same time, some vertices can be viewed as shared between triangles:
Fig. 1 Vertex indices
We will refer to the numbers on this picture as vertex indices. Using indices, each triangle could be made up as follows:
Top: 2, 1, 4 Middle: 1, 3, 4 Lower left: 0, 3, 1 Lower right: 3, 5, 4
This time, instead of the nine original vertices we use six. In cases when there are thousands of triangles, indices can save a lot of memory and require less processing power from the GPU.
Similar to a vertex buffer bound to GL_ARRAY_BUFFER target, OpenGL also supports index array buffers to feed the vertex attributes into the vertex shader.
An index array buffer must be bound to GL_ELEMENT_ARRAY_BUFFER target. For example, the indices from our example could be uploaded to OpenGL as follows:
GLuint indices[] = { 2, 1, 4, 1, 3, 4, 0, 3, 1, 3, 5, 4 }; GLuint vao; // Vertex array object GLuint index_vbo; // Index buffer object glGenVertexArrays( 1, &vao ); glGenBuffers( 1, &index_vbo ); glBindVertexArray( vao ); glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, index_vbo ); glBufferData( GL_ELEMENT_ARRAY_BUFFER, total_indices, indices, GL_STATIC_DRAW );
The triangles could later be drawn by the command
glDrawElements( GL_TRIANGLES, total_indices, GL_UNSIGNED_INT, NULL );
Recall that original rendering call without the indices was
glDrawArrays( GL_TRIANGLES, 0, total_vertices );
Download c262_lab05.zip and unzip the file under the labs directory. You should get the directory structure
C:\bcc\GL262Labs\labs\c262_lab05
Go ahead and open Visual Studio project
C:\bcc\GL262Labs\labs\c262_lab05\c262_lab05.sln
Compile it:
Build -> Build Solution
(or simply press F7.)
The idea of this lab is to use index array buffers in our lab projects
Take a look at the header file of the Model C++ class. It has a number of updates:
void set_index_buffer(GLuint* indices, int size); GLuint* indices; // Index buffer GLuint index_vbo; // Index buffer object
Open the implementation of the Model C++ class. Look at all changes marked by UPDATE! in the comments. This is a good time to discuss the code.
Open c262_lab05.cpp. Find the place where vertices[] array is created.
Make the following changes:
Write a comment with the index number (exactly as shown on Fig. 1) next to each vertex. Realize that some of the vertices are redundant.
Remove redundant vertices from the vertices[] array.
Remove redundant entries from the colors[] array. By now there should be only 6 vertices and 6 colors (instead of 9).
Update the indices[] array for triangles you'd like to display. If you want to keep the same three triangles as we had in the previous lab, there should be 9 integers in your indices[] array. Remember to keep the counter-clockwise winding order of your triangles.
Call Model::set_index_buffer() function to upload the index array to OpenGL
Compile and run the project.
Besides the fact that your program may be working correctly, it is utterly important to understand how it works. Now is the time to brainstorm the issues and ask questions. Everyone is encouraged to participate in the discussion!
There are no files to submit.
Demonstrate a working c262_lab05 program to your instructor.