A C++ Framework For Interaction With OpenGL
Hello people. So I guess there is no better place to start than from the beginning, huh? So here we go, first actual post here will be about this project that I have been working on (and tweaking) for a while now, a framework for interaction with OpenGL.
What I have been doing for a while is implementing this framework from the ground up. I wanted to get a good feel of the graphics pipeline by building every small part of it. That means, make something that doesn’t use libraries such as OpenGL Mathematics, not that there is anything wrong with those, but rather I want to implement everything. So let’s get down to what is currently available in the framework.
The Stuff
The plan was to create the framework in such a way that would be easy to use in other environments and to improve upon. After some time working on the design I ended up implementing the following:
- A Vertex and Fragment shader reader.
- A Shape creator with a few primitives such as cube, cylinder and sphere.
- Some simple transformations (translation, rotation, scaling).
- Phong Illumination.
- Camera manipulation.
All of it was implemented in C++ taking advantage of object oriented programming. For instance, a shape is literally a class that I instantiate for different objects that I want, and then I can get the geometry of those shapes through functions.
Reading Shaders
One thing that took a while was to make my own shader reader. In other words, the code necessary to read the OpenGL Shading Language (GLSL), compile it and link it to a “program”. I implemented it in such a way that logs would be displayed if any errors happened when compiling the code (and man, that saved me a lot of headaches). So far it works for Vertex and Fragment shader, I’ll be working on geometry shaders later.
I Need Shapes!
Next on the list was the geometry: actually making shapes. For starters we have three primitives: a cube, a cylinder and a sphere. I’m able to manipulate the mesh of these shapes by increasing the number of subdivisions for each one.
The cube is simple, each square made of two triangles, and each side of the cube is divided into rows and columns of those squares, depending on how many you want. The cylinder has different subdivisions for both the base and the height. The height is made of simple squares, and the base was created using polar coordinates. And finally the sphere, which is made from icosahedron subdivisions.
The geometry for the shapes also generate normals, which can be either “flat” or “smooth”. Those are important for a number of applications, like illumination. And speaking of illumination, each shape can also have materials assigned so it can be illuminated by the phong illumination model.
Moving
Not that fancy really, the basic transformations you need for computer graphics is here: translation, scale and rotation (in Euler angles, no fancy quaternions yet).
Around The World, Around The World
Well having those shapes and being able to move stuff is not fun if I can’t look at them in different angles: enter the camera. The pipeline is not there if we can’t generate the good old View Matrix and Projection Matrix, so let’s do it.
On the framework you can set up your projection however you like, with orthogonal or perspective projections. And also, you can move around the camera (or rather the world, but bear with me for the sake of simplicity) to look at your world however you prefer, using the familiar WASD keys and/or the mouse (hold the left mouse key and move around).
Light up the night
Finally, you don’t wat to look just at wire frames, how about some solid objects? I have got you covered, we have the Phong Illumination model here waiting for you. Like I mentioned before, you are able to assign materials for any shape you want to, and those values will be used when you set up the light in the world. By implementing the correct shaders you can use an object’s material and normals, together with the light source to make the object visible however you prefer.
That’s All (For Now)
And that is what I have got so far. It was fun implementing all this stuff and seeing it work. And now with this framework I have got the basics that I need to make fancy stuff. More illumination models, raymarching, texture mapping… let’s see what happens next.
All this code is up on my github. Not only the framework itself, but also the code for the three videos you can watch here (or in my youtube channel) is out there.