开发者

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

开发者 https://www.devze.com 2023-03-29 20:16 出处:网络
Please correct me if I\'m wrong. When using vertex and pixel shaders, we usually provide the code to compute the output gl_position of the vertex shader.

Please correct me if I'm wrong. When using vertex and pixel shaders, we usually provide the code to compute the output gl_position of the vertex shader. Then, we find ouselves with the input gl_FragCoord in the pixel shader. What are the name of the operations performed by OpenGL to compute gl_FragCoord from gl_position ? Is it correct that those are "projection" and "clip coordinates transform" ? Then, what exactly are the transformations performs during those operations ? In other terms, what is the mathematical relation between gl_FragCoord and gl_position, that I could use to replace the openGL开发者_JS百科 function ?

Thank you very much for any contribution.


gl_Position is in post-projection homogeneous coordinates.

It's worth noting that gl_Position is the (4d) position of a vertex, while gl_FragCoord is the (2d) position of a fragment.

The operations that happen in between are

  1. primitive assembly (to generate a triangle from 3 vertices, e.g.)
  2. clipping (i.e. cut the triangle in multiple triangles that are all on inside the view, if it does not initially fit)
  3. viewport application
  4. rasterization (take those triangles and generate covered fragments from them)

So, while you can find the formula to transform the arbitrary point that is represented from the vertex position in the 2d space that is the viewport, it's not in and of itself that useful, as the generated fragments do not align directly to the vertex position. the formula to get the 2d coordinate of the vertex is

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

Again, this is not gl_FragCoord. Check the details on rasterization in the GL specification if you want more in-depth knowledge.

I'm not sure exactly what you mean by "replace the openGL function", but rasterizing is non-trivial, and way beyond the scope of an SO answer.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号