Lesson 2 of 10 · GLSL shaders for beginners
The three numbers
Red, green, blue. That is how you make any color.
In “Hello, light” a single color lit up the whole screen. Here you see what it is made of. Any color you see on a screen is made of three numbers: how much red, how much green, how much blue. R, G, B.
If all three are at maximum (1, 1, 1), you get white. If all three are off (0, 0, 0), you get black. If only red is on (1, 0, 0), you get pure red. Above 1 the screen gives no more light and the channel stays at 1. That is why the sliders go up to 1.6 but the color stops changing at 1.
Three numbers between 0 and 1. The entire visible spectrum.
Notice something odd: we write 1. with a dot, not 1. That is because the GPU treats whole numbers and decimals differently. Here we always use decimals. The dot is mandatory.
float r = 1.;: float holds one number with decimals. Here, how much red.float g and float b: the same for green and blue.vec4(r, g, b, 1.): puts the three together as the frag’s color, with opacity 1.You know these:
gl_FragColor (01 · Hello, light).Next: in “Mixing” you do math with these numbers.
r = 1. to r = 0. and red is gone, you get cyan. Change b = 1. to b = 0. and you get yellow (red + green). Change g = 1. to g = 0. for purple. Change all three 1. to 0. and you get black.TIME
Replace the linegl_FragColor = vec4(r, g, b, 1.); with this:gl_FragColor = vec4(mix(vec3(.95, .25, .35), vec3(.95, .92, .25), .5 + .5 * sin(T)), 1.);
Watermelon pink and lemon yellow mixing. mix() is a slider between two colors. T is the clock. You meet it properly in “The clock”, lesson 07. For now: your sandbox is breathing.
The code
void main(){
float r = 1.;
float g = 1.;
float b = 1.;
gl_FragColor = vec4(r, g, b, 1.);
}