OpenGL Tutorial #6
-- "GLUT - the finale" --
Welcome to the final tutorial in this Hugi issue's series! The tutorial on how to code OpenGL for DOS, Windows, Linux, BeOS, MAC, and many, many others. Sorry, this won't teach you any new OpenGL features or effects. :( The GLUT libs/dlls are available at http://reality.sgi.com/opengl/glut3/glut3.html.
Introduction to GLUT
GLUT is an OpenGL clone library, it supports everything OpenGL does. It allows the user to work at a very high level, in terms of OS specific code. It also supports nice menu systems, mouse and keyboard input and quite a lot of other stuff. GLUT has ports for DOS, Windows, Linux, BeOS, MAC and some others. Which is pretty cool, so you can write some code, watcom it, visual C++ it, g++ it, or whatever, and it'll still do nice 3d accelerated effects.
It is IDENTICAL to OpenGL to do rendering code.
Setting up GLUT :>
This is so easy.
Firstly we create a window, to start with we specify its offset and dimensions.
glutInitWindowPosition(0, 0); // x and y position of window :)
glutInitWindowSize(Width, Height); // window's width and height.
Then its capabilities:
glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA | GLUT_DOUBLE);
That line gives us a depth (z) buffer, double buffer etc.
Now we'll create our window:
if(glutCreateWindow("Give me a name please") <= 0)
// give error
The argument is the window's title.
Wow! that was quicker than using standard OpenGL, eh?
The way GLUT works is that we give it some functions and it calls them for us during each render loop. Like this:
KeyHit is our keyboard input function, it gets called by GLUT every render loop, and it usually contains a switch statement to determine the key(s) that have been pressed, I'll show you the function below.
Next we set the Idle function, this is the function that is called when nothing is happening, stick a glutPostRedisplay() in there.
The glutDisplayFunc, specifies the function to draw our 3d scene. Finally we enter the message handling loop, and rendering loop by saying:
That's it, oh yeah, to get fullscreen, after you initialise you display funct- on, just say: glutFullScreen();
Coding Those Functions
First is the keyboard function, which I call KeyHit
void KeyHit(int Key, int X, int Y)
case 27: // is escape hit?
The display function is just the same as our DrawScene function, I don't need to give it here do I?
The idle function is just something like this:
Finally, you must call glutSwapBuffers() instead of SwapBuffers(window dc);
Compiling your code
Under Win9x/VisualC: Copy the DLLs to your Visual C++ directory for DLLs, and copy the libs to your Visual C++ directory for LIBS (just go into the ones full of DLLs and LIBs). Then link 'em in the normal way.
Under Linux: In Linux to do it quickly just go to /usr/lib/Mesa/demos and copy an makefile from there, replace the original name of the program it was for with your one, run make.
Sorry this is sparse, but the articles are due for Hugi tomorrow and I don't have time!
Well, yes, this is the final tutorial in this issue, I hope you enjoyed them, email me if you did, even if it's just to say "i liked your tuts" (you can cutnpaste that ;)).