Question

I've seen a lot of ways to declare the vertices of a cube in space. Some codes declare 36 vertices, some other just eight, like that:

private float vertices[] = {
                                -1.0f, -1.0f, -1.0f, //p1 lower left far plane (0)
                                1.0f, -1.0f, -1.0f,  //p2 lower right far plane (1)
                                1.0f,  1.0f, -1.0f,  //p3 top right far plane (2)
                                -1.0f, 1.0f, -1.0f,  //p4 top left far plane (3)
                                -1.0f, -1.0f,  1.0f, //p5 lower left near plane (4)
                                1.0f, -1.0f,  1.0f,  //p6 lower right near plane (5)
                                1.0f,  1.0f,  1.0f,  //p7 top right near plane (6)
                                -1.0f,  1.0f,  1.0f  //p8 top left near plane (7)
                                };

It looks like you draw just two faces and then fill the holes. That seems preferable so I'm saving precious bytes. Is that correct? What order shall I specify in the indices array?

---EDIT Ok, I've got it. This should be the order to declare the vertices, right? But my cube has wrong triangles on some faces.

private short order[] = {
                              0, 4, 5, 0, 5, 1, //bottom face
                              1, 5, 6, 1, 6, 2, //right face
                              2, 6, 7, 2, 7, 3, // top face
                              3, 7, 4, 3, 4, 0, // left face
                              4, 7, 6, 4, 6, 5, // front face
                              3, 0, 1, 3, 1, 2 // back face
                              };
Was it helpful?

Solution

Yes that's right. once you define the vertices for the front and back (near and far) faces you have given all the information needed to construct the cube. Anything else is redundant and waste memory.

The only thing you need to be careful about is this : When you actually use the vertices to construct the cube order matters.

By default opengl expects for the vertices to be provided counter clockwise. I believe you can override that behavior, but I have spent many hours trying to figure out what I thought were coding mistakes that turned out to be vertex ordering mistakes. As long as you remember to always construct the vertices in the counter clockwise manner (as viewed from the outside of the cube) the rendering should work fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top