-
glDrawElements()
Hi guys, a little help required with glDrawElements().
I can't for the life of me get it to draw anything, heres some code I am using
Code:
void display( void )
{
GLuint indices[] = { 50, 50,
50, 70,
70, 70,
70, 50 };
glClear( GL_COLOR_BUFFER_BIT );
glPushMatrix();
glColor3f( 1.0, 1.0, 1.0 );
glEnableClientState( GL_VERTEX_ARRAY );
glVertexPointer( 2, GL_INT, 0, indices );
glDrawElements( GL_POLYGON, 8, GL_UNSIGNED_INT, indices );
glDisableClientState( GL_VERTEX_ARRAY );
glPopMatrix();
glutSwapBuffers();
}
In the opengl red book it states that the type argument for glDrawElements() should be GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT or GL_UNSIGNED_INT.
However it also states that the type argument for glVertexPointer() should be GL_SHORT, GL_INT, GL_FLOAT or GL_DOUBLE. No unsigned type!!!
So, do you not need glVertexPointer() when using glDrawElements()? I tried it and that didn't work either.
Any comments would be great.
:)
-
ask here http://www.opengl.org they can solve this faster than i can.
-
For those interested the answer is that glDrawElements needs 2 array's, the vertex array and the index array. You pass the index array to glDrawElements not the vertex array
Code:
GLfloat vertexs[] = { 25.0, 25.0,
25.0, 75.0,
75.0, 75.0,
75.0, 25.0 };
GLubyte indices[] = { 0, 1, 2, 3 };
glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_INDEX_ARRAY );
glIndexPointer( GL_UNSIGNED_BYTE, 0, indices );
glVertexPointer( 2, GL_FLOAT, 0, vertexs );
glDrawElements( GL_QUADS, 4, GL_UNSIGNED_BYTE, indices );
The index array specifies which order to use the vertices in so you could do
Code:
GLfloat vertexs[] = { 25.0, 25.0,
25.0, 75.0,
75.0, 75.0,
75.0, 25.0 };
GLubyte indices[] = { 1, 2, 3 };
glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_INDEX_ARRAY );
glIndexPointer( GL_UNSIGNED_BYTE, 0, indices );
glVertexPointer( 2, GL_FLOAT, 0, vertexs );
glDrawElements( GL_TRIANGLES, 3, GL_UNSIGNED_BYTE, indices );
Which only uses 3 of the vertices and draws a triangle.
-
I found this very useful, I got my array to draw the points I want. :D
thx alot.