Open GL Super Bible

Previous Table of Contents Next


Our First Example

The code shown in Listing 6-2 draws some points in our 3D environment. It uses some simple trigonometry to draw a series of points that form a corkscrew path up the z-axis. This code is from the POINTS program, which is on the CD in the subdirectory for this chapter. All of the example programs use the framework we established in Chapters 4 and 5. Notice that in the SetupRC() function we are setting the current drawing color to green.

Listing 6-2 Rendering code to produce a spring-shaped path of points

// Define a constant for the value of PI
#define GL_PI 3.1415f

// This function does any needed initialization on the rendering
// context.
void SetupRC()
        {
        // Black background
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f );

        // Set drawing color to green
        glColor3f(0.0f, 1.0f, 0.0f);
        }

// Called to draw scene
void RenderScene(void)
        {
        GLfloat x,y,z,angle; // Storage for coordinates and angles

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

        // Save matrix state and do the rotation
        glPushMatrix();
        glRotatef(xRot, 1.0f, 0.0f, 0.0f);
        glRotatef(yRot, 0.0f, 1.0f, 0.0f);

        // Call only once for all remaining points
        glBegin(GL_POINTS);

        z = -50.0f;
        for(angle = 0.0f; angle <= (2.0f*GL_PI)*3.0f; angle += 0.1f)
                {
                x = 50.0f*sin(angle);
                y = 50.0f*cos(angle); 
  
                // Specify the point and move the Z value up a little
                glVertex3f(x, y, z);
                z += 0.5f;
                }

        // Done drawing points
        glEnd();

        // Restore transformations
        glPopMatrix();

        // Flush drawing commands
        glFlush();
        }

Only the code between calls to glBegin and glEnd is important for our purpose in this and the other examples for this chapter. This code calculates the x and y coordinates for an angle that spins between 0? and 360? three times. (We express this programmatically in radians rather than degrees; if you don’t know trigonometry, you can take our word for it. If you’re interested, see the box, “The Trigonometry of Radians/Degrees.” Each time a point is drawn, the z value is increased slightly. When this program is run, all you will see is a circle of points, because you are initially looking directly down the z-axis. To better see the effect, use the arrow keys to spin the drawing around the x- and y-axes. This is illustrated in Figure 6-3.


Figure 6-3  Output from the POINTS sample program


One Thing at a Time
Again, don’t get too distracted by the functions in this sample that we haven’t covered yet (glPushMatrix, glPopMatrix, and glRotate). These functions are used to rotate the image around so you can better see the positioning of the points as they are drawn in 3D space. We will be covering these in some detail in Chapter 7. If we hadn’t used these features now, you wouldn’t be able to see the effects of your 3D drawings, and this and the following sample programs wouldn’t be very interesting to look at. For the rest of the sample code in this chapter, we will only be showing the code that includes the glBegin and glEnd statements.


The Trigonometry of Radians/Degrees
The figure in this box shows a circle drawn in the xy plane. A line segment from the origin (0,0) to any point on the circle will make an angle (a) with the x-axis. For any given angle, the trigonometric functions Sine and Cosine will return the x and y values of the point on the circle. By stepping a variable that represents the angle all the way around the origin, we can calculate all the points on the circle. Note that the C runtime functions sin() and cos() accept angle values measured in radians instead of degrees. There are 2*PI radians in a circle, where PI is a nonrational number that is approximately 3.1415 (nonrational means there are an infinite number of values past the decimal point).

Setting the Point Size

When you draw a single point, the size of the point is one pixel by default. You can change this with the function glPointSize.

void glPointSize(GLfloat size);

The glPointSize function takes a single parameter that specifies the approximate diameter in pixels of the point drawn. Not all point sizes are supported, however, and you should check to make sure the point size you specify is available. Use the following code to get the range of point sizes, and the smallest interval between them:

GLfloat sizes[2];        // Store supported point size range
GLfloat step;            // Store supported point size increments

// Get supported point size range and step size
glGetFloatv(GL_POINT_SIZE_RANGE,sizes);
glGetFloatv(GL_POINT_SIZE_GRANULARITY,&step);

Here the sizes array will contain two elements that contain the smallest and the largest valid value for glPointsize. In addition, the variable step will hold the smallest step size allowable between the point sizes. The OpenGL specification only requires that one point size, 1.0, be supported. The Microsoft implementation of OpenGL allows for point sizes from 0.5 to 10.0, with 0.125 the smallest step size. Specifying a size out of range will not be interpreted as an error. Instead, the largest or smallest supported size will be used, whichever is closest to the value specified.


OpenGL State Variables
OpenGL maintains the state of many of its internal variables and settings. This collection of settings is called the OpenGL State Machine. The State Machine can be queried to determine the state of any of its variables and settings. Any feature or capability you enable or disable with glEnable/glDisable, as well as numeric settings set with glSet, can be queried with the many variations of glGet. Chapter 14 explores the OpenGL State Machine more completely.

Let’s look at a sample that makes use of these new functions. The code shown in Listing 6-3 produces the same spiral shape as our first example, but this time the point sizes are gradually increased from the smallest valid size to the largest valid size. This example is from the program POINTSZ in the CD subdirectory for this chapter. The output from POINTSZ is shown in Figure 6-4.


Figure 6-4  Output from POINTSZ program


Previous Table of Contents Next