Blog do Eduardo Costa Meu blog pessoal


Cube optimization

I'm doing some tests with OpenGL and cubes. I found some impressive numbers and I want to share them.

If you really know OpenGL, you are aware that glVertex with GL_TRIANGLES stinks. It's the simplest way, but is also the worst way. Using Vertex Buffer ObjectsglDrawElements and GL_TRIANGLE_STRIP is always a better idea.

Let's go to the numbers. In a cube, we have 6 faces, right? Each face has two triangles with three vertices each. This is 36 (6x3x2) points. For each point, we have 3 coordinates (X, Y, Z), plus a extra one for shaders (W) , and two more for texturing (U, V). Using floats, we have 6 of them with 4 bytes, this is 24 bytes per "point". This means the cube has 864 bytes (36x24).

How to improve? First, we will change the floats with shorts for XYZW. This will lead to 16 bytes (4 shorts of 2 bytes plus 2 floats of 4 bytes). This reduces the cube to 574 bytes (36x24). Better, but not good, yet.

With triangle strips, we reduce the vertice count. The texture I need for the cube allows me to make a "strip" for the sides, one for the top and one for the bottom. I managed to describe my cube using only 22 points. Now, my cube has 352 bytes (16x22). Way better, but we have the final trick!

VBOs: these 22 points have only 15 unique occurrences. This means we have 240 bytes (16x15), plus 22 shorts for indexes. Total: 284 bytes (44+240)!

This means we have a 68% of memory optimization!