Open GL Super Bible

Previous Table of Contents Next


A Nuclear Example

Letís put to use what we have learned. In the next example, we will build a crude, animated model of an atom. This atom will have a single sphere at the center to represent the nucleus, and three electrons in orbit about the atom. Here weíll use an orthographic projection, as we have previously in this book. (Some other interesting projections are covered in the upcoming section, ďUsing Projections.Ē)

Our ATOM program uses a timer to move the electrons four times a second (undoubtedly much slower than any real electrons!). Each time the Render function is called, the angle of revolution about the nucleus is incremented. Also, each electron lies in a different plane. Listing 7-1 shows the Render function for this example, and the output from the ATOM program is shown in Figure 7-17.


Figure 7-17  Output from the ATOM example program

Listing 7-1 Render function from ATOM example program

// Called to draw scene
void RenderScene(void)
        {
        // Angle of revolution around the nucleus
        static float fElect1 = 0.0f;

        // Clear the window with current clearing color
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // Reset the modelview matrix
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        // Translate the whole scene out and into view
        // This is the initial viewing transformation
        glTranslatef(0.0f, 0.0f, -100.0f);

        // Red Nucleus
        glRGB(255, 0, 0);
        auxSolidSphere(10.0f);

        // Yellow Electrons
        glRGB(255,255,0);

        // First Electron Orbit
        // Save viewing transformation
        glPushMatrix();

        // Rotate by angle of revolution
        glRotatef(fElect1, 0.0f, 1.0f, 0.0f);

        // Translate out from origin to orbit distance
        glTranslatef(90.0f, 0.0f, 0.0f);

        // Draw the electron
        auxSolidSphere(6.0f);

        // Restore the viewing transformation
        glPopMatrix();

        // Second Electron Orbit
        glPushMatrix();
        glRotatef(45.0f, 0.0f, 0.0f, 1.0f);
        glRotatef(fElect1, 0.0f, 1.0f, 0.0f);
        glTranslatef(-70.0f, 0.0f, 0.0f);
        auxSolidSphere(6.0f);
        glPopMatrix();

        // Third Electron Orbit
        glPushMatrix();
        glRotatef(360.0f, -45.0f, 0.0f, 0.0f, 1.0f);
        glRotatef(fElect1, 0.0f, 1.0f, 0.0f);
        glTranslatef(0.0f, 0.0f, 60.0f);
        auxSolidSphere(6.0f);
        glPopMatrix();

        // Increment the angle of revolution
        fElect1 += 10.0f;
        if(fElect1 > 360.0f)
        fElect1 = 0.0f;

        // Flush drawing commands
        glFlush();
        }

Letís examine the code for placing one of the electrons, a couple of lines at a time. The first line saves the current Modelview matrix by pushing the current transformation on the stack:

// First Electron Orbit
// Save viewing transformation
glPushMatrix();

Now the coordinate system is rotated around the y axis by an angle fElect1:

// Rotate by angle of revolution
glRotatef(fElect1, 0.0f, 1.0f, 0.0f);

Now the electron is drawn by translating down the newly rotated coordinate system:

// Translate out from origin to orbit distance
glTranslatef(90.0f, 0.0f, 0.0f);

Then the electron is drawn (as a solid sphere), and we restore the Modelview matrix by popping it off the matrix stack:

// Draw the electron
auxSolidSphere(6.0f);

// Restore the viewing transformation
glPopMatrix();

The other electrons are placed similarly.

Using Projections

In our examples so far we have used the Modelview matrix to position our vantage point of the viewing volume and to place our objects therein. The Projection matrix actually specifies the size and shape of our viewing volume.

Thus far in this book, we have created a simple parallel viewing volume using the function glOrtho, setting the near and far, left and right, and top and bottom clipping coordinates. When the Projection matrix is loaded with the Identity matrix, the diagonal line of 1ís specifies that the clipping planes extend from the origin to positive 1 in all directions. The projection matrix does no scaling or perspective adjustments. As you will soon see, there are some alternatives to this approach.

Orthographic Projections

An orthographic projection, used for most of this book thus far, is square on all sides. The logical width is equal at the front, back, top, bottom, left, and right sides. This produces a parallel projection, which is useful for drawings of specific objects that do not have any foreshortening when viewed from a distance. This is good for CAD or architectural drawings, for which you want to represent the exact dimensions and measurements on screen.

Figure 7-18 shows the output from the example program ORTHO on the CD in this chapterís subdirectory. To produce this hollow, tube-like box, we used an orthographic projection just as we did for all our previous examples. Figure 7-19 shows the same box rotated more to the side so you can see how long it actually is.


Figure 7-18  A hollow square tube shown with an orthographic projection


Figure 7-19  A side view showing the length of the square tube

In Figure 7-20, youíre looking directly down the barrel of the tube. Because the tube does not converge in the distance, this is not an entirely accurate view of how such a tube would appear in real life. To add some perspective, we use a perspective projection.


Figure 7-20  Looking down the barrel of the tube


Previous Table of Contents Next