1
\$\begingroup\$

I am struggling a bit to understand the values that gl_VertexID primitive contains when the vertex shader is executed.

I have the standard modern rendering pipeline, in which after setting up shaders, buffers, etc I call the code below to render a mesh

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, auxMesh->indicesBuffer);
    glDrawElements(GL_TRIANGLES, auxMesh->numIndices, GL_UNSIGNED_INT, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

In the vertex shader I want to manually access to the current vertex being rendered retrieving it from the original buffer (I know this sounds nonsense, but I need to do this for a reason), therefore I pass the vertex buffer as the texture buffer u_tbo_tex and I access to the actual coordinates as follows:

vec3 vertex_1 = texelFetch(u_tbo_tex, gl_VertexID + 0).xyz;
vec3 vertex_2 = texelFetch(u_tbo_tex, gl_VertexID + 1).xyz; 
vec3 vertex_3 = texelFetch(u_tbo_tex, gl_VertexID + 2).xyz;

And the values of these vertex vertex_1, vertex_2, vertex_3 make perfect sense.

However, I can't really understand what values gl_VertexID got in each iteration. Is gl_VertexID sequentially assigned following the range of [0...auxMesh->numIndices]. Or are the values increased +3 at every iteration because I am drawing triangles?

I need to understand this because I am interested in calling textureFetch(u_tbo_tex, i) where i is a arbitrary triangle in my mesh (or whatever i needs to be to access an arbitrary triangle), but I can't find the right way to access to it.

\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

gl_VertexID is the literal index value of the vertex that is being processed.

In the case of non-indexed geometry an implicit increasing index were assigned to each vertex being processed.

The Vertex Shader runs on individual vertices without knowledge of adjacency or the kind of primitive being processed. If you're using indexed geometry, a shared vertex will likely be processed fewer times than it's used.

As a tangent, Geometry Shaders have, given the right primitive type, some knowledge of adjacency but probably not enough to solve the problem at hand.

If you wish to identify the triangle you're in, consider using unindexed geometry as the mapping to triangle ID and vertex ID is a matter of division and modulo by 3.

Edit: removed term "vertex processing" which has wider meaning and mentioned GS

\$\endgroup\$
1
  • 1
    \$\begingroup\$ +1: However, it is not "as if", that is literally what happens. Each drawn vertex is assigned an incremental gl_VertexID (assuming it is not a strip/fan, because they do funny things to the vertex stream by referencing previous vertices). I do have a minor fuss regarding the last thing you wrote though: vertex processing actually includes primitive assembly (Geometry Shaders), which does have knowledge of adjacent vertices given the proper primitive type. This answer works best if you ignore Geometry Shaders or restrict "vertex processing" to vertex shading ;) \$\endgroup\$ May 2, 2014 at 1:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .