Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 function ?

Thank you very much for any contribution.

like image 535
wip Avatar asked Aug 23 '11 07:08

wip


People also ask

What is gl_Position in GLSL?

Vertex shaders manipulate coordinates in a 3D space and are called once per vertex. The purpose of the vertex shader is to set up the gl_Position variable — this is a special, global, and built-in GLSL variable. gl_Position is used to store the position of the current vertex.

What is gl_FragCoord?

Available only in the fragment language, gl_FragCoord is an input variable that contains the window relative coordinate (x, y, z, 1/w) values for the fragment. If multi-sampling, this value can be for any location within the pixel, or one of the fragment samples.

What is a fragment in GLSL?

A fragment shader processes… fragments. A fragment is basically a position on the window (X,Y), a depth value (Z), plus all the interpolated data from previous stages.

What is varying in GLSL?

varying variables contain data shared from a vertex shader to a fragment shader. The variable must be written in the vertex shader and the read-only value in the fragment shader is then interpolated from the vertices which make up the fragment.


1 Answers

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.

like image 131
Bahbar Avatar answered Sep 30 '22 22:09

Bahbar