OpenGL cube vertex and indices explained

Submitted by Dickens A S on Wed, 10/26/2022 - 16:18

Understanding the vertex coordinates and corresponding indices slightly complicated. 
Sometimes the developer gets carried away due to too many numbers needs to be maintained in the VBO array

This article gives a typical approach to number in counter clockwise and group indices same way

Let us go by step by step

Front face
---------------

3 ---- -----2
|           |
|           |
0 ----------1

From bottom left you can count 0,1,2,3 counter clock wise

Back Face
----------------

7 ---- -----6
|           |
|           |
4 ----------5

From bottom left you can count 4,5,6,7 counter clock wise

Now how to write a vertex array
val vertices1 = floatArrayOf(
-1f, -1f, 1f,
1f, -1f, 1f,
1f, 1f, 1f,
-1f, 1f, 1f,
-1f, -1f, -1f,
1f, -1f, -1f,
1f, 1f, -1f,
-1f, 1f, -1f
)
How to understand this array, we need to draw axes from the middle center of the cube, A typical explanation given below

              To (+Y)
                 |
                 |
                 |
                 |      /  Ba ( -Z)
                 |    /
                 |  /
(-X) Le ________ |/___________ Ri (+X)
                /|
              /  |
            /    |
     (+Z) Fr     |
                 |
                 |
               Bo (-Y)

Expansion for the abbreviations used 

Fr   -- Front 
Ri   -- Right 
Ba  -- Back 
Le  -- Left 
Bo  -- Bottom 
To  -- Top 

For all the left points which are 0,3,4,7 the X will be negative        ----> -1 
For all the right points which are 1,2,6,5 the X will be positive       ----> +1 
For all the bottom points which are 0,1,5,4 the Y will be negative ----> -1 
For all the top points which are 2,3,7,6 the Y will be positive         ----> +1 
For all the back points which are 4,5,6,7 the Z will be negative      ----> -1 
For all the front points which are 0,1,2,3 the Z will be positive      ----> +1 

Indices are two triangle split up for one side of the qube. 
Now let us assume indices, we need 12 triangles needs to be numbered. 
This is a typical order for better understand in counter clock wise. 

Front --> Right --> Back --> Left --> Bottom --> Top 
0,1,2       1,5,6       7,6,5      4,0,3      4,5,1            3,2,6 
2,3,0       6,2,1       5,4,7      3,7,4      1,0,4            6,7,3 

To remember numbering we can use the below numbering pattern 
zero    ---- to ----zero     (0,1,2,2,3,0) 
one     ---- to ---- one     (1,5,6,6,2,1) 
seven ---- to ---- seven  (7,6,5,5,4,7) 
four    ---- to ---- four     (4,0,3,3,7,4) 
four    ---- to ---- four     (4,5,1,1,0,4) 
three  ---- to ---- three   (3,2,6,6,7,3) 

For invisible bottom, back and left we do the numbering clock wise, 
for others we do it counter clock wise So the code will look like 

val indices = shortArrayOf( 0, 1, 2, 2, 3, 0, 1, 5, 6, 6, 2, 1, 7, 6, 5, 5, 4, 7, 4, 0, 3, 3, 7, 4, 4, 5, 1, 1, 0, 4, 3, 2, 6, 6, 7, 3 ) 

Now when you call the OpenGL method inside the draw loop the elements will be rendered based on indices 

glDrawElements(GL_TRIANGLES, indices.size, GL_UNSIGNED_SHORT, NULL) 

End of Article

Add new comment