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 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
- A Simplified Diagram of the Pipeline
- Vertex Shader
- Primitive Assembly
- Tessellation Shader
- Geometry Shader
- Geometry Shader Examples
- Rasterization and Interpolation
- Fragment Shader
- Creating a Shader
- Creating a Program
- Setup Example
- Troubleshooting: the Infolog
- Cleaning Up
Communication Application => Shader
Inter Shader Communication – Interfaces
Some notes before diving into the 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.
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.
- Directional Lights per Vertex I
- Directionl Lights per Vertex II
- Directional Lights per Pixel
- Point Lights
- Spot Lights
Playing 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
Image 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
Source code and a VS2010 project are also included in each example.