I. Introduction To OpenGL

OK, I assume if you're still here then you're hyped up to begin learning OpenGL. First off, you have to get the libraries…

OpenGL should be availible on whatever platform you happen to be on. The libraries and headers for Microsoft's version comes with Visual C++, or you can download them from Microsoft's site. Alternativly, you can hunt around for SGI's drivers at http://www.sgi.com/. If you're running Linux, I'd suggest getting Mesa. It's availible for free at sunsite (ftp://sunsite.unc.edu/pub/packages/development/graphics/mesa/). Whichever OGL library you get, you're also going to need some sort of windowing system to support it. For these examples I'm going to be using the GLUT, or OpenGL Utility Toolkit (ftp://sunsite.unc.edu/pub/packages/development/graphics/glut/). GLUT's main job is to provide a window to draw your OpenGL code into. GLUT is also portable, which means you can compile the same code for both Win95 and X-Windows.

Now that that's out of the way, lets go on to:

/**********************************************************************/ 

/******************************************/ 

/* A Very Simple OpenGL Example! */ 

/******************************************/

/* this code just creates a window and draws a rectangle in it */

#include  /* obviously change this to your native library if you're compiling under unix */ 

#include  

#include 


void init(void); 

void display(void);


int main (int argc, char **argv) {

 glutInit(&argc, argv);

 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

 glutInitWindowSize(250, 250);

 glutInitWindowPosition(100, 100);

 glutCreateWindow("My First OpenGL Application");

 init();

 glutDisplayFunc(display);

 glutMainLoop();

 return 0; 

}


void init(void) {

 glClearColor(0.0, 0.0, 0.0, 0.0);

 glColor3f(0.0, 0.0, 1.0);

 glMatrixMode(GL_PROJECTION);

 glLoadIdentity();

 glOrtho(-10.0, 10.0, –10.0, 10.0, –10.0, 10.0); 

}


void display(void) {

 glClear(GL_COLOR_BUFFER_BIT);

 glRectf(-5.0, 5.0, 5.0, –5.0);

 glutSwapBuffers(); 

} 

/**********************************************************************/

Lets go through this step by step:

First, I called the glutInit function, which provides general initialization for the GLUT library:

int main (int argc, char **argv) {

 glutInit(&argc, argv);

Next I initialize the display mode:

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 

This tells OpenGL that I want a double buffered window, with RGB color.

The next few lines create the window:

glutInitWindowSize(250, 250);

glutInitWindowPosition(100, 100);

glutCreateWindow("My First OpenGL Application"); 

As you can see, with GLUT this is fairly straightforward.

Now it's time to initialize OpenGL. Here's the code:

void init(void) {

 glClearColor(0.0, 0.0, 0.0, 0.0); /* set the background (clearing) color to RGB(0,0,0) – black */

 glColor3f(0.0, 0.0, 1.0); /* set the foreground color to blue */

 glMatrixMode(GL_PROJECTION); /* Initialize the matrix state */

 glLoadIdentity();

 glOrtho(-10.0, 10.0, –10.0, 10.0, –10.0, 10.0);

}

In OpenGL, matrices are used to manage the view. There are two matrix modes, projection and modelview. Projection is used to set up the viewport and clipping boundry, while modelview is used to rotate, translate and scale objects quickly.

Lets take a look at these two lines:

glLoadIdentity();

glOrtho(-10.0, 10.0, –10.0, 10.0, –10.0, 10.0); 

glLoadIdentity() loads the identity matrix into the current matrix state (in this case the projection matrix). You can consider this the resetting matrix…it resets everything back to zero. Next comes the call to glOrtho. This function sets up a clipping volume. You can think of a clipping volume as a box in which your drawing commands are rendered. As the viewer, we are positioned outside the box, looking in the front. What we see is whatever is inside the box, projected onto the flat surface that is the side. Anything outside the box is invisible. The glOrtho function creates an orthographic view-that is, one with no perspective. We'll get to perspective drawing later in the tutorial.

The arguments for glOrtho are as follows:

void glOrtho(double left, double right, double bottom, double top, double near, double far);

Now, lets continue with the application:

glutDisplayFunc(display);

glutMainLoop(); 

The first function sets the function that GLUT will use whenever it needs to update the view. We then call glutMainLoop() which actually runs the program. From this point on our work is done; GLUT will handle the details of managing the window and calling our painting function to display it.

Here is the display function again:

void display(void) {

 glClear(GL_COLOR_BUFFER_BIT);

 glRectf(-5.0, 5.0, 5.0, –5.0);

 glutSwapBuffers();

} 

The first thing we do is call glClear with GL_COLOR_BUFFER_BIT parameter. This will clear the window with the color we specified earlier using glClearColor. Next, we actually draw our rectangle, using the glRectf function.

Now's the time I should bring up something about OpenGL function calls – they often come in different forms. For example, take glColor, which sets the current foreground color. This function has several prototypes, for example:

glColor3f(float, float, float); 

glColor4f(float, float, float, float); 

glColor3d(double, double, double); 

glColor3i(int, int, int); 

etc, etc, etc.

As you can see, OpenGL functions are usually formatted like this:

gl Color 3 f

However, even though most OpenGL functions can accept any type, it's usually best to pass floats. OpenGL uses floating point values for all its internal calculations, so passing any other type is a waste of time since OGL will just convert it back to floating point anyway.

The next function, glutSwapBuffers(), swaps the back buffer to the screen buffer. In double-buffered mode, the drawing commands do not draw to the screen. Rather, they draw to an offscreen buffer. Once you are done drawing, you copy the entire back buffer to the screen at once, thus producing smooth animation. Of course, in this simple example there is no animation, but without a double buffer even moving the window around the screen will cause the rectangle to flicker. Besides, it's good to get into the habit of producing smooth graphics!

OK, now feel free to copy this program into an editor, compile it, and run it. Note you will have to link with whatever libs your version of OpenGL has provided, as well as the GLUT library. Under linux you will also probably have to link the X Window libraries such as libX11.a.

Congratulations! You are now an OpenGL Programmer! But I know what you're

saying…"Where are the 3D graphics? You think I'm reading this junk to learn

how to draw a stupid square?" Well, since you're so impatient, lets move on to creating 3D worlds with OpenGL…

Загрузка...