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

• 02-13-2013, 12:56 AM
harsha
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); } else { color = vec4(1.0f, 1.0f, 1.0f, 1.0f); } gl_Position = tdk_Vertex; }```

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.
Thanks.
• 02-13-2013, 07:55 AM
aqnuep
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 11.1.3.9
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].