1
votes

Is there any way to tell, from within a gl es vertex shader (that is drawing triangles) which of the three vertices is being processed?

Using gl_VertexID doesn't work for me, because it gives the index of the vertex in the list of vertices, but I use indices to specify a different order to draw the vertices, and so the value I want cannot be determined from gl_VertexID alone.

2
Another way to put it: I am looking for the value of v%3, where v is the index of the index-buffer value that has specified the vertex currently being drawn.weemattisnot
I think I have found that what I am looking for is deliberately not available for pipeline efficiency reasons. opengl.org/discussion_boards/showthread.php/…weemattisnot
You may be able to add a vertex attribute which will define this value. Still you might have issues if the vertices are reused (using indices) as the same vertex may need to have a different value depending on what triangle is being drawn. Even so you might be able to distribute these values across the indices for the most parts of the shape (maybe even whole).Matic Oblak
@matik-obllak is correct. If the mesh was "three colorable" then you could uniquely assign 1, 2, 3 to each vertex, but not even a tetrahedron is three-colorable. Alas, you can't share vertices and have unique indices.wcochran

2 Answers

1
votes

You can add a vertex attribute to represent the indices 0, 1, 2, but as @matic-oblak noted you may have to replicate some vertices that are shared between triangles. If the mesh is "three-colorable" (in the graph theory sense) then you can assign indices without any replication.

A tetrahedron is not 3-colorable, whereas a cube is 2-colorable, and we can triangulate the faces of a cube and get a 3-colorable mesh. Ordinary vertices have degree 6 in a triangular mesh and are "locally" 3-colorable.

enter image description here

Therefore you can 3-color a mesh as much as possible -- where it fails you will have to replicate vertices. Unfortunately 3-coloring is an NP-complete problem , but with a some simple heuristics I think you can do a fairly reasonable job.

0
votes

As I commented above, what I was looking for is deliberately not available for pipeline efficiency reasons. See the comment by Alfonse Reinheart at the following page:

https://www.opengl.org/discussion_boards/showthread.php/181822-gl_VertexId-gl_InstanceID-gl_PrimitiveID-but-where-is-gl_IndexID

The other answer, posted by wcochran is interesting, and could be a way to pass less information to the rendering pipeline, although as s/he points out, it comes at the cost of some substantial preprocessing.