In this tutorial shader programming using GLSL 1.2 will be covered. Although many parts are now considered deprecated, the essence of GLSL has remained constant. Besides, this tutorial will continue to be online because learning the basics of deprecated OpenGL is easier than learning the new versions. If you’re looking for a GLSL tutorial that deals only with non-deprecated features visit the GLSL Core tutorial, here in Lighthouse3D.
Shaders are a hot topic and 3D games have shown that they can be put to good use to get remarkable effects. This tutorial aims at providing an introduction to the world of shaders.
The tutorial contains an introduction to the specification, but reading the OpenGL 2.0 and GLSL official specs is always recommended if you get serious about this. It is assumed that the reader is familiar with OpenGL programming, as this is required to understand some parts of the tutorial.
GLSL stands for GL Shading Language, often referred as glslang, and was defined by the Architectural Review Board of OpenGL, the governing body of OpenGL.
I won’t go into disputes, or comparisons, with Cg, Nvidia’s proposal for a shading language that is also compatible with OpenGL. The only reason I chose GLSL and not Cg for this tutorial, is GLSL closeness to OpenGL.
Before writing shaders, in any language, it is a good idea to understand the basics of the graphics pipeline. This will provide a context to introduce shaders, what types of shaders are available, and what shaders are supposed to do. It will also show what shaders can’t do, which is equally important.
After this introduction the OpenGL setup for GLSL is discussed. The necessary steps to use a shader in an OpenGL application are discussed in some detail. Finally it is shown how an OpenGL application can feed data to a shader making it more flexible and powerful.
Some basic concepts such as data types, variables, statements and function definition are then introduced.
The tutorial covers both the ARB extensions and OpenGL 2.0 versions. The former for greater compatibility; and the latter, because in the near future it will be the standard solution. The differences are small, and mostly have to do with slightly different function names and constants. Color coding has been used to help the reader to distinguish between them. The ARB stuff is presented in blue, and the OpenGL 2.0 in orange.
This dual coverage is prone to mistakes, so please let me know if you find something wrong.
Please bear in mind that this is work in progress and therefore bugs are likely to be present in the text or demos. Let me know if you find any bug, regardless of how insignificant, so that I can clean them up. Also suggestions are more than welcome. I hope you enjoy the tutorial.
The Graphics Pipeline
OpenGL Setup for GLSL
- Creating a Shader
- Creating a Program
- Example: Source Code
- Trouble Shooting: the InfoLog
- Cleaning Up
Communication OpenGL => GLSL
- OpenGL Directional Light I
- OpenGL Directional Light II
- Directional Light per Pixel
- Point Light Per Pixel
- Spot Light Per Pixel