Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Getting the element index in the shader when using DrawElements

  1. #1
    Intern Newbie
    Join Date
    Oct 2016
    Posts
    46

    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

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,402
    Quote Originally Posted by stimulate View Post
    Is there a way to do this in a vertex Shader?
    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.

  3. #3
    Intern Contributor
    Join Date
    Dec 2016
    Posts
    53
    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

Posting Permissions

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