0

I'm drawing set of polygons using glMultiDrawArray command. I want to color each polygon with a different color. If I can access the primitive index (or the polygon) a vertex belongs to inside vertex shader, I can look color up from an array and set the color.

So my question is: Is it possible to access primitive index within a (vertex) shader?

What are the other alternatives to color each polygon using a different color?

warunapww
  • 966
  • 4
  • 18
  • 38
  • 1
    ...[`gl_VertexID`](https://www.khronos.org/opengl/wiki/Built-in_Variable_(GLSL)#Vertex_shader_inputs)? – genpfault Sep 13 '18 at 15:45
  • @genpfault thanks for the link. I think if gl_VertexID returns the absolute index of the vertex, then I can construct the index of the polygon. But if it is the relative index within the polygon, then I do not have enough information to figure out the index of the polygon. In the same link, I saw a command called "gl_DrawID", I believe it returns the index of the polygon. If that is the case, this is the best solution. I need to try above solutions first to make sure that my understanding is correct. – warunapww Sep 13 '18 at 16:03
  • 1
    Worst-case you can add another vertex attribute and populated it with whatever ID you want CPU-side and reference that. – genpfault Sep 13 '18 at 16:10

1 Answers1

2

Vertex shaders operate on vertices, not primitives (hence the name ;) ). As such, they have no access to any per-primitive data. Now, if each vertex is associated with exactly one primitive, then vertex operations are effectively per-primitive. But this will generally require replicating lots of per-vertex information, since most meshes will share vertices across multiple primitives.

Geometry shaders operate per-primitive, so it is entirely possible for them to be used for this. However, the best way to perform per-primitive operations will depend on exactly when and how you intend to do this.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982