GLSL shader coding

Contents

The rendering pipeline
Basic GLSL structures
Coordinate systems
Blinn-Phong Shading
Debugging
Texturing
Lighting beyond Blinn-Phong
Fogging
Vertex shader transformations
Scalar maps
Relief maps
Light intensity

Scope

This is a short introduction to graphics card programming for visual effects using the OpenGL rendering language GLSL. It comes more with a focus on concepts, what to do with the various bits of technology and how to solve certain problems than with a technical background on graphics hardware, an exhaustive summary of options or the fine print of GLSL syntax. For that reason, I don't claim to explain everthing technically corrent to the last detail. It's also not about the latest and greatest GLSL versions where some functions are deprecated and some syntax is changed (my experience is chiefly with OpenGL 2.x) - the underlying basic concepts aren't different though and you can achieve a lot using older GLSL versions as well - it might even perform well on older hardware. After all, the math of a transformation doesn't change, even if the name of the matrix does. If you want to use GLSL 4.x, the concepts by and large still apply, you just can't use all the code snippets literally.

Rendering is a subject in which lots of math is used - mostly linear algebra in the guise of vector geometry, the use of transformation matrices and such like, but even advanced math like differential geometry and tensor analysis are sometimes useful. Learning all the details of these disciplines are several university-level courses, but one doesn't need to understand the complete math to simply do interesting things. Nevertheless, I will assume a basic familiarity with concepts like transformation matrices, vector algebra or linear vs. non-linear functions in the following and just link explanations where required.

I have no formal background in 3d rendering, what I know about shader coding is self-taught - which probably on the positive side means I have a good grasp of how it feels to approach the subject as a beginner. There is also a good measure of perhaps more unusual approaches described - which is an advantage if you believe in out of the box thinking, a disadvantage however if you believe that the usual way of doing things is always best.

Largely I refrain from giving complete examples of shader code - one reason is that the details depend on the OpenGL version you want to run. The more important reason is that I believe one needs to tinker with things, insert a new line here and there, see what happens in order to learn anything - just copy/pasting a ready-made code doesn't lead to the same experience.

Finally, since I do shader coding mostly in the context of Flightgear, most of the examples will also be from the context of visualizing flight simulation, in particular driven by my interest in rendering atmosphere visuals.

Start with The rendering pipeline.

Useful links

Getting started with GLSL a tutorial on rigging OpenGL to the point of using shaders
GLSL documentation and technical references
OpenSceneGraph, an abstraction layer for the C++ part of rendering
OpenSceneGraph Tutorials
GLSL 1.1 quick reference card

Thanks

I'd like to thank the following people for patiently answering my questions, helping me to learn 3d rendering and providing example code from which I could learn:

• Stuart Buchanan
• Tim Moore
• Frederic Bouvier
• Emilian Huminiuc
• Lauri Peltonen

Back to main index     Back to rendering

Created by Thorsten Renk 2016 - see the disclaimer, privacy statement and contact information.