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 :


#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);
}
else
{
    color = vec4(1.0f, 1.0f, 1.0f, 1.0f);
}
gl_Position = tdk_Vertex;
}

Passing color to fragment shaders.

Square coordinates as :


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 :


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.
Thanks.

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:

Practically gl_VertexID is the element ID, which means for glDrawArrays it is in the range [first, first+count-1].

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.