
Hands on with OpenGL
Recently, I started studying about the homogeneous coordinate systems and their applications in Computer Vision. While rummaging around different articles and blog posts, I got fascinated by the use of maths in graphics and wanted to have an idea of how actually one does that, how one program the pixels to look like that of a real world and how is that we are able to map the real 3d world coordinates into this 2d image plane.
It is here, I came to know about the use of OpenGL in computer graphics. The first thought that came into my mind was “How do I install it on my Linux machine?” Well, the starting thought itself was a wrong one. To get a clear picture, let’s look at what is actually OpenGL.
What is OpenGL?
It is a specification. It is neither a library nor a package. It is a description of how something should work. It is not a concrete code which can be executed. Just like C++14 has got its own set of standards that one should follow to compile it, OpenGL has its own set of standards.
And that is why you can’t install OpenGL. It is already implemented inside your OS GPU driver, by manufacturers, without any kind of agreements on the actual implementation, and, so, different drivers may achieve the same functionality in different ways.
How do I use it then?
You can use the OpenGL functions by the help of other packages & libraries such as GLEW (OpenGL Extension Wrangler Library) which is a cross-platform C/C++ library that helps in querying and loading OpenGL extensions.
GLEW can load every actual OpenGL function you might need. It just needs to be included as a header file, and, then, initialized through the already-existing function. On succsessfull initialization, correct function pointers will be there.
Once you have access to the functions, you need to initialize a window interaction and OpenGL context. You can do this with the help of the following 2 cross-platform implementations:
- GLUT (OpenGL Utility Toolkit), which is an official and very old way of doing that.
- A newer library, called “GLFW” (Graphics Library Framework). This is what I used.
Dependency packages & libraries
Install the following packages & drivers to be able to work around with your graphics card:
sudo apt-get install cmake libx11-dev xorg-dev libglu1-mesa-devfreeglut3-dev libglew1.5 libglew1.5-dev libglu1-mesa libglu1-mesa-dev libgl1-mesa-glx libgl1-mesa-dev libglfw3-dev libglfw3
Let’s get our hands dirty
Now that you have a fair idea on OpenGL and have also installed the required libraries, let us write a simple program. Save the below code in main.cpp.
Now let’s create a Makefile to make it easy for us to execute the code and also simple to link the libraries.
Now to compile and run the program type: make && ./main
Sayōnara.
