What is a Fragment Shader in OpenGL?
The fragment shader is the last stop in the OpenGL Pipeline. It is where a pixel gets assigned a color.
The basic operation of a fragment shader is to provide a color to each pixel. More specifically, the fragment shader takes the Uniform data and the output from the rasterizer and computes the color of the pixel for each fragment.
Figure 1. Fragment Shader Stage
Inputs to a Fragment Shader
The fragment shader takes its input from either the Vertex, Tessellation or Geometry shader. The Fragment Shader takes this interpolated data, along with Uniform data and computes a color to each pixel before they are sent to the framebuffer.
The fragment shader can receive:
- Varying/Out variable
- Uniform data
- Texture data
Outputs of a Fragment Shader
The fragment shader requires a vec4 variable as its output. This output declares the color that should be assigned to each pixel.
If you are using OpenGL 3.0 and beyond, you have to declare this variable yourself as:
out vec4 fFragment color;
If you are using pre-OpenGL 3.0, you must use a default variable called gl_FragColor.
For example:
gl_FragColor=vec4(1.0,0.0,0.0,1.0); //red color