Installing OpenGL
You need to install an environment on your hardware where you can compile and
run OpenGL programs. OpenGL is supported on most modern operating systems, but
how to compile and link programs varies greatly. The description below assumes
that you already have a compilation environment set up.
Before asking for help, look at the
Troubleshooting hints
to localize the problem rather than asking a meaningless question like
It doesn't work.
Linux
OpenGL support is very easy to enable on current Linux distributions. For
distributions derived from RedHat Linux, the libraries and header files are
installed using the command
yum install freeglut-devel
Since GLUT depends on OpenGL and a number of other libraries, installing GLUT
will trigger the dependencies needed to install everything else. For
distributions derived from Debian such as Ubuntu, the installation command is
apt-get install freeglut3-dev
To compile and link your program all that is required is
gcc -o foo foo.c -lglut
On some Ubuntu based distros you need to explicitly grab GLU using
gcc -o foo foo.c -lglut -lGLU
Older distributions may put the files in /usr/X11R6, in which case you need to
add -I and -L flags to pick up the header files and libraries.
Once installed, run the glxinfo program and look for direct rendering in the
output. If the result is YES, then hardware support for OpenGL is working. If
it is NO, some things are done in software and you may take a performance hit.
Depending on your hardware, you may want to work on your X server.
Specifically, the nVidia and AMD/ATI web sites contains updated drivers that
result in improved performance over the stock Xorg drivers.
The compiz window manager (which is an OpenGL window manager) makes applications
which use glutIdleFunc() run jerky unless you enable VSync. This seems to be an
issue especially with newer Ubuntu installs.
OS/X
The OS/X Darwin environment is based on OpenGL. Therefore any compilation
environment for OS/X should already support compiling OpenGL programs.
To compile and link your program using the Apple SDK requires
gcc -o foo foo.c -framework GLUT -framework OpenGL
Note that under OS/X, the GLUT header files are in the subdirectory GLUT rather than
the GL subdirectory. The following code works on OSX and Linux
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
Windows
Various Windows versions support OpenGL natively, but graphics card
manufacturers often replace the native Windows OpenGL libraries with their own
libraries that support specific features of their hardware.
Since most Windows environments do not contain a native compilation suite,
installing the necessary header files and libraries differs depending on the
compiler used. You may also need to install
GLUT from Nate Robins.
Windows: MinGW
If you don't have a compiliation environment on Windows, you can install MinGW
which provides the gcc compiler for Windows.
Download and install
MinGW.
Accept all the defaults, except that you also should install "make". After the
installation is completed, change directories to C:\MinGW\bin and rename
mingw32-make.exe to make.exe to avoid having to type mingw32-make
every time you want to use make. You should also add C:\MinGW\bin to
your PATH.
Finally, install GLUT by unzipping
my version of GLUT
into C:\MinGW. It will provide include\GL\glut.h and
lib\libglut32cu.a needed to use GLUT. It also provides
glut32.dll. Move glut32.dll to C:\windows\system32 so that
the system can find this DLL at run time.
Note that my version of GLUT has been patched to provide glWIndowPos which
otherwise would not be available on Windows systems which support OpenGL
version 1.1.
To compile and link foo.c under MinGW you need
gcc -Wall -ofoo foo.c -lglut32cu -lglu32 -lopengl32
Windows: DevC++
Jeremy Salter provided this writup for installing OpenGL with
DevC++.
Windows: Other
Your Windows system may provide OpenGL 1.1, which dates from 1995. This version
will not contain the glWindowPos functions which I use a lot. You can provide
the same functionality in software by including
this code
in your build. DO NOT include this code in what you submit, as it will result in
a conflict in Linux or OSX systems. You do not need to install this code if you
use the glut32cu library described above as this library already adds this
code.
Troubleshooting hints
If you are having trouble getting the programs to compile, you may want to
consider these things to determine if the problem is in your compiler, header
files or linker/libraries.
- Try compiling the classic C program that prints Hello
World! to the screen to check that your compiler and linker works
in the absence of OpenGL. If this fails to compile and run, your compile
environment is broken and needs to be fixed before you can work on getting
OpenGL to work.
- Try compiling ex5.c. If you get errors like
error: GL/glut.h: No such file or directory
or
error: 'GL_COLOR_BUFFER_BIT' undeclared
the compiler cannot find yout OpenGL header files. Check that the
files are in an expected place like /usr/include/GL or use
the -I compiler flag to tell the compiler where it is.
- Try linking ex5.c.
undefined reference to 'glut... means the GLUT library was not found.
undefined reference to 'glu... means the GLU library was not found.
undefined reference to 'gl... means the GL library was not found.
Make sure the libraries are in an expected place like /usr/lib or
/usr/lib64 or use the -L flag to tell the linker where it is.
- If you get warning: implicit declaration of function 'glWindowPos2i'
try adding
#define GL_GLEXT_PROTOTYPES
before any includes.
- If you get undefined reference to glWindowPos2i you may have an
old GL library. Link in my glWindowPos code or use glut32cu.a with
MinGW.
- If you use C++, be aware that sometimes the compiler does name mangling on the
file names while glut32cu is plain C which does not do name maningling and may
not provide all the entry points.