OpenGL with SDL & GLM
Since the last post I’ve got a bit further with SDL – and liking it. I’ve also been using GLM as a replacement for the missing math functionality in OpenGL.
If you started at the previous post, you’ll have seen how to use SDL to create a window – now to actually put something in that window. Here I am covering similar ground to a much earlier post on rendering with OpenGL 3.0, but this time using SDL – and introducing GLM.
To reiterate:
Earlier versions of OpenGL allowed programmers to draw in ‘immediate mode’ – sending geometry data to the graphics card one element at a time. This had some benefits – easy to learn, easy to program – but at some cost: the updating of data on the main CPU program and constant passing of data to the GPU was not suitable for high performance.
Modern OpenGL still supports immediate mode graphics in “Compatibility mode”, but the core OpenGL API now works quite differently. Applications pass data into buffers where geometry and texture information is held on the GPU itself. A single line of code in an OpenGL application can draw a highly complex model… after you have set up and initialised all of the required buffers, at any rate.
Along with the growth of programmable functionality in modern graphics cards, modern OpenGL has also done away with what was called the ‘fixed-function pipeline’ – where standard algorithms were applied to process the geometry fed by the application to the graphics card. Accordingly, the application programmer now has to load and initialise shader programs to tell the GPU how to process the geometry. For now, we’ll be dealing with very simple shader programs – but these can grow significantly in complexity!
In summary, your first OpenGL program will do the following:
- Use SDL to create a window
- Load a vertex shader for processing the 3D geometry and a fragment shader for determining the output pixels, and pass the appropriate data from application to CPU when drawing an object.
- Create a vertex buffer array object, VAO, on the GPU for storing data about the 3D vertices. The data will be stored in a set of vertex buffer objects, VBO, each of which will be used (in this application) for storing information about one attribute (position, colour, etc.) of each vertex.
The exercise makes use of two additional libraries: GLEW is required on Windows to access OpenGL features after version 1.1, and GLM is an OpenGL math library that makes it easier to perform some of the geometrical manipulations required.
The exercise sheet is below, and completed source code is also available on github.

