My OpenGL program, using GLSL for shaders, has a simple vertex and fragment shader (given by a tutorial).
The vertex shader is:
#version 330
layout (location = 0) in vec3 Position;
void main()
{
gl_Position = vec4(0.5 * Position.x, 0.5 * Position.y, Position.z, 1.0);
}
And the fragment shader is:
#version 330
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
What is happening here is that the vertex shader divides the vertex coordinates by 2, and the fragment shader then colors it in red.
Now from my understanding, gl_Position
tells the fragment shader the pixel coordinates of this vector. And gl_Position
is an existing variable that both shaders know about, so the fragment shader will always look for gl_Position
when deciding where to draw that vertex.
However, what about FragColor
? In this example, it has been manually defined in the fragment shader. So how does OpenGL then know that FragColor
is the variable we are using to set the vertex's color? FragColor
can have been defined with a different name and the program still runs in the same way.
So I am wondering why gl_Position
is a variable that has already been defined by OpenGL, whereas FragColor
is manually defined, and how OpenGL knows how to interpret FragColor
?