opengl Shaders Shader for rendering a coloured rectangle


Example

A shader program, in the OpenGL sense, contains a number of different shaders. Any shader program must have at least a vertex shader, that calculates the position of the points on the screen, and a fragment shader, that calculates the colour of each pixel. (Actually the story is longer and more complex, but anyway...)

The following shaders are for #version 110, but should illustrate some points:

Vertex shader:

#version 110

// x and y coordinates of one of the corners
attribute vec2 input_Position;

// rgba colour of the corner. If all corners are blue, 
// the rectangle is blue. If not, the colours are 
// interpolated (combined) towards the center of the rectangle    
attribute vec4 input_Colour; 

// The vertex shader gets the colour, and passes it forward     
// towards the fragment shader which is responsible with colours
// Must match corresponding declaration in the fragment shader.  
varying vec4 Colour;    

void main()
{
    // Set the final position of the corner
    gl_Position = vec4(input_Position, 0.0f, 1.0f);
    
    // Pass the colour to the fragment shader
    UV = input_UV;
}

Fragment shader:

#version 110

// Must match declaration in the vertex shader.  
varying vec4 Colour;

void main()
{
    // Set the fragment colour
    gl_FragColor = vec4(Colour);
}