OpenGL Shaders

Shaders are separately-compiled programs written aside from your main OpenGL program. Your main OpenGL program compiles and uploads them to the GPU during the initialization process. Once on the GPU they will execute there to achieve maximum performance by utilizing your graphics card hardware.

Common OpenGL Shaders

The three most common shader types are geometry shader, vertex shader and fragment shader. Together they form part of OpenGL 3D rendering pipeline. Shaders receive information (usually arrays of vertex, texture or color data), process that information and produce an output. This output value is then passed on to the next shader in the pipeline.

It is not uncommon to skip the geometry shader so that your shader chain consists only of vertex and fragment shaders. Many pipelines remove the geometry shader completely or replace it by application-side code that imitate its functionality.

For example OpenGL ES (and WebGL) do not have a geometry shader at all. It’s often imitated in software, and in some circumstances isn’t required at all.

Simple Shader Examples

Below is the source code that serves as a starting point for each shader in particular. Each example provides absolute minimum code that barely does anything. While not required, it’d be nice to know a thing or two about 3D matrices before starting to learn OpenGL shaders, because some shaders require them to produce an output.

Matrices must be precalculated, usually using an existing pre-written C, C++, C++11 or C# matrix library, before sending them over to the shader as an array buffer. In this tutorial we will only focus on the shader example source code.

You can learn about matrices elsewhere on this site.

For now, let’s take a look at these basic GLSL shader examples.

Example of GLSL Geometry Shader (

Unlike vertex and fragment shaders that operate on one vertex (or fragment) at a time geometry shader operates on an entire object.

    #version 330 core    layout (points) in;    layout (line_strip, max_vertices = 2) out;    void main() {        gl_Position = gl_in[0].gl_Position + vec4(-0.1, 0.0, 0.0, 0.0);        EmitVertex();        gl_Position = gl_in[0].gl_Position + vec4(0.1, 0.0, 0.0, 0.0);        EmitVertex();        EndPrimitive();    }

Example of GLSL Vertex Shader (simple.vs)

The vertex shader example below demonstrates the minimalistic shader setup possible. Vertex shaders, as the name suggests operate on vertex data of the object that was processed earlier by the geometry shader.

It simply takes an X and Y coordinate, leaving Z = 0. You could easily pass a vec3 structure including all 3 coordinates: X, Y and Z. But this simplistic shader was tailored for making 2D graphics, where Z axis is normally ignored.

    #version 330 core    layout (location = 0) in vec2 position;    void main()    {        gl_Position = vec4(position.x, position.y, 0.0, 1.0);    }

Example of GLSL Fragment Shader (simple.frag)

Finally, the fragment shader works on one pixel (fragment) at a time. It simply outputs a color. In this case the result is black color where R=0,G=0,B=0.

Color parameters, like many other coordinates in OpenGL range from 0.0f-1.0f where 1.0f describes the highest value. In this case 255 bit per each color channel. This coordinate scaling is not uncommon when dealing with vertices either.

    #version 330 core    out vec4 color;    void main() {        color = vec4(0,0,0,1);    }

Did tutorials on this site help you save a trip to the book store? Perhaps, you can help me write more tutorials and pay for hosting fees.

If you wish to support this site make a donation which often encourages me to write even more free tutorial articles. Patience and hard work go into writing educational material and your support helps run this website. Oh, and also helps me continue serving Google searchers with OpenGL programming tutorials.

Some support levels are qualified for my downloadable OpenGL tutorial book in PDF format (paperback coming soon) for Kindle devices.

OpenGL Book: A OpenGL Tutorial Reference BookOpenGL BookA OpenGL Tutorial Reference Book

If tutorials on this site are not enough, or you simply like reading from a physical book or a digital device (Kindle, iPad, tablets, etc.) check out OpenGL Book. Written by the author of tutorials on this site.

This book is a OpenGL tutorial and a reference that guides the reader through the process of setting up and initializing OpenGL, drawing 3D primitives and creating 3D computer games.