Jump to content
  • Advertisement
Sign in to follow this  
Alessandro

converting immediate mode to vertex

This topic is 3436 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

Hi guys, I'd like to convert the following code using glDrawElements instead. glBegin (GL_TRIANGLES); for (int i=0; i<1024; i++) { for(int j=0;j<3;j++) { int vi=3*face_indicies[i*9 + j]; glVertex3f (vertices[vi+0],vertices[vi+1],vertices[vi+2]); } } glEnd(); What I'd like to do is to use glDrawElements, but when I use it the mesh vertices shown on screen are all messed up. This is the routine that I tried to use and that should do the job done in immediate mode: for(int i = 0; i < 1024*9; i+=9) { glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_BYTE, &face_indicies); } Thanks for any help...

Share this post


Link to post
Share on other sites
Advertisement
I think you misunderstood how glDrawElements is supposed to operate. The idea is to put the entire geometry into a vertex buffer, and call glDrawElements one single time, not once per triangle. The way you attempted is going to be even less efficient than immediate mode.

Share this post


Link to post
Share on other sites
Hi Yann, thanks for your clarification. So the right way to use it would be just:

glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_BYTE, face_indicies);

The problem I find is that I can't convert the way those vertices array are packed (they come straight out from a software that exports a 3d mesh into text) so that glDrawElements can read it.

With the immediate mode the mesh is render correctly, so I guess face_indicies are not sorted correctly for the glDrawElements function ?

Share this post


Link to post
Share on other sites
Quote:
Original post by Alessandro
The problem I find is that I can't convert the way those vertices array are packed (they come straight out from a software that exports a 3d mesh into text) so that glDrawElements can read it.

How does that stop you from, after loading the data, reorganize it to a suitable format for glDrawElements?

Share this post


Link to post
Share on other sites
Well, I really can't see what's going on in that immediate mode routine:

face_indicies is organized so that it holds an array of 1024*9.

What I see is that vertices are given skipping data from the array: I really can't figure what to sort: only vertices ? or also face indicies ?

Share this post


Link to post
Share on other sites
Well, I need a clarification about how glDrawElements works.
Provided that the following code, running in immediate mode, works just fine:

glBegin (GL_TRIANGLES);
for(int i=0;i<FACES;i++)
{
for(int j=0;j<3;j++)
{
int vi=face_indicies[j];
glVertex3f (vertices[vi][0],vertices[vi][1],vertices[vi][2]);
}
}
glEnd ();

And that using the following doesn't work:

glDrawElements(GL_TRIANGLES, 1024*3, GL_UNSIGNED_BYTE, face_indicies);
(I initialized the vertex pointer properly:
glVertexPointer(3, GL_FLOAT, 0, vertices);
glEnableClientState(GL_VERTEX_ARRAY);
)

Immediate mode works very simple: it loops every face, retrieving the vertex index and building one triangle per loop.

So how glDrawElements works instead ? I supposed it would be just straightforward, since immediate mode works just fine.

Since it is not, what could it possibly be wrong ? Providing faces and vertices arrays is not enough ? At this point I am VERY confused: are those arrays actually sorted out properly ? If not, why they work in immediate mode ?

sorry but I am very very confused, if someone could please explain ...

Share this post


Link to post
Share on other sites
You have a vertex list that contains well, a list of vertices. Preferably this list is stripped of duplicate verts for performance reasons.

You also have a index list that references which vertices in the vertex list to use when making the triangles.





Share this post


Link to post
Share on other sites
Quote:
Original post by Alessandro
glDrawElements(GL_TRIANGLES, 1024*3, GL_UNSIGNED_BYTE, face_indicies);
(I initialized the vertex pointer properly:
glVertexPointer(3, GL_FLOAT, 0, vertices);
glEnableClientState(GL_VERTEX_ARRAY);
)

Think about this piece of code for a minute. First you draw the vertex array, and then you setup the proper vertex array states. Don't you think setting up first and then draw it be more logical.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!