Variables
Task
Declare four float variables for the red, green, blue, and alpha channels. Assign them the following values:
- red =
0.9 - green =
0.3 - blue =
0.6 - alpha =
1.0
Then use these variables directly inside a vec4 / float4 to set the output color.
Theory
A variable is like a box with a name. You can put something inside - like a number or a color - and use it later in your shader program. It helps organize the shader’s logic and makes it easier to reuse values efficiently.
Variable Type
Every variable has a type, which tells the computer what kind of thing is inside the box. Here are some common primitive types in shaders:
| Type | What it holds | Example value |
|---|---|---|
float |
single decimal number | 0.5, 1.0, 3.14 |
int |
whole number | 1, 42, -5 |
bool |
true or false | true, false |
If you try to use an int like a float, the shader will get confused - like trying to use a whole number where a decimal is needed. That’s why types are important: they tell the shader what kind of data it’s working with, so it can use it the right way. Using the wrong type can lead to errors or unexpected results.
Variable Declaration
To declare a variable in a shader, you specify its type, then give it a name, and optionally assign it a value:
float brightness = 0.8;
int numberOfLights = 4;
bool isVisible = true;