5
$\begingroup$

I'm new to shaders and know that you can color pixels with gl_FragColor but sometimes there is this thing:

vec2 uv = gl_FragCoord.xy / screenSize; // or resolution or smthn depends on the implementation

If gl_FragCoord is like pixel coordinates, what does uv get?

Why is it often done in GLSL?

If someone could even draw an example of which part of the screen will be UV it will be very helpful!

$\endgroup$
  • $\begingroup$ It is important to note that the Rasterization stage before the optional Fragment Shader performs fragment attribute interpolation and viewport transformation for the gl_FragCoord attribute. $\endgroup$ – Matthias Oct 14 '17 at 16:29
16
$\begingroup$

First, gl_FragCoord.xy are screen space coordinates of current pixel based on viewport size. So if viewport size is width=5, height=4 then each fragment contains:

enter image description here

Why are uvs needed? For example I rendered geometry to screen quad and then I need to apply some postprocessing on this quad in another rendering pass. To sample from that quad I need texture coordinates in range [0, 1]. So to calculate them I perform division gl_FragCoord.xy / viewPortSize.

$\endgroup$
  • $\begingroup$ It might be worth saying that GL_TEXTURE_RECTANGLE_ARB doesn't use normalized coordinates, but that GL_TEXTURE_*D textures do. $\endgroup$ – user1118321 Feb 13 at 5:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.