Download presentation
Presentation is loading. Please wait.
Published byLeonard Lloyd Modified over 8 years ago
1
A study of efficiency INDEX BUFFERS JEFF CHASTINE 1
2
This is what we made previously JEFF CHASTINE 2
3
3 This is what we want to make
4
JEFF CHASTINE 4 What’s the big deal?
5
JEFF CHASTINE 5 There are 3 triangles
6
JEFF CHASTINE 6 There are 3 triangles T1 Vertex count: 3
7
JEFF CHASTINE 7 There are 3 triangles T2 Vertex count: 3+3
8
JEFF CHASTINE 8 There are 3 triangles T3 Vertex count: 3+3+3 = 9
9
JEFF CHASTINE 9 Let’s count again… “Real” vertex count = 6 1 23 456
10
JEFF CHASTINE 10 These are used more than once!
11
JEFF CHASTINE 11 And, practically speaking… T4 Vertex count: 3+3+3+3 = 12 vs. 6 “real” vertices
12
WHY THIS IS A BIG DEAL JEFF CHASTINE 12 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!
13
JEFF CHASTINE 13 Enter the Index Buffer 1 23 456
14
JEFF CHASTINE 14 Enter the Index Buffer 0 12 345
15
JEFF CHASTINE 15 Specify Triangle 1 0 12 345 {0, 1, 2} T1
16
JEFF CHASTINE 16 Specify Triangle 2 0 12 345 {0, 1, 2, 1, 3, 4} T2
17
JEFF CHASTINE 17 Specify Triangle 3 0 12 345 {0, 1, 2, 1, 3, 4, 2, 4, 5} T3
18
JEFF CHASTINE 18 And, practically speaking… 0 12 345 {0, 1, 2, 1, 3, 4, 2, 4, 5, 1, 4, 2} T4 Index buffer!
19
JEFF CHASTINE 19 Index Buffer size 0 12 345 {0, 1, 2, 1, 3, 4, 2, 4, 5, 1, 4, 2} = 48 bytes T4
20
COMPARISON JEFF CHASTINE 20 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!
21
CODE JEFF CHASTINE 21 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);
22
CODE JEFF CHASTINE 22 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);
23
JEFF CHASTINE 23 This is what we wanted to make
24
JEFF CHASTINE 24 This is what we made
25
JEFF CHASTINE 25 Why? Only 1 color per vertex
26
END WITH A NICE RELIC JEFF CHASTINE 26
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.