Results 1 to 3 of 3

Thread: glDrawElements() and the built-in gl_VertexID

  1. #1
    Member Contributor
    Join Date
    Jun 2011
    Posts
    90

    glDrawElements() and the built-in gl_VertexID

    I have some problems with using glDrawElements and the built-in shader variable gl_VertexID.

    I am doing hair rendering. I saved the positions of the vertices in ONE array, and used the following code to draw the hair model:
    Code :
    int pointIndex = 0;
    for ( int hairIndex=0; hairIndex < hairCount; hairIndex++ ) 
    {
    glDrawArrays( GL_LINE_STRIP, pointIndex, segmentCount+1 );
    pointIndex += segmentCount+1;
    }
    the variable hairCount represents the number of hair strand in this model, and the segmentCount represents the number of line segments in each hair strand, which is the same for all the hair strands.

    But now I want to take advantage of the built-in GLSL variable gl_VertexID, so I replaced glDrawArrays with glDrawElements, and constructed an indices array as follows:
    Code :
    GLuint *hairElementsIndex = new GLuint[POINT_COUNT];
    for(int i=0; i<POINT_COUNT; ++i)
        hairElementsIndex[i]=i;
    the constant POINT_COUNT is the number of vertices in the hair model.
    And changed the drawing call to:
    Code :
     for ( int hairIndex=0; hairIndex < hairCount; hairIndex++ ) 
    {
    glDrawElements( GL_LINE_STRIP, segmentCount+1, GL_UNSIGNED_INT,
    (GLvoid *)(hairIndex * (segmentCount+1)) );
    }
    However, the rendering result is not as expected, the end points of some hair strands seem to connect with the beginning points of some other hair strands. I cannot find any differences between these two drawing calls. Could anyone help me?



    Another question is, if I sed glDrawElements, would gl_VertexID equal to the value in the indices array. In my case, I called glDrawElements several times. Will gl_VertexID restart from zero at each glDrawElements call? (Actually, I want gl_VertexID to equal to the index into the position array of the hair vertices.)

  2. #2
    Super Moderator OpenGL Guru imported_dorbie's Avatar
    Join Date
    Jul 2000
    Location
    Bay Area, CA, USA
    Posts
    3,966

    Re: glDrawElements() and the built-in gl_VertexID

    gl_Vertex_ID is not incompatible with glDrawArrays, the number is implicitly incremented for each vertex.

    Are you are confusing data indices with the vertex count in the index array?

  3. #3
    Member Contributor
    Join Date
    Jun 2011
    Posts
    90

    Re: glDrawElements() and the built-in gl_VertexID

    Sorry, I did confuse these terms. I make it clear now.

    If I use the following code to draw the hair model:
    Code :
    int pointIndex = 0;
    for ( int hairIndex=0; hairIndex < hairCount; hairIndex++ ) 
    {
    glDrawArrays( GL_LINE_STRIP, pointIndex, segmentCount+1 );
    pointIndex += segmentCount+1;
    }
    gl_VertexID will restart from zero at each call of glDrawArrays. As I saved the positions of all the hair vertices in a single array, when rendering a vertex I want to get the ID of that vertex. I used glVertexAttribPointer() like this:
    Code :
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 
    (const GLvoid *)0);
    to make gl_VertexID increase every time a vertex is rendered(three numbers are extracted).

    The GLSL specification description of gl_VertexID says it was "explicitly generated from the content of the GL_ELEMENT_ARRAY_BUFFER by commands such as glDrawElements". So I constructed an element index array, wishing gl_VertexID would not restart at each call of glDrawElements.

Similar Threads

  1. gl_VertexID and *BaseVertex
    By grisha in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 07-02-2014, 11:37 AM
  2. glDrawArraysIndirect and gl_VertexID
    By soconnell in forum OpenGL: Drivers
    Replies: 0
    Last Post: 07-26-2013, 07:21 AM
  3. Replies: 13
    Last Post: 05-03-2013, 07:50 AM
  4. gl_VertexID in GLSL 120 on OSX?
    By imported_JoshKlint in forum OpenGL: GLSL
    Replies: 4
    Last Post: 11-17-2011, 06:56 PM
  5. gl_VertexID issue
    By yagero in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 01-17-2011, 01:09 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