• Advertisement
Sign in to follow this  

glBegin/glEnd to glDrawArrays

This topic is 3613 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 some code that looks like this glBegin(GL_TRIANGLES); for (x=0 ...) { glVertex3f(vertex[polygon[x].a].x, vertex[polygon[x].a].y, vertex[polygon[x].a].z); glVertex3f(vertex[polygon[x].b].x, vertex[polygon[x].b].y, vertex[polygon[x].b].z); glVertex3f(vertex[polygon[x].c].x, vertex[polygon[x].c].y, vertex[polygon[x].c].z); } glEnd(); but I can't use anymore glBegin/glEnd. The code must be rewrite using glDrawArrays or smth else... Any idea how to make this ?

Share this post


Link to post
Share on other sites
Advertisement
You already have collected your vertexes and polygon indexes into 2 arrays. What you have to do now is:

INT iNumIndices = 3 * NumPolygons;
glEnableClientState( GL_VERTEX_ARRAY );
glVertexPointer( 3, GL_FLOAT, 0, (GLvoid *)vertex );
glDrawElements( GL_TRIANGLES, iNumIndices, GL_UNSIGNED_SHORT, (GLvoid *)polygon );
glDisableClientState( GL_VERTEX_ARRAY );

change GL_UNSIGNED_SHORT and GL_FLOAT with the proper enum ID for vertex and indexes components data type and change the magic number "3" into 4 if you are using 4 components for vertexes coordinates (X,Y,Z and W).

Notice that you can avoid call any time glEnableClientState() / glDisableClientState() if you are using this system to draw all your objects. In such case it is enough to you to call glEnableClientState() once during the application setup and call glDisableClientState() during the cleanup.

I hope this can help.

Share this post


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

  • Advertisement