Supercomputer Applications

The Basic Box Program


Plotting a Box in 3D

The commands we have been using to plot points and lines are already written in a 3-dimensional format. The command glVertex3f( x, y, z ) is already a 3D function, and can be used to draw a box using simple OpenGL primitives. The primitive GL_LINE_STRIP starts drawing a line from an initial vertex, and then connects from there to subsequent vertices as they are called. By comparison, GL_LINES requires vertices to be given in pairs since this primitive expects the endpoints of a segment that needs to be drawn.
The Bottom and Top of the Box - Use GL_LINE_STRIP to draw a square.

  glBegin( GL_LINE_STRIP );

      // Bottom of Box
      glVertex3f(-0.5, -0.5, -0.5);
      glVertex3f(-0.5, -0.5, 0.5);
      glVertex3f(0.5, -0.5, 0.5);
      glVertex3f(0.5, -0.5, -0.5);
      glVertex3f(-0.5, -0.5, -0.5);

      // Top of Box
      glVertex3f(-0.5, 0.5, -0.5);
      glVertex3f(-0.5, 0.5, 0.5);
      glVertex3f(0.5, 0.5, 0.5);
      glVertex3f(0.5, 0.5, -0.5);
      glVertex3f(-0.5, 0.5, -0.5);

  glEnd();

Drawing the sides of the box: - Use GL_LINES to draw each edge.
  glBegin( GL_LINES );  // For the Sides of the Box

     glVertex3f(-0.5, 0.5, -0.5);
     glVertex3f(-0.5, -0.5, -0.5);

     glVertex3f(-0.5, -0.5, 0.5);
     glVertex3f(-0.5, 0.5, 0.5);

     glVertex3f(0.5, -0.5, 0.5);
     glVertex3f(0.5, 0.5, 0.5);

     glVertex3f(0.5, -0.5, -0.5);
     glVertex3f(0.5, 0.5, -0.5);

  glEnd();



Rotating the Box

To appreciate the 3D plot, it is sometimes good to rotate the image to view it from a different angle. The following code shows the use of the glRotatef( d, x, y, z ) command which takes four parameters. The first one is the amount of roatation in degrees, and the other three are for determining the axis of rotation. A call such as glRotatef( 30.0, 0.0, 1.0, 0.0 ), for instance, will rotate 30 degrees around the y-axis since the values for x and z are zero whereas the value for y is 1. The calls to the rotation transformations are preceded by a glPushMatrix() command that changes the point of reference prior to calling the DrawBox() function. The call to glPopMatrix() returns the point of reference to the origial configuration.


   glPushMatrix();

     glRotatef(30.0, 0.0, 0.0, 1.0);
     glRotatef(20.0, 0.0, 1.0, 0.0);
     glRotatef(40.0, 1.0, 0.0, 0.0);

     DrawBox();

   glPopMatrix();


The Actual Program

Check out the code for the following simple program that draws a box. The first view is not rotated, but the second is rotated around each of the axes.

Viewed Straight On


No Rotation
Viewed at an Angle


Rotation
Source Code



Other Details:

The glLoadIdentity() command is essential so that transformation commands such as scaling and rotation are done to the original base rather than one that is already transformed. Without loading the Identity Matrix, some of these commands would be performed on the previous transformation giving unexpected results whenever any interrupt is issued such as resizing or moving the window. Try commenting out the glLoadIdentity() command as well as the glPushMatrix() and glPopMatrix() commands in some of the programs that we have already demonstrated to see the results. You will be amazed at how strangely the program reacts.