This page is reserved for answers and tips regarding OpenGL programming. If you feel something should be added or have a contribution, please email the unit lecturer. You might also be interested in the OpenGL sample source code page.
To compile your Open GL program using gcc you need to do something like this:
gcc simple.c -o simple -I/usr/X11R6/include/ -L/usr/X11R6/lib -lX11 -lXi -lXmu -lglut -lGL -lGLU
This assumes a single source file, simple.c
. This file should
include the glut (which includes gl.h
) i.e.
#include <GL/glut.h>
For some examples see the sample
code page. Note that if you include glut.h
then gl.h
and glu.h
are
automatically included as well.
For the assignments you are required to use a Makefile
. Also
your solution probably will incorporate several source files. Do not put all
your assignment code into one source file — break it up into logical
units (C) or classes (C++).
Note that if you are coding in C++, gcc v.3+ uses namespaces and so if you include standard classes such as iostreams, you'll need to include:
using namespace std;
at the start of your source file, otherwise you'll need to fully qualify everything
within the std namespace, e.g. std::cout
Mesa is an open source software implementation of OpenGL. You should use and download MESA only if your computer (and more specifically your graphics card) does not have any existing hardware OpenGL drivers. Because MESA is a software implementation of OpenGL it will not give the same performance as a hardware based implementation.
You can find hardware OpenGL drivers for popular graphics cards on-line. The OpenGL.org site provides a news about current OpenGL compatible cards. You can download drivers for Linux direct from manufacturers web sites, e.g. Nvidia and ATI.
The Linux lab machines have MESA and GLUT already installed so you should be able to complete the assignments on these machines without installing any additional software.
Where is GLUT?
GLUT (OpenGL Utility Toolkit) is a software library that uses OpenGL to provide
simple portable window management, mouse, keyboard and menu interaction (these
are not provided
by OpenGL). Most OpenGL distributions include GLUT. If you are using Mesa,
GLUT is in the separate MesaDemos-x.y.z.tar.gz file.
If you don't already have GLUT installed, you should grab the MesaDemos package
and unpack it before compiling Mesa.
On a Unix or Linux based system, OpenGL works via the X Windows system. This means that you can run an executable on one machine and display the graphics output on another. To do this you need to set your DISPLAY environment variable and configure your local machine to accept connections from the remote host. You also need to have the GLX extension installed on your local (display) Linux machine.
For this example, let us assume you are running your OpenGL program on a Linux machine called "ra-clay1.its.monash.edu.au" and you wish to display the output on your Linux machine at home, which has the hostname "blaster.octopus.net.au".
On the remote machine (ra-clay1.its.monash.edu.au) type the command:
setenv DISPLAY blaster.octopus.net.au:0.0
On the local machine (blaster.octopus.net.au) type the command:
xhost +ra-clay1.its.monash.edu.au
blaster will then print something like "ra-clay1.its.monash.edu.au being added to access control list" – now your local machine will accept connections from ra-clay1.
You can now run your program from ra-clay1, and the X window output (i.e. OpenGL stuff) will appear as an X Window on blaster. The interactive speed will depend on network bandwidth. Doing this over a modem connection is not recommended :-(.
You will need to substitute your own local host name (or use IP numbers) for the local machine. Likewise for ra-clay if you want to use another machine as your remote host.
Using forwarding via ssh:
Here is a quicker method from Andrew Bowden that works behind firewalls (the above method will not):
The solution is much simpler (and secure) if you just use ssh to forward your
X traffic for you. All that needs to happen is you as the user on your
local machine creates (or appends to an existing file) the file {home}/.ssh/config and in this file you add the line:
ForwardX11 yes
Then, all you need to do is to connect to ra-clay with the command:
ssh -X <username>@ra-clay1.its.monash.edu.au
and all graphical output will automagically be placed on your local machine!
From Tim McCoy:
I downloaded and attempted to make the
Mesa-5.0 source. It made (ie: make) and installed (ie: make installed) fine,
but in executing make check and make exec, I got some errors, and Unreal Tournament
2004 wouldn't run, nor would any of my other OpenGL Hardware accelerated programs.
Seems to have caused an issue with my libraries - not even the recommended
manual install method worked.
From Rob Colvin:
The newsgroups seem to be full of people having trouble with RedHat8.0 and
GLUT. The simplest solution I have found (I have been playing round with old
versions of Mesa, and FreeGlut and anything else I could find for the last
few hours) is to download the following rpms from the redhat website as required
• glut-3.7-8.i386.rpm
• glut-devel-3.7.-8.i386.rpm
and install them, I could then compile the majority of source found in the
Mesa 3.4 demo tar.gz avalable from the Mesa website.
I think there is also a warning in the FAQ on the Mesa site which might explain
the current RedHat 8 / Mesa5.0 problem, though I haven't read much into it.
More from Tim:
I'd rpm'ed the glut package, but not the glut-devel... Oh well! No further
luck with the Mesa-5.0 libraries, mainly because there are issues linking in
the new libraries, and as described on the Mesa website, Hardware acceleration
will suffer a great issue as a result of using these libraries as primaries.
I found those who feel they have to use the v5.0 libs can (as in the readme)
cp Makefile.X11 Makefile
make
And then in the source directory will be the libraries. I've found these can
be accessed by defining an absolute path to them, but this will just get messy
come submission time. So rpms are the go here. The next step of the install
is the issue - writing over the default libs is causing issues as I assume
there are conflicts in the libGL.so type files. Students are reporting an all
clear using the standard configure, make, make install procedure, but I'm assuming
one of two things:
1. RH has a specific implementation (possible, by more likely is the following)
2. These students are not using Linux as a primary OS, and hence may not be
noticing that hardware acceleration has died. I noticed a slight lag in X (my
first warning sign), and it was not until I ran progs requiring hardware acceleration
that I found the issue. It seemed to be an issue linking to the new v5.0 library
(libGL, or something similar). To test Hardware accel, try installing a game
like foobillard (http://www.geocities.com/harpin_floh/foobillard_page.html)
or tuxracer shuld fail or show high CPU usage is not hardware accelerated.
Note that this is dependant on your Distro and X being set up with hardware
acceleration enabled (eg: NVIDIA drivers from NVIDIA
website)
So rpm the glut-packages from redhat.com (or rpmfind.net) to get it all working
and maintain system integrity.
Also, it's worth noting that for RedHat8.0 users, the compile option should
be:
-I/usr/include/GL
(not /usr/local/include/GL
)
Yes, it is possible to run GLUT and OpenGL on Windows. For assignment submissions, please test your program on Linux to ensure portability.