OpenGL ES - glDrawElements - Misunderstanding Indexes

I wonder if anyone can help me understand how indexes work with glDrawElements. In the example below (taken from http://www.everita.com/lightwave-collada-and-opengles-on-the-iphone ), the author mentions that you can have only one set of indexes, in this case

const GLushort tigerBottomIndices[] = { 0,1,2, 3,0,4, 1,5,6, … 

};

My question is what these indexes describe? Do I correctly assume that the first three are the positions of the vertices, and the second three are the corresponding normals, and the last three are the coordinates of the texture?

Thanks in advance!

 #import "OpenGLCommon.h" const Vertex3D tigerBottomPositions[] = { {0.176567, 0.143711, 0.264963}, {0.176567, 0.137939, 0.177312}, {0.198811, 0.135518, 0.179324}, … }; const Vertex3D tigerBottomNormals[] = { {-0.425880, -0.327633, 0.350967}, {-0.480159, -0.592888, 0.042138}, {-0.113803, -0.991356, 0.065283}, … }; const GLfloat tigerBottomTextureCoords[] = { 0.867291, 0.359728, 0.779855, 0.359494, 0.781798, 0.337223, … }; const GLushort tigerBottomIndices[] = { 0,1,2, 3,0,4, 1,5,6, … }; glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glBindTexture(GL_TEXTURE_2D, tigerTextures[5]); glVertexPointer(3, GL_FLOAT, 0, tigerBottomPositions); glNormalPointer(GL_FLOAT, 0, tigerBottomNormals); glTexCoordPointer(2, GL_FLOAT, 0, tigerBottomTextureCoords); glDrawElements(GL_TRIANGLES, 210, GL_UNSIGNED_SHORT, tigerBottomIndices); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); glDisableEnableClientState(GL_TEXTURE_COORD_ARRAY); 
+6
source share
2 answers

Each value in the index array indicates the same time for the position, normal and texture coordinates.

They are organized only by groups of 3, because they simply describe the vertices of a triangle, so, of course, 3 vertices = 1 triangle.

 const GLushort tigerBottomIndices[] = { 0,1,2, // #1 Triangle 3,0,4, // #2 Triangle 1,5,6, // #3 Triangle … 

So, let's pick the first value of these indices, 0 .

It means:

Select vertex position number 0

Also select normal vertex number 0

And select texture coordinates 0

 const Vertex3D tigerBottomPositions[] = { {0.176567, 0.143711, 0.264963}, // This is the position number 0 {0.176567, 0.137939, 0.177312}, {0.198811, 0.135518, 0.179324}, … }; const Vertex3D tigerBottomNormals[] = { {-0.425880, -0.327633, 0.350967}, // This is the normal number 0 {-0.480159, -0.592888, 0.042138}, {-0.113803, -0.991356, 0.065283}, … }; const GLfloat tigerBottomTextureCoords[] = { 0.867291, 0.359728, // These are the tex-coords number 0 0.779855, 0.359494, 0.781798, 0.337223, … }; 

So, this information is sent to the vertex shader:

VertexPosition: 0.176567, 0.143711, 0.264963

VertexNormal: -0.425880, -0.327633, 0.350967

VertexTextureCoordinates: 0.867291, 0.359728

...

If you use no indexes, opengl will send this vertex data linearly, so after sending the vertex data number 0 it will send the data to position 1 of the arrays, then 2, 3, 4, etc.

This is good, but sometimes your triangles end with one or two identical vertices. Consider this:

enter image description here

You can see 2 triangles forming a square, and they have 2 common vertices, 0 and 2. Therefore, instead of 6 vertices, which are 3 for each triangle, we have only 4, and 2 tragedies use the same data for 2 of their vertices. This is good for performance, especially when you have large models with hundreds of triangles.

To draw the first triangle, we need the numbers of vertices 0, 1, and 2, and for the second triangle we need the numbers of vertices 0, 2, and 3.

See without an array of indices opendl will try to use the vertices 0, 1 and 2 (ok for the first triangle), but for the second triangle opengl will look for the vertices 3, 4 and 5. This is not true.

And so we create an index array, so opengl can send the correct vertices for the vertex shader. In our case, our index array will look like this:

 const GLushort tigerBottomIndices[] = { 0,1,2, 0,2,3, } 
+26
source

Indexes mean in the sense of the indexes of the elements in the array. Index 0 refers to the first element of the array, index 1 refers to the second, etc.

In your example, the first indices 0, 1, 2 address the first three vertices that have the positions of the first three Vertex3D elements of the Vertex3D array, the normal of the first three tigerBottomNormals elements (with 3 floats forming one normal vector), and also for texture coordinates.

The first argument to the glDrawElements call tells OpenGL how to form primitives from indexed vertices. GL_TRIANGLES means that every three indexed vertices form a triangle.

Thus, the vertices with indices 0, 1, 2 form a triangle, 3,0,4 form the next, 1,5,6 form another triangle, etc.

+1
source

Source: https://habr.com/ru/post/908413/


All Articles