OpenGL Game Development Tutorials

OpenGL Book: A OpenGL Tutorial Reference Book
Easily understand OpenGL and learn how to create 3D worlds and games from scratch.

OpenGL Gems is the authorative guide on OpenGL programming published by Learning Curve / Education for software developers! This book in 3D C++ programming series clearly, and in easy to follow language, explains fundamental principles used behind programming 3D games using the OpenGL cross-platform framework.

Preorder Now in PDF format for Kindle and get a discount.
OpenGL Gems is coming out on April 15th, 2017.
The paperback version will be available via this Amazon page, please bookmark it!

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 (simple.gs)

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

Make a Donation

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 Book
OpenGL Book - A 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.

Preorder Here
© 2017 Copyright OpenGL Tutorials (www.falloutsoftware.com)

All content and graphics on this website are the property of www.falloutsoftware.com - please provide a back link when referencing on other sites.