From Ohm

OpenGL GLSL Shader Language

GLSL, the OpenGL Shader Language, is a high-level programming language for shaders as a replacement for the low-level shader program extensions. It was first introduced with the OpenGL 2.1 standard.

The GLSL shading language is much inspired by the syntax of the C/C++ programming language. Just like that, the OpenGL driver will compile a GLSL shader program into machine code as defined by the OpenGL shader program extensions “ARB_vertex_program” and “ARB_fragment_program” and execute those programs on the GPU.

The simplest GLSL shader program consists of a vertex shader, which transforms the vertices as given in the shader input register gl_Vertex, with the combined model-view-projection matrix as given by the uniform matrix gl_ModelViewProjectionMatrix and a fragment shader, which writes a constant color into its output register gl_FragColor:

Simplest vertex shader:

#version 120
void main()
{
   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

Simplest fragment shader:

#version 120
uniform vec4 color;
void main()
{
   gl_FragColor = color;
}

Each GLSL program must contain a main function, which is executed for each incoming vertex resp. fragment. Variable types can be float, vec2, vec3, vec4, mat2, mat3, mat4.

A parameter, which is constant for the run-time of a shader is called a uniform register. It can be written on the CPU side and read on the GPU side.

A so called varying register is a data channel between the vertex and the fragment shader stage. By writing values into such a varying register on the vertex shader side, the values will be interpolated during the rasterization stage of the graphics pipeline and emerge in the corresponding input register in the fragment shader.

As an example, the per-fragment interpolation of color attributes is achieved by introducing a varying register “frag_color” in both the vertex and the fragment shader:

Vertex shader example:

#version 120
varying vec4 frag_color;
void main()
{
   frag_color = gl_Color;
   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

Fragment shader example:

#version 120
varying vec4 frag_color;
void main()
{
   gl_FragColor = frag_color;
}

Retrieved from http://schorsch.efi.fh-nuernberg.de/roettger/index.php/MedicalVisualization/OpenGLGLSLShader

Page last modified on November 22, 2016, at 11:17 AM