In glsl, what is the formula used to calculate gl_fragCoord from gl_position?

Please correct me if I am wrong. When using vertex and pixel shaders, we usually provide code to calculate the output gl_position of the vertex shader. Then we find the elements with gl_FragCoord input in the pixel shader. What is the name of the operations performed by OpenGL to compute gl_FragCoord from gl_position? Is it right that this is a “projection” and a “transformation of clip coordinates”? Then, what kind of conversions are performed during these operations? In other words, what is the mathematical relationship between gl_FragCoord and gl_position that I could use to replace the openGL function?

Thanks so much for any input.

+6
source share
1 answer

gl_Position is in uniform post projection coordinates.

It is worth noting that gl_Position is the (4d) position of the vertex, and gl_FragCoord is the (2d) position of the fragment.

The operations that occur between them are

  • primitive assembly (for creating a triangle of three vertices, for example)
  • clipping (i.e. cut a triangle in several triangles that are inside the view if it does not initially fit)
  • application to view
  • rasterization (take these triangles and generate covered fragments)

So, although you can find a formula for transforming an arbitrary point represented from the position of a vertex in 2d, which is a viewport, it is not useful in itself, since the generated fragments do not directly align the position of the vertices. formula for obtaining the 2d coordinate of the vertex

2d_coord_vertex = viewport.xy + viewport.wh * (1 + gl_Position.xy / gl_Position.w)/2 

Again, this is not gl_FragCoord. Check the rasterization information in the GL specification if you want to get deeper knowledge.

I'm not sure what you mean by "replacing the openGL function", but rasterization is nontrivial and goes beyond the scope of the SO answer.

+9
source

Source: https://habr.com/ru/post/895658/


All Articles