A Beginner’s Guide to Setup OpenGL in Linux (Debian)

Step-by-step guide for setting up OpenGL in Ubuntu along with the installation of required libraries: GLFW and GLAD.

Priyanshu
Geek Culture

--

Setup OpenGL in Ubuntu

So you wanna get started with OpenGL in Linux, but couldn’t find enough resources on the net to set it up? Don’t worry, I got you covered!
This article would provide simple instructions for setting up OpenGL environment in Ubuntu and other Debian-based Linux Distributions without using VS Code. We’ll also be installing the required libraries GLFW and GLAD.

For a tutorial on how to use OpenGL, I’d recommend this website which teaches it from the very basics using easy-to-understand examples. If you prefer video tutorials, you might want to check this out.

Introduction

OpenGL is a cross-platform, cross-language API that provides us with a large set of functions that we may use to render 2D and 3D vector graphics. The API is typically used to interact with the GPU, to achieve hardware-accelerated rendering.

However, OpenGL by itself is not an API, but merely a specification. It is just a description of what exactly the result/output of each function should be and how it should perform. It is already implemented inside your driver, by manufacturers, following the specification. Hence, there is no such thing as “installing” OpenGL. But, we do need to install libraries, that would help us interact with the operating system to access the implementation and set up the windowing system & OpenGL context.

OpenGL Setup: Installing dependencies

Before installing the required libraries, we’ll first have to install a few dependencies. So open your terminal and run the following commands:

sudo apt-get update
sudo apt-get install cmake pkg-config
sudo apt-get install mesa-utils libglu1-mesa-dev freeglut3-dev mesa-common-dev
sudo apt-get install libglew-dev libglfw3-dev libglm-dev
sudo apt-get install libao-dev libmpg123-dev

OpenGL Setup: GLFW Library

Before you start with creating stunning graphics, you need to initialize an OpenGL context and create an application window to draw in. We’ll do this using a popular C library: GLFW(Graphics Library Framework). This library also helps us handle the input from the joystick, keyboard, and mouse.

Running the below commands would install GLFW in your system:

cd /usr/local/lib/
git clone https://github.com/glfw/glfw.git
cd glfw
cmake .
make
sudo make install

OpenGL Setup: GLAD Library

As we know, OpenGL is only really a specification that is implemented insider of the driver that your graphics card supports. Since there are many different versions of OpenGL drivers, the location of most of its functions is not known at compile-time and needs to be queried at run-time. Now it’s a cumbersome process to retrieve the location of the function and load that in function pointers, for each function we need. Thankfully, there’s a library that can save us the hassle: GLAD.

GLAD (Multi-language Loader Generator) is an open-source library that uses a web service where we can tell GLAD for which version of OpenGL we’d like to define and load all relevant OpenGL functions according to that version.

So to install this library, perform the following steps:

  1. Head on to the GLAD web service.
  2. Set the language to C++ and choose the specification as OpenGL.
  3. In the API section, select gl version of at least 3.3, make sure the profile is set to Core, and that the Generate a loader option is ticked.
  4. Ignore the extensions and click Generate to produce the resulting library files.
  5. GLAD, by now, should have provided you a zip file: glad.zip containing two folders(include and src).
  6. Copy the folders inside include (glad and KHR) into your include(s) directory: cp -R include/* /usr/include/
  7. Now copy the file glad.c inside the src folder to your current working directory.

Running your first OpenGL Program

You’re now done with installing the required libraries.
Time to test if everything was installed properly using “Hello Triangle”: a simple program to render a triangle. You may copy the code from here and save it in a file named hello_triangle.cpp

Let’s compile our code & generate an executable a.out:

g++ hello_triangle.cpp glad.c -ldl -lglfw

Run ./a.out to see the program work. You should get the output as below:

Triangle rendered with OpenGL: an API to render vector graphics
A triangle rendered with OpenGL

That completes your OpenGL setup. Hope it was simple, and you were successful in it. If you wanna learn more about the source code that you just copied, read through this!

If you’ve any queries or got stuck somewhere, feel free to comment. Would try my best to help!
Also if you found this helpful, do clap and share it with your friends!

See you then. Until next time! :))

--

--