Thread: glDrawElements()

  1. #1
    Unregistered
    Guest

    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.

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    ask here http://www.opengl.org they can solve this faster than i can.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    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.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  4. #4
    Registered User
    Join Date
    Jan 2004
    Posts
    68
    I found this very useful, I got my array to draw the points I want.

    thx alot.
    -Ti22-

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. glDrawElements()
    By linuxdude in forum Game Programming
    Replies: 5
    Last Post: 07-11-2006, 01:41 PM