Jump to content
  • Advertisement
Sign in to follow this  
SomeOtherDude

OpenGL Another post about glDrawElements

This topic is 4057 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 am trying to implement the use of the glDrawElements function as a pre-req for implementing VBOs in a demo I am working on. I can't figure out how glDrawElements works with multiple data types at once (ex: verts, norms, tex coords). I have a header file like so: int some3dobject_numVerts = 7; GLfloat some3dobject_Verts [] = { -0.656056f, 3.581464f, -3.808501f, -0.584739f, 3.581464f, -3.808501f, -0.656056f, 3.558471f, -3.881124f, -0.584739f, 3.558471f, -3.881124f, -0.656056f, 3.581464f, -3.808501f, -0.584739f, 3.581464f, -3.808501f, -0.656056f, 3.558471f, -3.881124f, -0.584739f, 3.558471f, -3.881124f }; int some3dobject_numTexs = 7; GLfloat some3dobject_Texs [] = { 0.000000f, 0.000000f, 1.000000f, 0.000000f, 0.000000f, 1.000000f, 1.000000f, 1.000000f, 0.000000f, 0.000000f, 1.000000f, 0.000000f, 0.000000f, 1.000000f, 1.000000f, 1.000000f }; int some3dobject_numNorms = 11; GLfloat some3dobject_Norms [] = { 0.000000f, 0.953359f, -0.301840f, 0.000000f, 0.953359f, -0.301840f, 0.000000f, 0.953359f, -0.301840f, 0.000000f, 0.953359f, -0.301840f, 0.000000f, 0.953359f, -0.301840f, 0.000000f, 0.953359f, -0.301840f, 0.000000f, 0.953359f, -0.301840f, 0.000000f, 0.953359f, -0.301840f, 0.000000f, 0.953359f, -0.301840f, 0.000000f, 0.953359f, -0.301840f, 0.000000f, 0.953359f, -0.301840f, 0.000000f, 0.953359f, -0.301840f }; int some3dobject_numFaces = 3; GLuint some3dobject_Vidxs [] = { 0,1,2, 1,3,2, 4,5,6, 5,7,6 }; GLuint some3dobject_Tidxs [] = { 0,1,2, 1,3,2, 4,5,6, 5,7,6 }; GLuint some3dobject_Nidxs [] = { 0,1,2, 3,4,5, 6,7,8, 9,10,11 }; (the vertex, normal, and texture coordinates in the arrays above are just examples, thats not the actual object I am drawing. Regardless of what object I draw with it, the normal and texture coords don't seem to be getting passed right) I am attempting to feed this info to the glDrawElements function like so: glEnableClientState ( GL_NORMAL_ARRAY ); glEnableClientState ( GL_TEXTURE_COORD_ARRAY ); glEnableClientState ( GL_VERTEX_ARRAY ); glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE ); glBindTexture(GL_TEXTURE_2D, texture[0]); glNormalPointer ( GL_FLOAT, 0, &some3dobject_Norms[0] ); glTexCoordPointer (2, GL_FLOAT, 0, &some3dobject_Texs[0] ); glVertexPointer ( 3, GL_FLOAT, 0, &some3dobject_Verts[0] ); glDrawElements ( GL_TRIANGLES, some3dobject_numfaces * 3, GL_UNSIGNED_INT, &some3dobject_Vidxs[0] ); glDisableClientState ( GL_VERTEX_ARRAY ); This doesn't work as expected. Should my vertex, normal, and texture indexes all be in one array, or is it possible to pass them seperately? Also, I am unclear as to what exactly the second parameter of glDrawElements should be. Reading the docs indicate it is the number of elements to be rendered, so I am guessing in this case it is the number of triangles? Please forgive my ignorance, but isnt that the same as the number of faces? Either way, I am unsure wether it starts its count from 0 or 1. If my attempted explanation of the problem isn't clear enough, I have posted a link below to another question someone posted that is basically the same as mine. http://lists.apple.com/archives/mac-opengl/2002/Jun/msg00149.html I am thankful for any help. [Edited by - SomeOtherDude on August 13, 2007 2:27:38 AM]

Share this post


Link to post
Share on other sites
Advertisement
Number of elements means the size of the index array, so numfaces * 3 is correct. I also noticed that you have 3 separate index arrays: some3dobject_Vidxs, some3dobject_Tidxs, some3dobject_Nidxs. OpenGL supports only one index array per draw call. Just re-arrange the normal and texture arrays to match size and order of the vertex array.

Share this post


Link to post
Share on other sites
You can keep them in separate. Nothing wrong with that.
For each vertex, you need to have 1 texcoord, 1 normal.

Quote:
Also, I am unclear as to what exactly the second parameter of glDrawElements should be.


It should be the number of indices. Remember that each triangle has 3 indices so the number has to be a multiple of 3.

Share this post


Link to post
Share on other sites
I can't really see anything that looks wrong from what you've got there. I'm assuming your coordinates are correct, and that you could draw what you're trying to draw using immediate mode (glVertex,glNormal,etc).

The second parameter should be the number of elements (vertices) you're rendering. So 3 times the number of triangles in your case. I'm not sure what you mean about starting with 0 or 1, if you want to draw 3 triangles in GL_TRIANGLES mode you need to draw 9 elements. I'm also not sure what you mean about the number of faces, where do you need to use that?


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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!