opengl Shaders Shader for rendering a coloured rectangle

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

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);
}


Got any opengl Question?