Open GL Super Bible

Previous Table of Contents Next


The Rendering Function

Next, you’ll see we have created the function RenderScene().

// Called by AUX library to draw scene
void CALLBACK RenderScene(void)
        {
        ...
        ...

This is where we have moved all code that does the actual drawing in the window. The process of drawing with OpenGL is often referred to as rendering, so we used that descriptive name. In later examples we’ll be putting most of our drawing code in this function.

Make note of the CALLBACK statement in the function declaration. This is required because we’re going to tell the AUX library to call this function whenever the window needs updating. Callback functions are simply functions that you write, which the AUX library will be calling in your behalf. You’ll see how this works later.

Drawing a Rectangle

Previously, all our program did was clear the screen. We’ve added the following two lines of drawing code:

// Set current drawing color to red
//       R       G       B
glColor3f(1.0f, 0.0f, 0.0f);

// Draw a filled rectangle with current color
glRectf(100.0f, 150.0f, 150.0f, 100.0f);

These lines set the color used for future drawing operations (lines and filling) with the call to glColor3f(). Then glRectf() draws a filled rectangle.

The glColor3f() function selects a color in the same manner as glClearColor(), but no alpha translucency component needs to be specified:

void glColor3f(GLfloat red, GLfloat green, GLfloat blue);

The glRectf () function takes floating point arguments, as denoted by the trailing f. The number of arguments is not used in the function name because all glRect variations take four arguments. The four arguments of glRectf(),

void glRectf(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2);

represent two coordinate pairs—(x1, y1) and (x2, y2). The first pair represents the upper-left corner of the rectangle, and the second pair represents the lower-right corner. See Figure 3-4 if you need a review of OpenGL coordinate mapping.

Initialization

The main body of friendly.c starts the same way as our first example:

void main(void)
        {
        // AUX library window and mode setup
        auxInitDisplayMode(AUX_SINGLE | AUX_RGBA);
        auxInitPosition(100,100,250,250);
        auxInitWindow("My second OpenGL Program");

        // Set function to call when window needs updating
        auxMainLoop(RenderScene);
        }

As before, the three auxInitxxx calls set up and display the window in which we’ll be drawing. In the final line, auxMainLoop() takes the name of the function that does the drawing, RenderScene(). The AUX library’s auxMainLoop() function simply keeps the program going until it’s terminated by closing the window. This function’s single argument is a pointer to another function it should call whenever the window needs updating. This callback function will be called when the window is first displayed, when the window is moved or resized, and when the window is uncovered by some other window.

// Called by AUX library to draw scene
void CALLBACK RenderScene(void)
        {
        // Set clear color to Blue
        glClearColor(0.0f, 0.0f, 1.0f, 1.0f);

        // Clear the window
        glClear(GL_COLOR_BUFFER_BIT);

        // Set current drawing color to red
        //                 R     G        B
        glColor3f(1.0f, 0.0f, 0.0f);

        // Draw a filled rectangle with current color
        glRectf(100.0f, 150.0f, 150.0f, 100.0f);

        glFlush();
        }

At this point, the program will display a red square in the middle of a blue window, because we used fixed locations for the square. If you make the window larger, the square will remain in the lower-left corner of the window. When you make the window smaller, the square may no longer fit in the client area. This is because as you resize the window, the screen extents of the window change; however, the drawing code continues to place the rectangle at (100, 150, 150, 100). In the original window this was directly in the center; in a larger window these coordinates are located in the lower-left corner. See Figure 3-5.


Figure 3-5  Effects of changing window size

Scaling to the Window

In nearly all windowing environments, the user may at any time change the size and dimensions of the window. When this happens, the window usually responds by redrawing its contents, taking into consideration the window’s new dimensions. Sometimes you may wish to simply clip the drawing for smaller windows, or display the entire drawing at its original size in a larger window. For our purposes, we usually will want to scale the drawing to fit within the window, regardless of the size of the drawing or window. Thus a very small window would have a complete but very small drawing, and a larger window would have a similar but larger drawing. You see this in most drawing programs when you stretch a window as opposed to enlarging the drawing. Stretching a window usually doesn’t change the drawing size, but magnifying the image will make it grow.


Previous Table of Contents Next