Real-Time 3D Rendering in XPL0

Here are some programs that show how XPL0 can be used to animate a 3D object. Each one builds upon the preceding example. All the source code and executables are contained here: TORUS.ZIP (210k, 3-Jul-2006). You need to run the programs to appreciate the speed that the images are drawn. They run under both DOS and Windows.

All the programs can be terminated by pressing the Esc key. Some programs depend on the file texture.bmp, which prevents them from being run directly from the zip file under Windows. You'll need to extract the files to a folder.

The first example is boiled down to a mere two pages of code. Sines and cosines define a series of points on the surface of a torus. The points are connected with lines to form a wire mesh. Difference equations rotate the points about the X, Y and Z axes. If you run Torus1.exe for a minute or two, you'll see something wrong with this simple approach.

Torus2 improves on Torus1 by making it interactive. The mouse rotates a reference frame about the X and Y axes. Each vertex in the torus is aligned with the reference frame by multiplying it by a rotation matrix. The number of facets can be adjusted with the arrow keys.

Torus3 increases realism by using solid surfaces. 32-bit XPL0 with its VESA graphics provides a substantial increase in speed over 16-bit XPL0. An image buffer eliminates flicker and enables the use of the Painter's Algorithm. This draws facets into the buffer starting with the farthest one. Nearer facets get painted over some of the farther ones.

Torus4 shines some light on the subject. Instead of the facets emitting light they are illuminated with directional and ambient light. The brightness of the reflected light is determined by a facet's orientation. Vectors perpendicular to each facet (called "normal" vectors) are used to determine the orientation and thus the brightness. Normal vectors are also used to eliminate drawing facets that face away from the viewer, which speeds things up. Speed is a constant concern and is monitored by displaying "Frames/second".

Torus5 uses Gouraud shading to smooth the surface without resorting to lots of facets. Normal vectors are added at the corners of each facet and determine the brightness at the vertices. Brightness is interpolated across the surface of a facet, which results in a smooth gradient. Gouraud shading only slows the code slightly. This version also adds panning and zooming, which utilize clipping and scaling.

Torus6 uses texture mapping to convert the torus into a donut. The facets are drawn using interpolation to step through a texture image. This selects the colors of the pixels to be plotted. The brightness of the colors is modified by a look-up table, which results in a shaded 3D image. Either Gouraud or flat shading can be selected. (Gouraud shading is shown.)

Torus7 adds a shadow and a background. The tablecloth is a tiling of squares in the X-Y plane. This provides a simple object for the shadow to be projected upon. Ambient light illuminates the shaded area.

A great reference is: Real-Time Rendering.

Back to the XPL0 home page

Last updated: 7-Jul-2006