Modern OpenGL with Python - Part 0
Jorge Martínez Garrido
January 1, 2022
Computer graphics is, from my point of view, one of the most interesting subjects within software development. In the end, no matter which is your field of study, you will end up dealing with computer graphics in one way or another.
For example, in the case of the Python ecosystem, you are likely to have used matplotlib. However, you may have noticed that it limits as soon as you start asking it for more complex scenes or animations. As an example consider the following problem:
What if I want to create an interactive Solar System having each planet its own texture? An what if I want to real-time process inputs and events from the user to update some-how the graphics scene?
The matplotlib package is amazing but it was not devised to cover previous kind of problems.
Therefore, how can I create high-quality real-time interactive graphics? The answer is simple: use a graphics API.
What is a graphics API?
But first, what is an API?
API stands for Application Programming Interface. Typically, an API ships in the form of a software library which provides you with fundamental routines in order to simplify a particular workflow. For example, let us consider again the case of matplotlib:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4], 'royalblue', marker='o', label=r"$f(x)=x$")
ax.set_xlabel(r"$x$")
ax.set_ylabel(r"$f(x)$")
ax.set_title("The identity line")
ax.legend(shadow=True)
plt.show()
Previous piece of code allowed us to create in a very easy way a figure with a dot marker at each one of the data points which form the line. Furthermore, this line has been colored with a royalblue color, ships with a shadowed legend, labeled axis and a title.
How is it possible that we managed to tell our computer to turn on/off the screen’s pixels to create previous figure?
That is the idea of an API. It hides most of the tedious logic and provides you with a high-level interface so you can focus on getting your things done.
Back to the graphics API concept
Knowing this, it is possible now to come back to the concept of graphics API. As you may have guessed, these are software libraries which provide you with a variety of commands for creating beautiful computer-generated graphics on your machine.
Among some of the most popular graphics APIs1 you can find:
- OpenGL: https://www.khronos.org/opengl/
- Vulkan: https://www.vulkan.org/
- DirectX: https://docs.microsoft.com/en-us/windows/win32/directx
As you saw in the title of this series, the one we will be using is the OpenGL one. Why? Because it is very easy as compared to the other available alternatives.
If you are interested in which of the previous graphics API is the best for you, there are plenty of forums and videos discussing this topic.
Old vs Modern OpenGL
OpenGL has existed for around 30 years since the publication of this post. In fact, OpenGL version 1.0 was released on June 30, 1992. The hardware capable of dealing with computer graphics in those days was limited to universities, labs and big companies using CAD programs.
They way OpenGL used to work in those days is no longer used. However, it is still accessible. Why? Because legacy software. This has lead to a bunch of outdated materials which newcomers take as good resources for learning OpenGL. As soon as they dig a little bit on the web, they find they have “lost” their time learning outdated concepts.
We will talk about Old and Modern OpenGL in future posts, but for the moment, you can start by reading OpenGL history.
Why Python?
Python ships is syntactic sugar, which makes this language a very readable one. The ecosystem and Python community is huge, there are lots of useful packages and projects from which you can benefit from.
Another point on the usage of Python is that information about how to use it with OpenGL is almost non existent. Thus, I would like to share with other Python developers how to deal with computer graphics using Python.
By the end of this series, you should be familiar with the main concepts and workflow behind any application using OpenGL. After this, you will be able to apply the same concepts and workflow within other programming languages as long as you are familiar with those.
FAQ and misconceptions about OpenGL
A very popular misconception is to think that OpenGL is some kind of “open-source” library who’s source code is located in an online repository.
OpenGL has no source code: it is just an API specification!
OpenGL as an API specification
Your computer is probably different than mine one, as they ship with different hardware. For example, consider the graphics card. This piece of hardware has evolved dramatically during the last 15 years: different models, slots, computation power…
Imagine now having to program a graphics API which has to consider each one of the models in the market, so it can work in every computer. It would require an insane amount of work to be maintained! Not only that, manufacturers may not want to share all the technical details about their products. Therefore, how could this problem be addressed?
What could be done instead is to provide a specification, that is a common set of commands and functions which specify the input and output commands and let the logic within these functions to be implemented by each one of the hardware manufacturers. This is the main idea behind the OpenGL API specification.
Main advantages are:
- Hardware independence.
- Common standard, that is what Open in OpenGL stands for.
Therefore, if you want to “install OpenGL”, just make sure that your computer recognizes your graphics card and that its drivers are up to date. If your computer is modern enough, it is likely you already have OpenGL installed in your machine.
Mesa 3D: an open-source implementation of OpenGL API specification
We talked before about manufacturers the fact that manufacturers may not want to share their OpenGL implementation, as this would expose the internals of the products. In this case, you are limited to update your graphics card with the drivers they provide you.
Hopefully, the Mesa 3D graphics library solves for this problem by providing an open-source implementation of the OpenGL API specification. You can always check Mesa 3D official documentation and check the official Mesa 3D repository.
If you are using a Linux distro, it is likely you have installed Mesa 3D within your machine.
Who’s behind OpenGL? The Khronos Group
Even if OpenGL has no source code, there is people behind it making sure it is compliant with the latest updates and improvements in the computer graphics market. From Khronos’ official webpage:
The Khronos Group is an open, non-profit, member-driven consortium of over 150 industry-leading companies creating advanced, royalty-free interoperability standards for 3D graphics, augmented and virtual reality, parallel programming, vision acceleration and machine learning
Up to which OpenGL version does my computer support?
In khronos’ info webpage you can look for your graphics card and check a variety of specifications and requirements related with OpenGL support.
References
A comparison of modern graphics APIs, by Alain Galvan. ↩︎