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.



4 из 19