Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain the vertices due to which a fragment shader is called in OpenGL ES?

Tags:

opengl-es

In OpenGL ES how can I obtain the vertices due to which a fragment shader is called from the fragment shader?

The fragment shader it appears has no link to it's correponding vertices?

like image 751
seahorse Avatar asked Jan 18 '23 14:01

seahorse


2 Answers

There is no direct way to access vertices in your fragment shader and a vertex shader only deals with 1 vertex at a time and has no concept of the whole primitives. The fragment shader only deals with fragments.

You can output information about a specific vertex and have it interpolated to your fragment shader like how you can blend colors across a triangle, or do texture coordinates. Often are you will need to do that blending in the fragment shader anyway so you might as well have it done automatically in the vertex shader. You can get the fragments 3d space coordinates (but only the fragments, not the surrounding vertices).

With that said you can attach any arbitrary information to your shaders. For example in addition to position, normals and texcoords you could also have an extra attribute that has the information about each vertex that you want to be accessible in a specific vertex. If could be 3 sets of x,y,z cordinates if that is the information you want and a number indicating which vertex number in the primitive you are dealing (ie the first 2nd or 3rd vertices in a triangle) and/or the sequence number in the whole mesh. Or it could be an just be an index number if that is what you want. The extra attributes would take more memory though as you would have to duplicate the vertex data for each vertex in the primitive to ensure that the fragment shader has the same data from all 3 of its vertices.

For example the following attributes for each vertex (for triangle primitives):

v1, n1, uv1, v1, v2, v3, 0
v2, n2, uv2, v1, v2, v3, 1
v3, n3, uv3, v1, v2, v3, 2
v4, n4, uv4, v4, v5, v6, 0
v5, n5, uv5, v4, v5, v6, 1
v6, n6, uv6, v4, v5, v6, 2

If you need normals or texture coords you would need to duplicate that too.

Also consider what are you actually doing with the vertex information in the fragment shader. Rather than sending the data for all the vertices, could you precalculate the answer you where going to calculate in the fragment shader, and instead attach the answer as an attribute and interpolate that across from the vertex shader to the fragment shader.

Another approach would be to dump all the vertex data in a uniform and look it up. You would probably still need some kind of index information attached as vertex attributes though so it knows what index it is supposed to look up. Also remember that lookup will be slow since it's happening every single pixel and you are likely to be getting collisions since each vertex will be looking at the information about the ones surrounding it.

In newer versions of OpenGL (3.x+) geometry and tessellation shaders have access to this kind of information and can output it to the next thing in the shader pipeline.

With that said it would help to know exactly what you are trying to accomplish as it give an idea of what vertex data needs to be passed to the fragment shader. There also may be a much simpler way of doing what you want without having access to that data in the fragment shader.

like image 130
David C. Bishop Avatar answered Jan 31 '23 23:01

David C. Bishop


A fragment shader does indeed have no link to the vertex shader, and the vertex shader is per-vertex with no concept of the total set that created that piece of geometry. So you can't obtain the vertices that gave rise, eventually, to that instance of the fragment shader.

Is there a specific effect you're trying to achieve? Whatever it is you want to calculate from the vertices is usually achievable by loading appropriate varyings — e.g. if you load the 3d location of each vertex to a suitable varying then each fragment will inherently know its location.

like image 21
Tommy Avatar answered Jan 31 '23 23:01

Tommy