用於渲染彩色矩形的著色器

OpenGL 意義上的著色器程式包含許多不同的著色器。任何著色器程式必須至少有一個頂點著色器,用於計算螢幕上各點的位置,以及一個片段著色器,用於計算每個畫素的顏色。 (實際上故事更長,更復雜,但無論如何……)

以下著色器適用於 #version 110,但應說明以下幾點:

頂點著色器:

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

片段著色器:

#version 110

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

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