ASHUTOSH
ASHUTOSH

Reputation: 892

Order of Indices affect the cube structure in OPENGL

Ok I know the order would definitely affect the structure if it is in wrong order. But I have two sets of Indices which I suppose are correct to render a cube. When I use one of the set,the cube is rendered properly,but for the other it is not(Check the images).

Correct rendering Incorrect rendering

Here is what I am using: The following renders correctly

GLubyte cubeIndices[24]={0,3,2,1,2,3,7,6,0,4,7,3,1,2,6,5,4,5,6,7,0,1,5,4};
glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE,&cubeIndices);

This one doesnt render properly

GLubyte cubeIndices[24]={1,2,3,4,5,8,7,6,1,5,6,2,2,6,7,3,3,7,8,4 ,5,1,4,8};
glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE,&cubeIndices);

I am using same set of vertices for both. Can anyone tell me why is not working???

Upvotes: 1

Views: 1944

Answers (1)

Reto Koradi
Reto Koradi

Reputation: 54592

While the two index sequences look completely different at first sight, they are indeed closely related. The main difference is that the second one contains 1-based indices, while the first one contains 0-based indices. You will need 0-based indices for OpenGL.

There's another difference. Grouped by quads, the first sequence is this:

0,3,2,1
2,3,7,6
0,4,7,3
1,2,6,5
4,5,6,7
0,1,5,4

The second one, after subtracting 1 from each value, is this:

0,1,2,3
4,7,6,5
0,4,5,1
1,5,6,2
2,6,7,3
4,0,3,7

The order of the quads is obviously different. Now re-ordering the second list to make the order of quads the same, and writing them side by side for comparison:

0,3,2,1    0,1,2,3
2,3,7,6    2,6,7,3
0,4,7,3    4,0,3,7 --> 0,3,7,4
1,2,6,5    1,5,6,2
4,5,6,7    4,7,6,5
0,1,5,4    0,4,5,1

For the 3rd quad, I also cyclically shifted the indices for easier comparison.

The difference is that all the quads have exactly the reverse order when comparing the two index sequences. This can be important when rendering them with OpenGL, because it determines the winding order of the quads. Particularly if you enable backface culling, you want to make sure that the orientation of polygons is counter-clockwise when looking at the cube from the outside.

Upvotes: 2

Related Questions