IndexBuffers.pptx

advertisement
INDEX BUFFERS
A study of efficiency
JEFF CHASTINE
1
This is what we made previously
JEFF CHASTINE
2
This is what we want to make
JEFF CHASTINE
3
What’s the big deal?
JEFF CHASTINE
4
There are 3 triangles
JEFF CHASTINE
5
There are 3 triangles
T1
Vertex count: 3
JEFF CHASTINE
6
There are 3 triangles
T2
Vertex count: 3+3
JEFF CHASTINE
7
There are 3 triangles
T3
Vertex count: 3+3+3 = 9
JEFF CHASTINE
8
Let’s count again…
1
2
4
3
5
6
“Real” vertex count = 6
JEFF CHASTINE
9
These are used more than once!
JEFF CHASTINE
10
And, practically speaking…
T4
Vertex count: 3+3+3+3 = 12
vs.
6 “real” vertices
JEFF CHASTINE
11
WHY THIS IS A BIG DEAL
• Inefficient
• Takes up more memory
• Each vertex can be 48 bytes
• Position: 3*4 = 12 bytes
• Color: 4*4 = 16 bytes
• Normal: 3*4 = 12 bytes
• Texture coordinate: 2*4 = 8 bytes
• Takes up more processing!
• Must translate/rotate/scale/skew vertices
• Redundant computations!
JEFF CHASTINE
12
Enter the Index Buffer
1
2
4
JEFF CHASTINE
3
5
6
13
Enter the Index Buffer
0
1
3
JEFF CHASTINE
2
4
5
14
Specify Triangle 1
0
T1
1
3
2
4
5
{0, 1, 2}
JEFF CHASTINE
15
Specify Triangle 2
0
1
2
T2
3
4
5
{0, 1, 2, 1, 3, 4}
JEFF CHASTINE
16
Specify Triangle 3
0
1
2
T3
3
4
5
{0, 1, 2, 1, 3, 4, 2, 4, 5}
JEFF CHASTINE
17
And, practically speaking…
0
1
2
T4
3
Index buffer!
JEFF CHASTINE
4
5
{0, 1, 2, 1, 3, 4, 2, 4, 5, 1, 4, 2}
18
Index Buffer size
0
1
2
T4
3
4
5
{0, 1, 2, 1, 3, 4, 2, 4, 5, 1, 4, 2} = 48 bytes
JEFF CHASTINE
19
COMPARISON
•
Without Index Buffers
• Each vertex takes up 48 bytes
• Each triangle has 3 vertices
• 4 triangles have 12 vertices
• Total size = 4 triangles * 3 vertices * 48 bytes each = 576 bytes
•
With Index Buffers
• Each vertex takes up 48 bytes
• Have only 6 of them
• Index buffer is 48 bytes
• Total size = 6 vertices * 48 bytes each + 48 bytes = 336 bytes
•
In this case, ½ the number of vertices to process!
JEFF CHASTINE
20
CODE
•
Before this, still use the Vertex Array Object (VAO)
•
Create a specialized kind of buffer!
GLuint index_vbo;
// Ask the driver to create a buffer for us
glGenBuffers(1, &index_vbo);
// Tell the driver that it's an index buffer. Instead of GL_ARRAY_BUFFER
glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, index_vbo);
// This time, just go ahead and pass the data off to the buffer because
// we're not packing multiple data sets into the buffer - only indices
glBufferData (GL_ELEMENT_ARRAY_BUFFER, size, indices, GL_STATIC_DRAW);
JEFF CHASTINE
21
CODE
•
To render the triangles:
• Don’t use glDrawArrays (GL_TRIANGLES, 0, NUM_VERTICES);
• This is the non-index-buffer way!
// Use both the vertex buffer and index buffer!
glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_INT, NULL);
JEFF CHASTINE
22
This is what we wanted to make
JEFF CHASTINE
23
This is what we made
JEFF CHASTINE
24
Why?
Only 1 color per vertex
JEFF CHASTINE
25
END WITH A NICE RELIC
JEFF CHASTINE
26
Download