Calculate Index-buffer size of a cube

2.7k views Asked by At

I've been following a Computer Graphics course for a few weeks with an exam coming up, however I've gotten stuck on the topic of index buffers.

I know that a vertex buffer stores all vertices of a mesh. Now suppose we wish to make a cube. This means there are 8 vertices required to construct it. Each vertex is composed of 3 coordinates (x,y,z), thus the vertex buffer will be taking up 3 x 8 = 24 units of space.

I've read that index-buffers store the indices of vertices. So the indices of the vertices of our cube would be 1 to 8. This means 1 triangle takes up 3 units of space.

A question bothering me is: How many units of space would our index-buffer take?

I reason that a cube has 6 faces. Each face is composed of 2 triangles. This means there are 12 triangles, thus the index-buffer would take 3 x 12 = 36 units of space. (I used this site to reach this conclusion: https://msdn.microsoft.com/en-us/library/windows/desktop/bb147325(v=vs.85).aspx). However, my answer sheet claims it is 32.

Normally I wouldn't bother to post on StackOverflow and just ask my teacher, however he is currently on holiday and won't be back until after the exam. Thus, can anyone explain to me how the size of the index-buffer is regulated? (Assume it is the index-buffer used in OpenGL, if that matters).

1

There are 1 answers

1
GraphicsMuncher On BEST ANSWER
  1. A cube has 6 faces
  2. Each face is made up of 2 triangles
  3. Each triangle is made up of 3 vertices
  4. Thus, 6 * 2 * 3 = 36 vertices. Each vertex get indexed once here, so without any tricks, your index buffer has 36 elements in it.

However, this method applies if you are using GL_TRIANGLES, i.e. where each triangle is specified independently of the next.


You can use GL_TRIANGLE_STRIP and specify each face with 4 indices using 6 draw calls, but this gets you a size of 4 * 6 = 24. This is a wasteful use of draw calls, and 24 != 32. Plus, you'll have to use some index-buffer-offset draw call, which is just silly for drawing a cube.


You can use GL_TRIANGLE_STRIP and the degenerate triangle strip method to reduce the number of indices you need even further.

  1. Wrap 4 face of the cube around one axis. Each edge has 2 vertices, but the first edge must be repeated in order to cover the last face. That's 2 * 5 = 10.
  2. Index the uncovered face on which the last index from step 1 lies, repeating the starting index (causing a degenerate triangle). 4
  3. Add a degenerate index on the last index, and one again at the beginning of the remaining cube face. 2
  4. Index the last cube face. 4
  5. In total, we have 10 + 4 + 2 + 4 = 20 indices.

But this is not 32. Bummer.


If you really wanted to pack that index buffer down, you could use 2 triangle fans (via GL_TRIANGLE_FAN) and some fancy draw calls to get it down to 16 indices. Each fan starts on an opposing corner and wraps around its adjacent faces.

Alas, this does not equal 32, either.


So, where does this eldritch 32 come from?

  • It could be the number of bits in an int, which is probably what you're using as the index type in your index buffer. This is a size of something...indexy...that's 32...
  • It could be that the answer thought using GL_TRIANGLES for 4 sides and GL_TRIANGLE_STRIP for 2 sides was an intelligent thing to do.
  • It could almost certainly be that either you've misread the question, read the answer to a different problem, or your answer sheet is wrong.

32 just does not make sense.