|

OpenGL Tutorial Part 01 - by Ulf Ochsenfahrt
Hi, this is the first of the OpenGL with FreePascal tutorials. I'll introduce the most important parts of an OpenGL-program in the following (Some parts will be explained in latter tutorials, so don't ask :-). This is my first tutorial ever and as I'm not natively an english speaker, I would like you to send me any corrections, comments or annotations you might have. They're not going directly to /dev/null...
As you can see, this is html and I tried to keep to the standards, so that it is compliant with ANY BROWSER.
If you want to read this, you'll have to get used to my programming style...
P.S.: I like yellow (and white).
Idea
We'll be using OpenGL with hardware acceleration enabled to do all the rendering. We'll at the same time use the OpenGL Utility Toolkit (GLUT) to keep all the windowing stuff. This makes it possible to easily port Win32 programs to Linux and vice versa. At the same time we don't have to learn the windowing stuff anyway.
System Requirements
What you'll need to follow this tutorial step by step:
- Windows Me (or compatible)
- it might work with Linux
- i didn't test it with anything else, so don't quote me on that
- FreePascal (http://www.freepascal.org)
- FreePascal GL and GLUT units (same as above)
- GL32.DLL (should come with your graphics driver)
- GLUT32.DLL (get it either here or here)
- i only tested the second source yet
System Setup
Make sure the following settings/configurations are like this
- the DLLs are placed in /windows/system
- the FreePascal IDE compiler target is set to "Win32"
- the linker is enabled (OPTIONS-LINKER)
- the GL and GLUT units can be reached (OPTIONS-DIRECTORY)
- don't enable all the optimization options (I had some problems with them)
Tutorial 01
Idea
We'll produce the outline of one of the simplest OpenGL/GLUT programs I can think of. ok, we'll already have some extras, but never mind.
So, how does GLUT work? Remember, GLUT does all the windowing stuff for us. All we have to do is draw, if something has to be drawn and process events, if there are events. GLUT works on the basis of registering callback functions. They are called when the appropriate events have occurred. In this tutorial we will utilize two of the callback functions. In special, we will register a draw callback and a reshape (or resize) callback.
Code
Let's do some declaration things. I don't think they'll need comments.
Program Tutor01;
Uses GL, GLUT;
If someone resizes the window we'll have to react and change the OpenGL drawing parameters in response. In detail we'll have to change the viewport to the complete window. Then we change the projection matrix and switch back to the viewing matrix which is the standard matrix we normally use.
Procedure reshape(width, height : longint); stdcall;
Var aspect : glFloat;
Begin
glViewport(0, 0, width, height); //change viewport to complete window
glMatrixMode(GL_PROJECTION); //I want to change the projection matrix
glLoadIdentity; //First load the identity matrix
aspect := width / height; //calculate the aspect ratio
glFrustum(-1.0*aspect, 1.0*aspect, 1.0, -1.0, 1.0, 20.0); //and produce our viewing space
glMatrixMode(GL_MODELVIEW); //someone might want to change the viewing matrix, so switch back
glLoadIdentity; //load the identity matrix
glutPostRedisplay; //tell GLUT we want to draw
End;
If you don't understand everything on the first try, don't mind. It will be explained in more detail some further tutorial.
Ok, now we tell GLUT that we have a callback for drawing. Anytime drawing is requested, GLUT will call this function.
Procedure draw; stdcall;
Begin
glClear(GL_COLOR_BUFFER_BIT); //clear the screen or window
glBegin(GL_QUADS); //we want to draw a QUAD
glVertex3f(-0.5, -0.5, -2.0); //top-left corner
glVertex3f(-0.5, 0.5, -2.0); //bottom-left corner
glVertex3f( 0.5, 0.5, -2.0); //bottom-right corner
glVertex3f( 0.5, -0.5, -2.0); //top-right what, yes, corner
glEnd;
glFlush; //tell OpenGL is should empty its command buffer (execute all commands)
glutSwapBuffers; //tell GLUT to actually show the nice white square
End;
You might have noticed the glFlush-Command. OpenGL keeps an internal list of all command you might have issued. Many, if not all, are NOT executed immediatly. That's why we have to tell OpenGL that it has to flush the command buffer, that is, to do all the not-yet done drawing, because we want to show the buffer to someone. Then we can tell GLUT to actually show the buffer, which by now has been filled by OpenGL. Simple as that.
Still, GLUT doesn't know where to find the callback functions. We tell it this (and by the way do some initialization) in the main program.
Begin
glutInit(@argc, argv); //let GLUT do some initialization
glutInitDisplayMode(GLUT_RGB or GLUT_DOUBLE); //make the window highcolor, double buffered
glutCreateWindow('Tutorial 01'); //now create the first window
glutDisplayFunc(@draw); //register callback functions
glutReshapeFunc(@reshape);
glutMainLoop; //and now actually run
End.
That's all for this tutorial. You should now be able to compile and run your first OpenGL demo program. Congratulations.
Back to previous page
|