12

I see different number of vertices in different online sites to represent the same cube in OpenGL ES. For Example this is one:

float vertices[] = { -width, -height, -depth, // 0
                              width, -height, -depth, // 1
                              width,  height, -depth, // 2
                             -width,  height, -depth, // 3
                             -width, -height,  depth, // 4
                              width, -height,  depth, // 5
                              width,  height,  depth, // 6
                             -width,  height,  depth // 7
        };  
  short indices[] = { 0, 2, 1,
                0, 3, 2,

                1,2,6,
                6,5,1,

                4,5,6,
                6,7,4,

                2,3,6,
                6,3,7,

                0,7,3,
                0,4,7,

                0,1,5,
                0,5,4
                         };

And here is another one:

float vertices[] = {
            //Vertices according to faces
                -1.0f, -1.0f, 1.0f, //Vertex 0
                1.0f, -1.0f, 1.0f,  //v1
                -1.0f, 1.0f, 1.0f,  //v2
                1.0f, 1.0f, 1.0f,   //v3

                1.0f, -1.0f, 1.0f,  //...
                1.0f, -1.0f, -1.0f,         
                1.0f, 1.0f, 1.0f,
                1.0f, 1.0f, -1.0f,

                1.0f, -1.0f, -1.0f,
                -1.0f, -1.0f, -1.0f,            
                1.0f, 1.0f, -1.0f,
                -1.0f, 1.0f, -1.0f,

                -1.0f, -1.0f, -1.0f,
                -1.0f, -1.0f, 1.0f,         
                -1.0f, 1.0f, -1.0f,
                -1.0f, 1.0f, 1.0f,

                -1.0f, -1.0f, -1.0f,
                1.0f, -1.0f, -1.0f,         
                -1.0f, -1.0f, 1.0f,
                1.0f, -1.0f, 1.0f,

                -1.0f, 1.0f, 1.0f,
                1.0f, 1.0f, 1.0f,           
                -1.0f, 1.0f, -1.0f,
                1.0f, 1.0f, -1.0f,
                                    };

Which representation is highest performance in OpenGL ES ( Note that it is OpenGL ES and not OpenGL)? Which is the best representation when I want to specify normals for lighting. In one my other quations on SO, I get the impression that I need to specify 24 vertices for the cube, another one says I need 36 vertices, yet another one says I need a different number getting contradictory answers to the same question. Anyway I want to know correct single answer that is technically correct.

I know there is a concept of Triangle Strip or Triangle fans etc. for drawing but in my case I am using:

gl.glDrawElements(GL10.GL_TRIANGLES, mNumOfIndices,
                GL10.GL_UNSIGNED_SHORT, mIndicesBuffer);

So this should clearly clarify what I am asking.

1 Answer 1

27

If using indices, you only need to provide 8 distinct vertices to define a cube in OpenGL ES. For example:

static const GLfloat cubeVertices[] = {
    -1.0, -1.0,  1.0,
    1.0, -1.0,  1.0,
    -1.0,  1.0,  1.0,
    1.0,  1.0,  1.0,
    -1.0, -1.0, -1.0,
    1.0, -1.0, -1.0,
    -1.0,  1.0, -1.0,
    1.0,  1.0, -1.0,
};

static const GLushort cubeIndices[] = {
    0, 1, 2, 3, 7, 1, 5, 4, 7, 6, 2, 4, 0, 1
};

Using a triangle strip for the geometry here, I only needed to provide 14 indices to specify the cube. You could remove the above indices and provide vertices in that order, if you'd like.

genpfault describes the case where you provide pure triangles to OpenGL ES, in which case you'd need 36 vertices. As you can see, many of these vertices are redundant, so strips or indices can reduce the geometry you need to send.

Eric brings up a good point in his comment, that if you need to provide a texture or color to each face (which appears to be your goal) you'll want to use 24 vertices or indices. You need this so that you can address each face separately.

What's highest performance for your particular application will depend on the hardware you're running this on. For example, on the PowerVR chips inside iOS devices, Apple has this to say:

For best performance, your models should be submitted as a single unindexed triangle strip using glDrawArrays with as few duplicated vertices as possible. If your models require many vertices to be duplicated (because many vertices are shared by triangles that do not appear sequentially in the triangle strip or because your application merged many smaller triangle strips), you may obtain better performance using a separate index buffer and calling glDrawElements instead. There is a trade off: an unindexed triangle strip must periodically duplicate entire vertices, while an indexed triangle list requires additional memory for the indices and adds overhead to look up vertices. For best results, test your models using both indexed and unindexed triangle strips, and use the one that performs the fastest.

3
  • 1
    Additional vertexes may be required if the cube varies from face to face. For example a cube with different colors on each face could require 24 vertexes, and 36 indices if drawn with glTriangles. If not all sides of the cube are visible, the drawing could be optimized by not drawing the faces that are not visible, reducing the index count.
    – Mr. Berna
    Feb 17, 2011 at 19:13
  • @Mr. Berna - Good point. I've integrated this in my answer, because it does sound like he's looking for different colors for the faces of his cube.
    – Brad Larson
    Feb 17, 2011 at 19:30
  • 3
    @Brad Larson - Sorry for posting in this old thread, but I think the indices should be {0, 1, 2, 3, 7, 1, 6, 4, 7, 5, 2, 4, 0, 1} to form a correct cube. at lest yours did not work for me. =)
    – Dafen
    May 12, 2014 at 23:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.