Jump to content
  • Advertisement
Sign in to follow this  
Alessandro

Problem with vertex arrays

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

If you intended to correct an error in the post then please contact us.

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:

o quadT.obj
mtllib quadT.mtl
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 this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!