16.8. X and 3D
Of course, Linux can display not only two-dimensional windows and structures in its graphical environment, but also three-dimensional graphics. There is a de facto standard for programming three-dimensional graphics, OpenGL , which originally came from big-iron Unix workstations, but which Linux supports just fine on inexpensive boards generally available for PCs. In this section, we look at how to set it up.
16.8.1. OpenGL Setup
As with many other subsystems of a free software operating system, Linux gives us a number of choices of OpenGL. Among those are Mesa, TinyGL, and YGL. The most prominent one, and the de facto standard for OpenGL on Linux, is Mesa.
OpenGL itself is platform neutral, so to "glue" OpenGL to a specific windowing system, an extension is required. For X11 this extension is called GLX. GLX contains X protocol extensions to allow OpenGL to be sent over the X socket to the X server. This is called indirect rendering. X.org has another option that is much faster, but works only on the local display. This option is called direct rendering and is explained in the following section.
X.org from Version 4 and up contains a framework for allowing direct access to the graphics hardware in a safe and efficient manner. This framework is called Direct Rendering Infrastructure (DRI), and accelerated OpenGL implementations sit on top of this framework. DRI consists of several components:
188.8.131.52. Proprietary drivers
Unfortunately, not all graphics hardware manufacturers want to publish information about how their hardware works. This is especially true for modern 3D accelerated hardware. But fortunately, the X.org XAA driver architecture is binary-compatible even across versions of operating systems (as long as the hardware architecture is the same), so installing a proprieratary binary-only driver is quite easy these days.
NVIDIA and ATI graphics cards are commonly found in PCs today. Newer versions of those cards are not supported by X.org/DRI for 3D hardware accelerated graphics, so we need to use the proprietary drivers published by the manufacturer.
The NVIDIA (http://www.nvidia.com/) driver doesn't seem to use DRI, but the overall design is quite similar. The driver comes as a ready-to-run installer-binary file that builds and installs a kernel module (which corresponds to the DRM driver in DRI) and then installs an X.org 2D XAA driver and replaces the libGL library on the system with an NVIDIA specific one. Notice that the kernel module comes with source code, but the other two components are binary only. For more details about how to install the NVIDIA driver , please read the information available on the company's web site.
ATI (http://www.ati.com/) also provides an accelerated 3D driver for its modern cards for Linux. Unlike NVIDIA's, this one actually uses the DRI framework. Except for that, they work in similar ways: kernel module with source available, binary-only X.org driver, binary-only DRI driver, and a replacement libGL.
184.108.40.206. Configuring X.org for OpenGL
With all the components of OpenGL and the related drivers in place, you can configure your system to use it.
Diagnosis. glxinfo is a valuable tool for setting up X.org for OpenGL. it gives information about the OpenGL capabilities of the current X11 display. Example output from glxinfo is as follows:
$ glxinfo|less name of display: :0.0 display: :0 screen: 0 direct rendering: Yes server glx vendor string: SGI server glx version string: 1.2 server glx extensions: GLX_ARB_multisample, ... client glx vendor string: SGI client glx version string: 1.4 client glx extensions: GLX_ARB_get_proc_address, ... GLX extensions: GLX_ARB_get_proc_address, ... OpenGL vendor string: Tungsten Graphics, Inc. OpenGL renderer string: Mesa DRI Radeon 20030328 AGP 1x x86/MMX/SSE2 TCL OpenGL version string: 1.2 Mesa 6.1 OpenGL extensions: ...
This listing shows that we are currently using direct rendering with a Mesa-based DRI driver for an ATI Radeon graphics card. If hardware acceleration was not set up properly or something did not work, it would say direct rendering: No instead.
Altering xorg.conf . To get started using DRI, a couple of lines need to be added to the xorg.conf file shown earlier in this chapter:
Section "Module" ... # This loads the GLX module Load "glx" # This loads the DRI module Load "dri" EndSection ... Section "DRI" Mode 0666 EndSection
The Load statements take care of loading the modules required for OpenGL into the X server, and the Mode statement in the DRI section sets the file permission of the character special file that applications use to communicate with the DRM kernel driver. The special file is /dev/dri/cardN. Setting the permissions to 0666 allows all users with access to the X display to use hardware-accelerated OpenGL.
Mesa is a 3D graphics library with an API very similar to that of OpenGL. The core Mesa library is licensed according to the terms of the X.org copyright (an MIT-style license). Mesa is included with X.org together with the DRI framework, but if you want to use OpenGL on a platform not supported by DRI or want to get started with OpenGL programming, installing your own copy of Mesa can be a good ideaif not for anything else, then for getting the source code for the example programs.
Installing Mesa. If, for some reason, you want to compile Mesa yourself, it is a simple matter of downloading the latest MesaLib (along with MesaDemos) from http://www.mesa3d.org/, unpacking it, and compiling it. The current version of Mesa does not use GNU autoconf; instead it comes with a Makefile that contains targets for a large number of operating systems and hardware combinations:
$ tar xfj MesaLib-6.2.1.tar.bz2 $ tar xfj MesaDemos-6.2.1.tar.bz2 $ cd Mesa-6.2.1 $ make Will write a list of supported build targets $ make linux-x86 We choose Linux/x86
When the build is complete, run some of the demos to check that everything works:
$ cd lib $ export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH $ cd ../progs/demos $ ./gears
If the demo application worked, install Mesa like this:
$ cd ../../ $ cp -r include/GL /usr/local/mesa/include Install header files $ cp -d lib/* /usr/local/mesa/lib Install libs
Hiding the headers and libraries away in /usr/local/mesa allows you to easily switch between the system-provided OpenGL and Mesa by setting LD_LIBRARY_PATH to include or exclude /usr/local/mesa/lib.