9

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.

wip
  • 2,313
  • 5
  • 31
  • 47
  • I have had the same problem and question a while ago, and have answered it myself in the question "OpenGL: Compute eye space coord from window space coord in GLSL?", which you can find a link to to the right. – Razzupaltuff Aug 23 '11 at 11:19
  • Sorry karx11erx, this is not what I want. Actually, I think what I want is the opposite. I do not want to "unproject", I want to project my 3d coordinates to the 2d screen coordinates. – wip Sep 04 '11 at 05:46
  • You can see how to do that there, too, because the shader first unprojects (from light view space) and then projects (to viewer screen space). – Razzupaltuff Sep 05 '11 at 16:15

1 Answers1

11

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.

Bahbar
  • 17,760
  • 43
  • 62
  • Thank you for your answer. My purpose is to implement a shadow map lighting shader. I created a depth map (shadow map) by rendering the scene from the light position (using the light's "projection matrix"), however when rendering the final scene's vertices I do not know which coordinates to use to fetch the shadow data from the map. I thought I could use the light's "projection matrix" to get the value that was gl_position when I created the shadow map, and then compute the value that was gl_fragCoord when I created the shadow map, to fetch the shadow data at the correct position. Can it work? – wip Aug 23 '11 at 09:29
  • @wil: the typical way to do that is to pass a world or view space position along with gl_Position (in a different output) to the fragment shader. That way, in the fragment shader, the correct interpolated position (in view or world space, depending on what you passed), can be transformed by a matrix to bring it back to light space. Short answer is, don't rely on gl_FragCoord, pass what you need. – Bahbar Aug 23 '11 at 19:03
  • I'm not sure exactly what you're after, but it does not sound like computing a shadow map. That's what I was referring to, and sounds significantly different to your own thing. I'd recommend to open a new question, with some insight as to _what_ you're trying to solve – Bahbar Sep 16 '12 at 20:09