Honza Šurík
Honza Šurík

Reputation: 55

glDrawElements - how does it "consume" indices?

I am curious how glDrawElements uses it's indices. Let's say I have array of indices {1, 2, 3, 4, 5, 6}, drawn by GL_TRIANGLES. My question is, whether it forms 2 triangles by indices "123", "456" or 4 triangles "123", "234", "345", "456". I am importing .obj model, data are loaded as they should, but the rendering just gets messed up.

Upvotes: 2

Views: 990

Answers (2)

M.S.
M.S.

Reputation: 102

If you use GL_TRIANGLES it will be 123, 456. If you use GL_TRIANGLE_STRIPS it will be 123, 234, 345, 456.

Upvotes: 5

Damon
Damon

Reputation: 70136

In GL_TRIANGLES mode, it needs 3 vertices for every triangle, thus it will pull 3 indices at a time (so, it will draw the two triangles with indices 1,2,3 and 4,5,6).

Different figures apply if you have adjacency or draw a triangle strip, of course.

Also, since you mentioned that you are importing an OBJ file, do note that there may be different indices on vertices and texture coordinates and/or normals in the OBJ file.
This is not a bug, but a feature. This is allowable in OBJ, and many exporters do that when surfaces are smooth shaded (i.e. two faces share a normal) to conserve space.
You must construct a list of unique vertices, each with its own normal, texcoord, etc. and use the indices for these.

Upvotes: 4

Related Questions