Fragment Shader
Task
Let's play with numbers and make the screen turn blue!
Your mission: change the zeros to ones (one at a time) and see what happens to the screen. Try different combinations like:
- $(1.0, 0.0, 0.0, 1.0)$
- $(0.0, 1.0, 0.0, 1.0)$
- $(0.0, 0.0, 1.0, 1.0)$
Theory
Fragment shaders are written in specialized languages like GLSL (OpenGL Shading Language) or HLSL (High-Level Shading Language, used with DirectX). These shaders run directly on the GPU (Graphics Processing Unit), allowing for highly parallel and efficient pixel-level computations.
GLSL
void main() {
gl_FragColor = COLOR;
}
🧠 main function
It is the place where all the action starts. When your shader runs, it jumps into the main() function and follows the instructions inside. It tells each pixel what to do.
🎨 gl_FragColor
Now imagine your screen is made of millions of tiny dots called pixels. Each pixel is waiting for someone to tell it what color to be.
That's where gl_FragColor comes in. It is like a paintbrush that sets the color of each pixel. You give it a color, and it paints that pixel with it.
HLSL
float4 main() : SV_Target {
return COLOR;
}
🧠 main function
It is the place where everything begins. When your shader runs, it jumps into the main() function and follows the instructions inside. It tells each pixel what to do.
🎨 return
Inside main(), we use return to send a color back to each pixel. This tells the pixel exactly what color to show on the screen.
📌 SV_Target
The tag SV_Target is a special tag that tells computer that this is the final color we want to show on the screen.