Lesson 0c: Getting OpenGL Set Up on Linux

Downloading and Installing the OpenGL and GLUT Developer Files

To get OpenGL working on Linux, you need to make sure that you have gl.h and glut.h, among other files. They're usually located in /usr/include/GL/. If you have these files, then most likely you're already set, and you can skip to the next section in this lesson.

If you don't already have them, you'll have to download and install the OpenGL and GLUT development libraries. The best way to do this is to find the appropriate package files. It's best to avoid downloading and compiling the source code for the libraries, as there are lots more things that can go wrong and for various reasons tends to be very annoying.

What package files you'll need depends on the distribution of Linux that you're using. If you're using Debian like me, you'll need a .deb file. To find out what package you need, you can go to the Debian packages website and search for files packages containing gl.h and glut.h, located in /usr/include/GL/. A quick search now reveals that the freeglut3-dev and mesa-common-dev packages provide these files, but this may change, and you may need a different package for an NVIDIA graphics card. To install the packages in Debian, the easiest thing to do is to open a terminal and enter "sudo apt-get install package1 package2"; this will take care of downloading and installing the packages and getting the other packages on which the OpenGL and GLUT development packages depend.

Another common package file format is .rpm, used in the Red Hat distribution and other distributions. To install the developer files using RPM packages, you'll want to search (e.g. using Google) for an RPM file that provides gl.h and glut.h, and install them, e.g. by double-clicking them in a file explorer. Different distributions have package formats other than .deb and .rpm; the process for them is similar.

If you find a package for a different distribution than the one you're using, you can usually convert it to the file format used in your distribution using the command "sudo alien package_name".

Compiling and Running the Test Program

Once you have gl.h and glut.h, make sure that OpenGL works. You can do this by downloading the source code for this later lesson and testing it out. Download the ZIP file and extract it to a folder (e.g. using a file explorer). Then, from the command-line, use "cd" to change to the directory where you extracted it. Enter "make" to compile the program, and make sure there are no error messages. Then, enter "./cube" to run the program, and make sure that it runs. Press ESC to exit the program when you're done.

If there was an error, most likely you don't have the developer packages installed properly. But if they are installed, you'll have to figure out how to fix the problem. Googling the error message that you get isn't a bad starting point. You can also find an online forum and ask someone there.

IDEs

To my knowledge, the standard way of editing and compiling a C++ program in Linux is to edit it with a text editor, such as KWrite or GEdit, or Emacs or Vim for advanced users, and compiling it using the "make" command like in the previous section. There are some IDEs out there, such as KDevelop, that you can use instead. If you'd like, you can get one of these and figure out how to use it.

Makefile

Look at "Makefile" in the source directory. It is an important file used by "make" to figure out how to compile the program. The line "PROG = cube" tells "make" that we want the executable file to be called "cube". The line "SRCS = main.cpp imageloader.cpp" tells "make" the files that we need it to compile. You'll have to edit these lines if you ever want to change the name of the executable or change the source files that "make" compiles.

Making Sure That Hardware Acceleration Works

Now that you can compile and run an OpenGL program, you need to make sure that OpenGL is using hardware acceleration. To check this, enter "glxinfo" in the command line. If you have glxinfo installed, it will either say "direct rendering: Yes" or "direct rendering: No" at the top of the output. If you don't have direct rendering, OpenGL programs will work, but they'll be really slow, as they will not use your graphics card, so you'll want to get it working.

Getting hardware acceleration to work is a rather complex problem. I can't tell you everything, but I will give you some starting points. You need to make sure that your OpenGL drivers are installed and working properly. Checking /var/log/Xorg.0.log for lines beginning with "(EE)" should be helpful. Entering "LIBGL_DEBUG=verbose glxinfo" at the command line might give you more information about what's wrong. Again, Googling your error message and posting on a Linux forum tend to be helpful.

When you get it working, congratulations! You're ready to learn how to program in 3D.

Next is "Part 1: The Basics".