Getting the element index in the shader when using DrawElements

Hello,

i was wondering if there was any way to retrieve the “index of the index” currently processed in the vertex shader. I know about gl_VertexID, but this does not seem to be what i mean, since it is the index of the vertex in the vertex buffer. But i need a way to treat ranges in the index buffer differently than others.

What i am trying to do is to transform some indices differently than others, in a fixed pattern ( (A, B, B), (B, A, A) ). The first index for the first triangle is supposed to use Matrix A, the last 2 indices of the first triangle should use Matrix B and so on.

Is there a way to do this in a vertex Shader?

Thanks

No. Repeated indices correspond to multiple references to a single vertex, not to multiple vertices. If an index is repeated, it’s entirely possible (and very likely) that the vertex shader will only be called once for that index.

The closest you can get is to put the attribute data in uniform arrays (or textures), make the index buffer an integer attribute, and use glDrawArrays(). The vertex shader would then use the integer attribute to retrieve the other data from the arrays/textures.

This is likely to be slower than using glDrawElements() because the memory accesses are performed as random-access fetches within the shader rather than being pre-fetched by the hardware.

If there is a pattern to your indices, then yes, you could use gl_VertexID and use the modulo operator, if it’s every third vertex, etc.

Jeff