Example: Gouraud-Shaded Tunnel

The Tunnel program draws a 20x40x100 rectangular Gouraud-shaded tunnel. Through keyboard controls, you can move back and forth through the tunnel, or rotate your viewpoint up to 360 degrees. Through keyboard controls, you can move back and forth through the tunnel, or rotate your viewpoint up to 360 degrees. The up and down arrow keys move the viewer forward or backward, while the left and right arrow keys turn the viewer to the left or right in 6 increments. Tunnel uses a 640x480 virtual buffer whose color depth is the same as the display color depth. However, if a 256-color display is active, Tunnel will use a high color virtual buffer because Fastgraph does not support Gouraud shading for 256-color virtual buffers.

The tunnel is defined in world space and is 100 units long (from z=100 to z=200), 40 units high, (with the floor at y=0), and 20 units wide (extending from x=-10 to x=10). We define the initial POV at (0,10,50) with no rotation, which places us outside the tunnel with a reasonable eye level (we don't want to be looking right at the tunnel floor). The initial view looks like this:

Tunnel's CheckForMovement() function is a scaled-down version of the keystroke processing function found in the other 3D examples. CheckForMovement() first tests for POV movement. The movement occurs in 2-unit increments, so if the up arrow is pressed we call fg_3Dmoveforward(2.0) to move forward, or if the down arrow is pressed, fg_3Dmoveforward(-2.0) to move backward. CheckForMovement() next checks for POV rotation. The rotation occurs in 6 increments, so if the right arrow is pressed we call fg_3Drotateright(60) to turn right, or if the left arrow is pressed, fg_3Drotateright(-60) to turn left. When we modify the POV, we set Redraw to TRUE so CheckForMovement() will draw the updated tunnel.

If the POV changes in the current frame, CheckForMovement() then redraws the tunnel to reflect the new POV. When this occurs, we first fill the virtual buffer with white pixels to erase the previous frame. Next we call the program's DrawTunnel() function to draw the resulting tunnel. Finally, we call fg_vbscale() to display what we just drew, and set Redraw to FALSE in preparation for the next frame.

The Tunnel program's DrawTunnel() function simply calls fg_3Dshade() for each of the tunnel's four sides. The FacesRGB array passed to fg_3Dshade() contains the RGB values corresponding to each polygon vertex. We chose RGB values that would make the tunnel's near end (at z=100) lighter than its far end.

C/C++ version

C++Builder version

Delphi version

Visual Basic version

<< Prev

Next >>

Fastgraph Home Page


copyright 2001 Ted Gruber Software, Inc.