Contents:
This week, we will walk through an introductory OpenGL program. It will be necessary to discuss some 2d graphical concepts before we analyze the code.
I have put the example program, as well as the Windows 95 DLLs and libraries, on my FTP site. You can download them by clicking the links below. You'll need WinZip to unZIP the files afterwards.
Download the Win95 DLLs & Libraries
There are three subdirectories here, once you unZIP this file. Text files README.1st and README.TXT tell you what to do with the contents. NOTE: If you look in your :\WINDOWS\SYSTEM file and find Glu32.dll and Opengl32.dll, you may already have OpenGL installed. I found these DLL's in my Windows 95 installation, and suspect they came with the system.
Download the Graph2D Example Program
To compile this program using Visual C++, simply open the Graph2d directory and find Graph2d.mak. This will open up a new Project pseudo-directory. Then pull down the PROJECT directory, compile and execute the program. (I did this under C++ Version 2.0. I do not yet know if it will work under 1.5 or other versions. Please let me know.)
Reference Books for OpenGL
I'm using two of them. The first, 3D Graphics Programming with OpenGL, focuses exclusively on the Windows and C++ environment. (Clayton Walnum, Que books 1995, $45.00. I got it at Books-A-Million.) The Graph2D example above is from this book. It comes with a diskette containing all the example code.
The second, OpenGL SuperBible, is a brand new publication. It comes with a CD containing the WIN95 DLLs and libraries, and all the example code. (R. S. Wright and M. Sweet, Waite Group Press 1996.) This book has very clear explanations of graphical principles but does not walk you through the process of building OpenGL apps in Visual C++ as nicely as the first book does. The SuperBible relies on the AUX library in its first chapters; its "Shortest OpenGL" program is presented below.
We will need to understand how to construct homogenous matrices
for two dimensional scaling, translation and rotation. This material
is covered well and clearly, in the FVD text on pages 201-210.
Time does not permit me to reproduce this material in these notes.
Here are the essential ideas:
1. A graphical object can be represented as a set of points (which can define lines, which in turn can serve to define polygons, solid polyhedra, curved surfaces, etc.)
2. Three important transformations (changes of data) are translation ("sliding"), uniform scaling ("growing and shrinking") and rotation.
3. We can represent transformations by two dimensional matrices which are represented by arrays of numbers. We can represent points by vectors which are columns of numbers.
4. Multiplying a vector by a matrix produces a new vector which is the transformed result, as if the corresponding point had been moved in the same way as the matrix's corresponding transformation.
5. The plural of matrix is matrices. Just thought you'd like to know.
6. Two or more transformations, such as a translation and a rotation, can be combined together before applying them to data. This can be a VERY economical thing to do, instead of applying the transformations separately to the same data set.
7. We always keep the original data describing an object. Cumulative multiplication errors can corrupt an object's description.
A1.
Go read the FVD section and be ready to work problems. Construct
a single transformation matrix that has the effect of rotating
a data object 45 degrees clockwise about the point (4,5).
OpenGL Examples
Actually, we are equipped with TWO examples. The first one is so small that I didn't bother putting it up on FTP. And this small example doesn't require the use of C++. It can be compiled in plain old C. It uses the AUX library, which is full of surprisingly rich primitives that help to get started. (There's a procedure called AuxWireTeapot that draws the famous graphical teapot...)
// shortest.c
// The shortest OpenGL program possible, so says the SuperBible
# include <windows.h> // standard Window header
# include <conio.h> // console i/o functions
# include <gl\gl.h> // openGL functions
# include <gl\glaux.h> // AUX Library functions
void main (void)
{
// these are the AUX functions to set up the window
auxInitDisplayMode(AUX_SINGLE | AUX_RGBA);
auxInitPosition(100,100,250,250);
auxInitWindow("My first OpenGL Program");
// These are the OpenGL functions that do something in the
window
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
// stop and wait for a keypress;
cprintf("Press any key to close the window\n");
getch();
}
The second example is considerably more complex, as it creates
a dialog object. I have provided you with the source code for
this example, and we will discuss it in class from overhead foils.
A2.
Get at least one of the programs ("shortest.c" or "Graph2d")
working on your computer, or on one of the laboratory machines,
by next Monday's class. If you are having trouble getting lab
access due to police or key problems, don't feel too badly. Just
dash out and buy a Pentium Pro ... :)