Help end child hunger

GLSL Tutorial – Core


This is the updated version of GLSL tutorial. Only the core version is dealt in here. For compatibility features please go to the original GLSL 1.2 tutorial.

This tutorial does not intend to replace the specs as THE source to fully grasp GLSL and OpenGL, check the documentation at the OpenGL documentation page. The specs are essential, but can be hard as a learning starting point. So consider this tutorial as a gentle introduction to the theme.

As always, your cooperation is a must to get things right. Tutorials always have bugs, mistakes, and things that are not clearly explained. Furthermore, I might get somethings wrong, but please be gentle :-). Your feedback is important.

The tutorial starts with an overview of the pipeline. It describes all its components, and introduces the relevant GLSL bits for each. It then proceeds to the OpenGL side, looking at the setup procedure to have our shaders loaded, compiled, linked, and finally running.

The language in which shaders are written is very similar to C, hence the learning curve shouldn’t be too steep if you’re familiar with it. Some concepts are presented focusing on the parts that are slightly different.

The pipeline requires feeding, and shaders do have to communicate between themselves. These are covered on the next two sections.

Before presenting the shader examples we’ll make one final stop to talk a little bit about some issues that are very relevant to shader writing: spaces, transformations, and interpolation.

Finally the examples are presented. With time their number will grow πŸ™‚

Well, this is it. Have fun playing with GLSL and OpenGL!


The Graphics Pipeline

OpenGL Setup

GLSL Syntax

Communication Application => Shader

Inter Shader Communication – Interfaces

Some notes before diving into the examples

Shader examples

Cube – This is the first example of this tutorial. It is quite simple on purpose, it just covers the basics to get our geometry on screen. It uses uniform blocks, and it shows how to transform points using the projection-view-model matrix.


Colored Cube – In this second example we talk about several approaches on how to set color. We explore colour as a vertex atribute, and setting color with uniform variables. We also introduce the theme of debugging shaders using color to display input or computed values.


spotlight Lighting – Lighting is essential in CG. This example goes through the basic theory and implementation of simple local lighting, using both Gouraud and Phong shading models for three common light types: directional, point and spot lights.



tcDemoPlaying with Texture Coordinates – Β Texture coordinates are commonly used to define how an image maps to a surface. However texture coordinates are not restricted to perform this mapping. Texture coordinates per se can be used to color a model. Here we will see a couple examples of how texture coordinates can be used on their own and explore a few new GLSL functions, such as mix and smoothstep.


texturedImage Texturing – Β Images are commonly used as wallpaper applied to 3D shapes to create a variety of effects. In this section we focus on the OpenGL and GLSL typical scenario of texture usage, and explore a few new GLSL functions, such as textureSize, textureQueryLod, and textureQueryLevels.


Source code and a VS2010 project are also included in each example.

  10 Responses to “GLSL Tutorial – Core”

  1. If you want to learn shaders in OpenGL 4 (with tessellation) you can try QuickShader

  2. Hi and thanks for your tutorial (before reading it :laugh:).
    I would really like to read it on my kindle.
    The send to kindle extension for Firefox has good results on your articles, but I would have to save each of the rathe short pages as a whole book…

    Could you make it available as a PDF, or even better in some kind of ebook format?

    Thanks in advance!

  3. Hi,

    I have used your VSLib and the examples from this tutorial in a computer graphics course I have taught this semester. It has proven to be very easy to use, hiding all the ugly details from the students πŸ˜€

    Since your library saved me a lot of work I think it is only fair if I give something back (if you are interestered). On this site you can find all the examples I have implemented, including extensions of your original ones. I have also slightly extended the VSLib with computation of tangent and bitangent vectors for the vertices.

    Thank you for all the great work.

  4. Hi again,

    one more error. In method “VSShaderLib::addBlocks” uniform blocks are set only when they haven’t been set previously. This causes problems when the same block is used in multiple shaders with different active uniforms – some of them may not be set in the second shader. This happens e.g. in your Lighting examples.

    I am no expert in either OpenGL or your libraries but for now I fixed it by commenting out “if (!spBlocks.count(name))” in vsShaderLib.cpp on line 597. It may be more natural though to make “spBlocks” map non-static.

    Hope it helps.

    • Yes, you’re right. This issue has come up with recent NVIDIA drivers. I’ve already updated the libs but not the demos.

  5. Hi,

    great tutorial. However, I have found a small glitch in function “processMouseButtons”. You do not clamp beta to the same interval [-85, 85] as you do in function “processMouseMotion”. Thus, it can get out-of-range values which then turns havoc to the camera when you zoom πŸ˜€

  6. I would like the tutorial translated into Chinese, and put on my blog, I could do this ?

  7. Dear sir,
    Can you please add the tutorial for implementing texture with transparency through shader in gl_point_sprite; I tried a lot to achieve it but couldn’t get it.
    Thank you for your help πŸ˜€

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: