# Problem with vertex arrays

This topic is 2835 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a simple quad, made of two triangles, with the following vertices/indices setup:

 float verts[]={ -0.2 0 0.2 0.2 0 0.2 0.2 0 -0.2 -0.2 0 -0.2 } int indices[]={ 4 1 2 3 4 2 } glVertexPointer( 3, GL_FLOAT, 0, verts ); glEnableClientState( GL_VERTEX_ARRAY ); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_COLOR_ARRAY); glDrawElements( GL_TRIANGLES, 2*3, GL_UNSIGNED_INT, indices ); glDisableClientState( GL_VERTEX_ARRAY ); 

The output is the one attached: I cannot understand why that happens, obviously there is something wrong with the index setup.

But the above information, if saved to a .obj file, is displayed correctly in a 3D application:

g default
v -0.2 0 0.2
v 0.2 0 0.2
v 0.2 0 -0.2
v -0.2 0 -0.2
usemtl Default
f 4 1 2
f 3 4 2

Can anybody see what's wrong? Thanks

##### Share on other sites
Indices in vertex arrays are 0-based, so your indices are off by one. That is, index value 1 references the second vertex. The OBJ file format, however, is 1-based, so index 1 references the first vertex.

Thank you Bob

##### Share on other sites
I still have problems with the normals: they are not working as expected.

The .obj parser works just fine, storing data into the arrays, but I cannot understand how to convert and send data to the vertex array.

If you have a cube (8 vertices, 12 triangles) the resulting .obj will provide 24 normals; if you have just a quad (4 vertices, 2 triangles), the resulting .obj will have 4 normals.
So, how could I calculate the normal array size? And fill it with data, given that the .obj parser stores data in a normals[numNormals][3] array?

##### Share on other sites
A vertex in OpenGL is a collection of all the attributes necessary to describe it. Thus, vertex is a collection of position, color, normal, and/or whatever attribute you have. If you think of the implication of that, you will realize that a cube with normals has 24 vertices, not 8. Some positions are shared, but the shared positions do not have the same normals, and so the vertices are not the same.

The OBJ file format is not directly suitable for vertex arrays, since it has individual indices per attribute but vertex arrays require a single index array for all attributes. You have to either flatten the vertex structure and don't use an index array, or you have to re-structure the arrays so that all attribute indices are the same. The first option is the easiest. Just parse the indices from the file, and instead of saving the indices you save the vertex information into a linear array as you go, instead of keeping separate vertex and index arrays.

##### Share on other sites
Thanks for the explanation. If I get rid of indices, later I'd use glDrawArrays instead of glDrawElements, right?

That is correct.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 21
• 22
• 11
• 25