Part of the Khronos Group

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: when is the indices of vertices determined to be used in gl_VertexID

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Junior Member Newbie
    Join Date
    Jan 2013

    when is the indices of vertices determined to be used in gl_VertexID

    I am trying to understand the behavior of gl_vertexID in vertex shaders. For that I am trying to render 2 squares using two glDrawArrays calls one after another. And want to apply red color to only one square using gl_VertexID in vertex as :

    Code :
    #version 300 es
    precision highp float;
    out vec4 color;
    in vec4 tdk_Vertex;
    void main(void)
    if(gl_VertexID < 4)
        color = vec4(1.0f, 0.0f, 0.0f, 1.0f);
        color = vec4(1.0f, 1.0f, 1.0f, 1.0f);
    gl_Position = tdk_Vertex;

    Passing color to fragment shaders.

    Square coordinates as :

    Code :
    static GLfloat vertices[] =
           { -0.75f, 0.25f, 0.0f, 1.0f,
             -0.75f, 0.5f,  0.0,  1.0f,
             -0.25f, 0.5f,  0.0f, 1.0f,
             -0.25f, 0.25f, 0.0f, 1.0f,
              0.25f, 0.25f, 0.0f, 1.0f,
              0.25f, 0.5f,  0.0f, 1.0f,
              0.75f, 0.5f, 0.0f,  1.0f,
              0.75f, 0.25f, 0.0f, 1.0f};

    Making draw calls as :

    Code :
    for(int i=0; i<8; i+=4)
         glDrawArrays(GL_TRIANGLE_FAN, i, 4);

    Using Nvidia card, and calling two glDrawArrays calls is displaying the expected result i.e rendering red color to one square and white to other.

    Thus, want to know is this correct behaviour or gl_VertexID indices should generated during glDrawArrays call so that both squares have same red color?

    I am using 2 glDrawArrays calls , so my understanding is that both squares should be red according to specification.
    Want to test it for glsl 300 es.

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Dec 2007
    Not 100% sure about how it is defined in GLES, but in case of desktop OpenGL it is expected that gl_VertexID has the first parameter of glDrawArrays calls included in it. That means:

    glDrawArrays(GL_TRIANGLE_FAN, 0, 4) -> gl_VertexID = 0, 1, 2, 3
    glDrawArrays(GL_TRIANGLE_FAN, 4, 4) -> gl_VertexID = 4, 5, 6, 7

    The spec says the following:

    Quote Originally Posted by GL 4.3 section
    gl_VertexID holds the integer index i implicitly passed by DrawArrays or one of the other drawing commands defined in section 10.5.
    Quote Originally Posted by GL 4.3 section 10.5
    Elements first through first + count - 1 of each enabled non-instanced array are transferred to the GL.
    Practically gl_VertexID is the element ID, which means for glDrawArrays it is in the range [first, first+count-1].
    Disclaimer: This is my personal profile. Whatever I write here is my personal opinion and none of my statements or speculations are anyhow related to my employer and as such should not be treated as accurate or valid and in no case should those be considered to represent the opinions of my employer.
    Technical Blog:

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts