Anton's OpenGL 4 Tutorials

Mid 2012, I started teaching myself OpenGL 4.2 for teaching. This was new, because most of my graphics experience was with older "fixed-function" libraries that work mostly on the CPU. OpenGL 4 is very different to OpenGL 2.1. In code there is no more glBegin() and glEnd(). The problem-solving approach is also different, as most algorithms are implemented in shader programmes where your algorithm is designed to suit the hardware.

It is very hard to find reliable information on programming with OpenGL 4.x, so I intend to update these pages as a series of mini-tutorials for common techniques, and also to discuss some of the weird, wonderful, and frustrating coding issues that I run into... hopefully with solutions!

I usually say something on Twitter Follow @capnramses when I write a new article. Please let me know if you spot a mistake.

I'm also using these techniques in a video game that I've been working on for some years called Crongdor the Barbarian. I use WebGL at work which has a JavaScript interface instead of C, but much the same shaders. I recently did a WebGL game for the Ludum Dare 48-hours competition - Dolphin Rescue.

If you're wondering about where to get a package of completed "source code" demos, I actually think that's a bad idea when you're learning. You really do need to understand how every little bit works to get anywhere with OpenGL's low-level interface. All of the code that you need is in the tutorials. That said it can be a relief to check against a "finished one" to help spot any small mistakes. So, I'm currently putting them all together. I'm also formatting these tutorials into a little e-reader book (and maybe a small paper book) that also contains some more opinionated articles about how I would build larger programmes, and more kind of appendix topics that aren't directly related to GL tutorials, but might be useful in larger projects and games. I'll try and package all of this stuff together, and either release it for free, or for the price of a coffee, so I can make them faster. Give me a couple of months!

Latest Changes
16 Apr 2014 - Fixed lots of small errors in various articles.
8 Apr 2014 - Texture Shadows - fixed shaders to get around interpolation errors.
7 Apr 2014 - Skinning pt3 (version 1) uploaded.
3 Apr 2014 - Skinning pt1 and 2: Moved axis correction in blender. Simplified animation.
31 Mar 2014 - Added 'suggestions' to end of normal maps. Improved gl log code in "Extended".


"Hello Triangle" easy. 2 hours. Minimal code to draw a triangle using simple shaders and vertex buffers.
Extended Initialisation easy. 2 hours. Specifying OpenGL version, configuring the display, getting driver information, extension handling with GLEW, and adding a frames-per-second counter.
Shaders moderate. 2 hours. Shaders determine the style of rendering.
Vertex Buffer Objects easy. 2 hours. Vertex buffer objects hold the mesh data to render on the graphics hardware. We add a vertex buffer with colours to our triangle and show vertex-fragment interpolation.


Matrices and Vectors moderate. 2 hours. A light introduction to using vectors and matrices in OpenGL 4 for moving, scaling, and rotating, and some basic animation.
Virtual Camera moderate. 2-4 hours. Building a transformation pipeline to add viewpoint and perspective to a scene.
Quaternion Quick-Start moderate. 2-4 hours. A simple explanation of quaternions, and how to write your own quaternion code for 3d (actually 4d) rotation. Useful for full rotation (rolling balls, wheels, etc.), and interpolating rotations in animation.
Ray-Based Picking moderate. a couple of hours. Cast rays from mouse pointer to various primitive "bounding" shapes.

Lighting and Texture Maps

Phong Lighting moderate. ~1 day. Shading a scene with virtual lights.
Texture Maps moderate. 2-3 hours Loading images from files. Texture coordinates. Sampling textures in shaders. Mip-maps and filtering.

Tips and Tricks

Screen Capture easy. 1 hour. Simple function to save a screenshot to a file.
Video Capture moderate. 2 hours code. 2 hours processing. Extending the screen capture function to create a video from a regular series of screen dumps.
Debugging Shaders easy. 2 hours Methods for graphically "debugging" a shader programme part-by-part.

Mesh Files

Importing a Mesh File easy. 2-4 hours Choosing a file format, exporting a mesh, importing with the AssImp library, and copying into vertex buffers.

More Advanced Lighting and Texture Effects

Multi-Texturing easy. 1 hour Blending textures together for various effects.
Using Textures for Lighting Coefficients easy. 2 hours Specular Maps, Diffuse Maps, Emission Maps, and Ambient Occlusion Maps.
Fragment Rejection easy. 30 minutes They'll get over it. Tell a fragment not to draw at all for "all or nothing" transparency.
Alpha Blending for Transparency easy. 1 hour Techniques for managing geometry with transparent or semi-transparent parts.
Spotlights and Directional Lights easy. 1 hour. Some variations on lighting directions for interesting effects.
Distance Fog easy. 1 hour. Add fog, gloomy darkness, or a subtle colour fade to aid the perception of distance.
Normal Mapping moderate. 1 day. A "bump mapping" lighting technique/optical illusion - the easy way (using tools).
Cube Maps: Sky Boxes and Environment Mapping moderate. 1 day. Look at using GL cube maps for sky boxes, and environment-mapping reflection and refraction effects.

New Shader Stages

Geometry Shaders easy. a couple of hours. Extrude or delete geometry on-the-fly using a geometry shader.
Tessellation Shaders moderate. a few hours. Automatically generate more detail inside surfaces using new GPU hardware.

2d Rendering

2d GUI Panels easy. an hour For debugging and user interfaces.
Sprite Sheets and 2d Animation moderate. several hours. Handling 2d animation without the overhead caused by texture switching.
Bitmap Fonts moderate. a day or so per font. Using a font atlas and hand-drawing custom fonts.
Making a Font Atlas Generator Tool moderate. a day or so. Using the FreeType library to generate a font atlas and layout meta-data from a font file.


Vertex Displacement Animation easy. ~2 hours. [Taken down until I fix some wrongness].Using functions in a vertex shader to do effects like waves.
Particle Systems moderate. ~4 hours Create special effects in canned systems.
Hardware Skinning Part 1: Bones moderate. ~1 day I go over all the basic concepts of skinning, including importing bones, building skinning shaders, writing animation C-code, visually debugging, and the tricky parts of making a mesh with bones in Blender.
Hardware Skinning Part 2: Skeleton Hierarchies moderate. ~1 day In Part 2 we build skeleton hierarchies in Blender, and write C data-structures and animation functions.
Hardware Skinning Part 3: Key-Frame Animation moderate. ~1 day (UNDER CONSTRUCTION) In Part 3 we interpolate key-framed animations, and complete our animation functions.
Morph Target Animation moderate. 4 hours Using interpolation between animation "targets". AKA "blend shapes".

Multi-Pass Rendering

Switching Framebuffer fiddly interface. 2-5 hours. The foundation of all multi-pass technques. Render to a texture instead of the screen. Reuse that texture.
Image Processing with a Kernel 2 hours Slide a 3x3 or 5x5 window over the screen for effects like Gaussian blurs.
Colour-Based Picking fiddly interface. about a day. Render unique things to special colours (but don't show them). Test which special colour the mouse cursor is over.
Deferred Shading fiddly interface. about a day. Exploit texture attachments! Do lighting in screen space! Unlike other tutorials I also criticise it!
Texture Projection Shadows minefield! several days. Projecting shadows using the depth buffer. It is tricky to set up, and takes a lot of tuning to improve the visual quality.

Things That I'll Probably Do Eventually

Useful References