Results 1 to 4 of 4

Thread: How To Use - glDrawElements()

  1. #1
    Junior Member Newbie
    Join Date
    Aug 2005
    Posts
    5

    How To Use - glDrawElements()

    I've found a lot of documentation but I've yet to locate a simple working example of glDrawElements. The OpenGL programmer's guide provides an obscure pseudo example but it doesn't explain how the vertex data is stored in the array.

    Could someone show me a version of glDrawElements() drawing a simple primative?

  2. #2
    Senior Member Regular Contributor
    Join Date
    Aug 2005
    Posts
    145

    Re: How To Use - glDrawElements()

    Here's a simple example that draws a quad with per vertex position, color, and texcoords.

    Code :
    struct Vertex
    {
    	float pos[3];
    	float color[4];
    	float st[2];
    } quadVerts[4] = {
    	{{0,0,0}    ,{1,0,0,1}, {0,0}},
    	{{0,100,0}  ,{0,1,0,1}, {0,1}},
    	{{100,100,0},{0,0,1,1},	{1,1}},
    	{{100,0,0}  ,{1,1,1,1},	{1,0}}
    };
    GLushort quadIndices[4] = { 0, 1, 2, 3 };
    glVertexPointer(3, GL_FLOAT, sizeof(Vertex), quadVerts[0].pos);
    glEnableClientState(GL_VERTEX_ARRAY);
    glColorPointer(4, GL_FLOAT, sizeof(Vertex), quadVerts[0].color);
    glEnableClientState(GL_COLOR_ARRAY);
    glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), quadVerts[0].st);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glDrawElements(GL_QUADS, 4, GL_UNSIGNED_SHORT, quadIndices);
    glDisableClientState(GL_COLOR_ARRAY);
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);

  3. #3
    Junior Member Newbie
    Join Date
    Aug 2005
    Posts
    5

    Re: How To Use - glDrawElements()

    That's exactly what what I was looking for. Thank you.

  4. #4
    Senior Member Regular Contributor
    Join Date
    Aug 2005
    Posts
    145

    Re: How To Use - glDrawElements()

    My pleasure. If you want to use multiple texcoords per vertex, you'll probably need to grab the extension header(s). Really the only difference is that you need to activate the texture you want (using glClientActiveTexture) before you call glTexCoordPointer/glEnableClientState/glDisableClientState. This can trip you up if you're not careful (I know from personal experience in this area :-/).

    You can find all the extension stuff you may need here. Grab glext.h and wglext.h for Windows.
    http://oss.sgi.com/projects/ogl-sample/registry/

Similar Threads

  1. about glDrawElements
    By freesui1984 in forum OpenGL ES
    Replies: 3
    Last Post: 10-10-2008, 04:17 AM
  2. Getting glDrawElements
    By Cribs in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 03-24-2007, 09:46 PM
  3. glDrawElements
    By in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 11-28-2005, 04:39 AM
  4. Once & Again: glDrawElements
    By t33mu in forum OpenGL: Advanced Coding
    Replies: 8
    Last Post: 06-02-2001, 09:16 AM
  5. glDrawElements()
    By eckiller in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 05-04-2000, 11:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean