glDrawElements

Started by
6 comments, last by ViLiO 17 years, 7 months ago
I seem to be having some problems getting glDrawElements to work. Using the fixed function implementation below, I try to draw my mesh object:

int numIndicies = mesh->GetNumIndicies();
float* verts = mesh->GetVertexBuffer();
int* indicies = mesh->GetIndexBuffer();
glPushMatrix();
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_TRIANGLES);
    for(int i=0; i<numIndicies; i+=3)
    {
        glVertex3f(verts[indicies],verts[indicies[i+1]],verts[indicies[i+2]]);
    }
glEnd();
glPopMatrix();
The code above renders the mesh without any problems. Unfortunately, I am unable to render the mesh using (what I think is) a comparable implementation using glDrawElements:

glPushMatrix();
glColor3f(1.0f, 1.0f, 1.0f);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, mesh->GetVertexBuffer());
glDrawElements(GL_TRIANGLES, mesh->GetNumIndicies(), GL_INT,mesh->GetIndexBuffer());
glDisableClientState(GL_VERTEX_ARRAY);
glPopMatrix();
I've been staring at this for a while, but I can't seem to find the missing piece. Any help is greatly appreciated.
Advertisement
Indices must be unsigned; use GL_UNSIGNED_INT instead.
Due to my informations glDrawElements needs one of
GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT, or GL_UNSIGNED_INT
for the type parameter. You are using GL_INT.


EDIT: As Brother Bob has stated a minute earlier;)
That was it. Thank you!
I'm returning to this problem because I still seem to have some inconsistencies with immediate mode and glDrawElements(). This new problem came to light after fixing the type parameter in glDrawElements to GL_UNSIGNED_INT.

Using immediate mode my test model renders as correctly as such:




Whereas using glDrawElements produces undesireable results:




I am under the impression that both implementations (shown above more or less) should yield the same results.

Thanks for your attention.
Perhaps it is because ATM you're trying out glDrawElements only, but using indices at all makes sense only if you are re-using vertices. As long as each vertex is used only once, indices only increase the costs.

The structure you have should work fine if using glDrawArrays.


EDIT: Err, where has the reply of ViLiO gone to? IMHO he was totally right with his hint... Perhaps you haven't seen it: ViLiO stated out that the indices used by OpenGL do index _vertices_ and not co-ordinates of vertices. That means, that something like
glVertex3f(vertex[index].x,vertex[index].y,vertex[index].z);
(i.e. using the same index for all 3 co-ordinates) is the equivalent to what OGL does in glDrawElements.
Quote:Original post by haegarr
Perhaps it is because ATM you're trying out glDrawElements only, but using indices at all makes sense only if you are re-using vertices. As long as each vertex is used only once, indices only increase the costs.

The structure you have should work fine if using glDrawArrays.


EDIT: Err, where has the reply of ViLiO gone to? IMHO he was totally right with his hint... Perhaps you haven't seen it: ViLiO stated out that the indices used by OpenGL do index _vertices_ and not co-ordinates of vertices. That means, that something like
glVertex3f(vertex[index].x,vertex[index].y,vertex[index].z);
(i.e. using the same index for all 3 co-ordinates) is the equivalent to what OGL does in glDrawElements.


That did it. The way I needed to think about how glDrawElements() works comparably to immediate mode would be with the following code:

unsigned int numIndicies = mesh->GetNumIndicies();float* verts = mesh->GetVertexBuffer();unsigned int* indicies = mesh->GetIndexBuffer();glPushMatrix();glScalef(0.005f, 0.005f, 0.005f);glColor3f(1.0f, 1.0f, 1.0f);glBegin(GL_TRIANGLES);    for(int i=0; i<numIndicies; i++)    {       //notice the offsets are added to indicies       glVertex3f(verts[indicies],verts[indicies+1],verts[indicies+2]);    }glEnd();glPopMatrix();


The redbook illustrates your points made subtley. Once again, you the gamedev community catches a detail that I overlooked. Thanks for your help haegarr and ViLiO. Pluses for everyone (will have to hunt down ViLiO).

Thanks!
Quote:Original post by haegarr
EDIT: Err, where has the reply of ViLiO gone to? IMHO he was totally right with his hint... Perhaps you haven't seen it: ViLiO stated out that the indices used by OpenGL do index _vertices_ and not co-ordinates of vertices. That means, that something like
glVertex3f(vertex[index].x,vertex[index].y,vertex[index].z);
(i.e. using the same index for all 3 co-ordinates) is the equivalent to what OGL does in glDrawElements.

I wasn't 100% sure of myself so figured I'd remove my post in case it contained misinformation [embarrass]
Good to know my OpenGL isn't that rusty after all though as I tend to think only in D3D these days [wink]
Richard 'ViLiO' Thomasv.net | Twitter | YouTube

This topic is closed to new replies.

Advertisement