Results 1 to 5 of 5

Thread: Newbie problem with glDrawElements/Arrays

  1. #1
    Senior Member Regular Contributor
    Join Date
    Jun 2006
    Location
    Edinburgh - Scotland
    Posts
    146

    Newbie problem with glDrawElements/Arrays

    The following code works and I get a triangle
    Code :
      static GLfloat vtx[] =
      {
           -10.0f, 0.0f, -50.0f,
            10.0f, 0.0f, -50.0f,
             0.0f, 10.0f, -50.0f
      };
     
      glBegin(GL_TRIANGLES);
      glVertex3f(vtx[0], vtx[1], vtx[2]);
      glVertex3f(vtx[3], vtx[4], vtx[5]);
      glVertex3f(vtx[6], vtx[7], vtx[8]);
      glEnd();
    However when I try to use glDrawElements
    Code :
      static GLuint idx[] = { 0, 1, 2 };
     
      glEnableClientState(GL_VERTEX_ARRAY);
      glVertexPointer(3, GL_FLOAT, 0, &vtx[0]);
      glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, &idx[0]);
      glDisableClientState(GL_VERTEX_ARRAY);
    Or replcae the glDrawElements with glDrawArrays
    Code :
      glDrawArrays(GL_TRIANGLES, 0, 3);
    I get nothing....Given that the first code segment works what is it I'm doing wrong? (or even just suggestions).

    I've tried clockwise/aniti-clockwise indices, but since the first code segment works I didn't think that could be the problem.

    Thing is I've got another part of the code all working using glDrawArrays and glDrawElements using VBOs so I'm at a loss as to why this simpler code doesn't work.

    Cheers,

    STU!

  2. #2
    Guest

    Re: Newbie problem with glDrawElements/Arrays

    When a VBO is bound, the pointer parameter of the gl*Pointer functions acts as an offset into the buffer object. If you pass a pointer to a vertex array while a VBO is bound it will use the pointer's value as the offset.

  3. #3
    Senior Member Regular Contributor
    Join Date
    Jun 2006
    Location
    Edinburgh - Scotland
    Posts
    146

    Re: Newbie problem with glDrawElements/Arrays

    Excellent! An addition of
    Code :
      glBindBuffer(GL_ARRAY_BUFFER, 0);
    and everything works.

    Thanks,
    STU!

  4. #4
    Senior Member OpenGL Pro dletozeun's Avatar
    Join Date
    Jan 2006
    Location
    FRANCE
    Posts
    1,367

    Re: Newbie problem with glDrawElements/Arrays

    If you add : glBindBuffer(GL_ARRAY_BUFFER, 0);
    you won't use VBOs...maybe it is what you want...

    If you want to use VBO you have to create it with glBufferData and then doing:

    Code :
    glBindBuffer(GL_ARRAY_BUFFER, yourVBObuffer);
    glVertexPointer(3, GL_FLOAT, 0, NULL);
    But it is not worth to do this if you want to draw only a triangle.

  5. #5
    Senior Member Regular Contributor
    Join Date
    Jun 2006
    Location
    Edinburgh - Scotland
    Posts
    146

    Re: Newbie problem with glDrawElements/Arrays

    I am using both (or maybe). The triangle was just an example because I couldn't get it to work. I already had glBindBuffer to my VBO buffer IDs when using them. What I didn't know was to bind to ID 0 when using non-VBOs (obvious now of course).

    --Stuart.

Similar Threads

  1. glDrawElements mismatched arrays
    By Pyramus in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 03-20-2012, 06:29 PM
  2. glDrawElements and Normals in different arrays
    By manolopm in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 09-12-2010, 05:56 PM
  3. just another newbie question about vertex arrays...:))
    By miko in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 02-17-2003, 01:51 PM
  4. What is the optimal size for vertex arrays passed to glDrawElements?
    By CGameProgrammer in forum OpenGL: Advanced Coding
    Replies: 8
    Last Post: 04-29-2002, 06:45 PM
  5. compiled vertex arrays & glDrawElements
    By worm in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 12-11-2000, 11:22 AM

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