Make your own free website on Tripod.com

How to Create a 3D Engine with a Real-Life Example






Contents


 

Disclaimer:   I assume no responsiblity for any harm arising out of the use or misuse of this document.

Introduction

           Believe it or not, if you possess high school algebra and geometry knowledge, and some basic computer programming skills, you have all it takes to create a 3D engine. Most people commonly mistaken 3D engines as a user-revolve-around-the-world system when in fact it is the opposite. 3D engines are an egocentric system that revolves around the user. This treatment greatly simplifies the mechanical calculations needed to achieve three-dimensionality on a two-dimensional display. Having said that, let us begin to dissect the 3D engine and rebuild what we will have learnt to create one.

The Three Basic Processes of Creating 3D Graphics

           A 3D engine takes a set of points, the vertices of triangles, and transforms and translates them into a new set of points. Then, it's up to the program to display this new set of information on the computer screen. Thus, there are three basic processes involved in 3D applications: the transformation process, the translation process, and the display process. Most 3D engines incorporate the display process. Hence, the program only needs to tell the 3D engine how to transform and translate the vertices. The 3D engine will take care of the rest.

           Now, one may ask why the vertices of triangles are used in 3D engines. Why not rectangles or pentagons? The quick answer is that rectangles or pentagons do not form spheres very well, or curved objects for that matter. Remember, triangles are easier to shade in, and a square is easily two isosceles triangles connected together.  However, some 3D programs may use a combination of rectangles and triangles, such as computer-assisted design (CAD) programs.

The Transformation Process

           In an egocentric system, the world revolves around the user. This is exactly what the 3D engine does when transforming a vertex into a new one. When one turns to the right, the 3D engine simply does the opposite. It rotates the world to the left with the position of the user as the center of rotation. This is the transformation process and it encompasses transformations such as rotations, projections, mirroring, stretches, etc. For our purposes, we will deal only with rotation, as this is the most common transformation done by 3D engines.

           There are two ways to rotate a given 3D object. One way involves simultaneous rotation about the x, y, and z-axes. The other way involves sequential rotations, first, about the x-axis, then about the y-axis, and then about the z-axis. As with anything in real life, there are advantages and disadvantages associated with each of the two methods. We will give the final equations for each method and discuss their advantages and disadvantages below (derivations may be found elsewhere in the references section):

           The first method encompasses complete degrees of rotations* (we will discuss this more in the next paragraph). However, it is not very easy to discern the rotational angles when one wishes to animate complex tilted rotations.

           The second method is easier to deal with, as the angles of rotation are more discernable when one wishes to animate complex tilted rotations or the like. However, the major set back of this method is that some angles of rotations cannot be achieved without additional explicit routines. For example, if one were do to a rotation about the axes by these degree amounts: 2 degrees about the x-axis followed by 3 degrees about the y-axis followed by 5 degrees about the z-axis, one could get different results if the rotation about the y-axis was done before rotation about the z-axis, and vice versa. In short, the rotations already done to the vertices will affect the next one and the next one. Hence, the short fall of the second method is that not all rotations are possible without additional explicit routines.

           It is up to the creator to decide which method best suits his/her purpose. The first technique is more applicable to mouse-based type movements, while the second technique is more applicable to static displays, or CAD-type programs. The author chose to use the second method in his sample Crystal 3D Engine as the unobtainable angles did not interfere with the demonstrability of this 3D Engine.

The Translation Process

           The translation process is by far the easiest step and should be covered first.  However, 3D engines usually translate the world only after it has been transformed as follows.  When one walks forward, the 3D engine simply shifts the entire world backward. This is the translation process. To translate a given polygon to a new position, one simply adds or subtracts values from all the vertices of the polygon.

Displaying 3D Graphics on a 2D Screen

           Once the old vertices have been transformed and translated to new ones, it is time to display this new information on the screen. To convert 3D graphics to a 2D one, simply divide the x and y values of each vertex by their corresponding z value. This has the effect of decreasing the x and y values if the vertex was far away from the viewer, and increasing the x and y values if the vertex was up close to the user.  The figure below is that of a polygon.  However, it applies equally well to the single vertices of a polygon.

           The sets of 2D coordinates can now be rendered. Rendering polygons is beyond the scope and focus of this article.

            Let’s not forget one last important point.  At some point, the object may be ridiculously close to the viewer or behind the viewer.  We must specify the z-value above which the object will no longer be drawn on the screen, or the “un-view” value.

Putting It All Together

           To construct a 3D engine and hence the accompanying 3D application, we first have a routine where the vertices of the 3D models and the 3D world environment are read from files into memory. We preferably have the vertices within the files read into a matrix or an array of numbers. Then, we have a program loop where the user inputs are caught, telling the program the degrees of rotations or amount of translation to mirror. We then proceed to transform or translate the vertices already in memory according to one of two methods mentioned earlier. We put the transformed vertices into a second matrix, which we will operate on by dividing the x, and y values by the z values to obtain the third matrix, a two-dimensional matrix. We then display the 2-dimensional matrix accordingly, and go back to the program loop to await the next user input. This is pretty much the nuts and bolts to creating a 3D engine.

Common 3D-ing Mistakes

           The nasty overflow error occurs when the object is within the displayable z-values, but is on the far right of the screen, far left of the screen, or above or below the screen. To correct for this problem, have a routine where the polygons are drawn only if they are within the compiler limit.

           The looking up and down routine is a common source of frustration for many 3D engine creators. Never have an independent subroutine within the main transformation routine where two axes of rotation are used to generate the desired looking up and down angles. In the long run, the horizontal image will be slanted because one cannot restore the original angle simply by subtracting sine or cosine. The slanting is due to rounding up error in sine and cosine, inevitably coming from the round up of pi. The correct thing to do is to include a second transformation process after all else, where all points that have already been transformed will be transformed again via a rotation about the horizontal axis to generate the desired looking up or down angle.

Real Life Example: Crystal 3D Engine



NEW!!! Get StarFox3D WOW Room Final NEW!!!
More Actors, More Special Effects. Optimized for SPY KIDS 3D Glasses.
(08-26-2003)

Click here to download the originial Crystal 3D Engine (~200Kb)

This is a basic wire frame engine with walk-around capability, easy to edit work-frame, and a true anaglyphic mode with perfective boost (objects do go into and out of the screen). The algorithms built into this engine include perfect random restoring rotations, wait & blast off routine, hover, and rapid repeat translations.

This program was written and compiled in QuickBasic Extended version. The source code is included with this program.

References

·         Web sites:

 

Article written, and Crystal 3D Engine created by Uy Tran, 2003.