<p>Refer to narthex's excellent answer for details and example, but simply put,</p>

<p>If you want to <em>map</em> the pixel coordinate values to the range <strong>0</strong> to <strong>1</strong> , you divide by viewPortSize.</p>

CC BY-SA 4.0
2019-12-08T08:20:21.087Z
2019-12-08T08:20:21.087Z
WizardOfRobots
<p>First, <code>gl_FragCoord.xy</code> are screen space coordinates of current pixel based on viewport size. So if viewport size is <code>width=5</code>, <code>height=4</code> then each fragment contains:</p>
<p><a href="https://i.sstatic.net/Oa1zn.png" rel="noreferrer"><img src="https://i.sstatic.net/Oa1zn.png" alt="enter image description here" /></a></p>
<p>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 <code>gl_FragCoord.xy / viewPortSize</code>.</p>

CC BY-SA 4.0
2019-02-13T05:42:28.293Z
It might be worth saying that `GL_TEXTURE_RECTANGLE_ARB` doesn't use normalized coordinates, but that `GL_TEXTURE_*D` textures do.
user1118321
CC BY-SA 3.0
2017-10-14T12:42:15.177Z
2017-10-14T12:42:15.177Z
2020-06-18T08:32:12.800Z
mdkdy
<p>I'm new to shaders and know that you can color pixels with gl_FragColor but sometimes there is this thing:</p>

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

<p>If gl_FragCoord is like pixel coordinates, what does <strong>uv</strong> get?</p>

<p>Why is it often done in GLSL?</p>

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

CC BY-SA 3.0
2017-10-14T16:29:17.517Z
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.
Matthias
<opengl><shader><glsl>
GLSL. Can someone explain why gl_FragCoord.xy / screenSize is performed and for what reason?
idchlife
# GLSL. Can someone explain why gl_FragCoord.xy / screenSize is performed and for what reason?

(Asked by: username_0 on 2017-10-14)

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!

**Tags:** opengl, shader, glsl
> **Comments:**
> **username_1**: 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.  

---

## 2 answers

### Answer by username_3 on 2017-10-14. Score: 35 | Accepted answer

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](https://i.sstatic.net/Oa1zn.png)](https://i.sstatic.net/Oa1zn.png)

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`.

> **Comments:**
> **username_4**: It might be worth saying that `GL_TEXTURE_RECTANGLE_ARB` doesn't use normalized coordinates, but that `GL_TEXTURE_*D` textures do.  

---

### Answer by username_2 on 2019-12-08. Score: 6

Refer to narthex's excellent answer for details and example, but simply put,

If you want to *map* the pixel coordinate values to the range **0** to **1** , you divide by viewPortSize.


---

