[GLSL] Fragment Coordinate (gl_FragCoord)

Shinerd
2 min readMar 8, 2020

--

In the same way GLSL gives us a default output, vec4 gl_FragColor, it also gives us a default input,vec4 gl_FragCoord, which holds the screen coordinates of the pixel or screen fragment that the active thread is working on. With vec4 gl_FragCoord, we know where a thread is working inside the billboard. In this case we don’t call it uniformbecause it will be different from thread to thread, instead gl_FragCoordis called a varying.

#ifdef GL_ES
precision mediump float;
#endif
uniform vec2 u_resolution;void main() {
vec2 st = gl_FragCoord.xy/u_resolution;
gl_FragColor = vec4(st.x,st.y,0.0,1.0);
}

In the above code st is a variable that get vec2 value of the position of pixels that are represented between 0 and 1. We give the same codes(instructions) to each pixel, but gl_FragCoord make each of pixels ultimately have different color with others.

Can you tell where the coordinate (0.0, 0.0)is in our canvas?

- the left side below.

(https://thebookofshaders.com/03/)

It is the end of the basic structure of the shader. Now, let’s write the shader code in earnest.

--

--