3D Programming, Step-By-Step
|Top Previous Next|
This section walks you through the concepts of 3D programming, step by step.
Writing A 3D Program. Writing a 3D graphics program requires five basic steps:
1. Declare A Mesh Handle. Most of the time you will be creating meshes to create a 3D object. Before you can do this, you need to declare a mesh handle that will be used to control that mesh once it has been created. Handles are simply integer variables that hold the number of the handle. Normally you would declare a handle variable near the beginning of the source file because it will probably be global to most of the code. Here is an example of declaring a mesh handle:
2. Create A Mesh Object. Now that we have a variable to hold the handle, we need to actually get that handle. The following code gets a handle, and puts it into the handle variable:
The first argument, -1, sets the parent of the object to the base frame. The second argument, 1, specifies that we want a handle to a mesh. The final argument gives the object an internal name that can be used for identifying the object in some situations.
3. Set Up The 3D Window. In order to view 3D graphics in EXPL, you must configure the 3D graphics window and enable it. Here is an example of configuring the 3D window:
This example places the upper-left corner of the window at 200, 10 and the width and height at 430, 380. Once you've positioned and sized the window, you need to enable it. The following code enables the window:
Once you have enabled the window, all 3D graphics will appear within the confines of the window.
4. Add Triangles. 3D objects are constructed from a series of interconnected triangles. Once you have a handle for a mesh, you can add as many triangles as needed to construct the object you want. For example, this line of code adds one triangle to the mesh:
Add3DTriangle(WrkMesh,-1.0,-1.0,-1.0, -1.0, 1.0, 1.0, 1.0, 1.0,-1.0, $FF0000FF);
The first argument specifies the handle for the mesh we want to add the triangle to. The next nine arguments are the X, Y and Z coordinates for the three points that define the triangle. The final argument specifies the color of the triangle.
5. Scale Mesh. 3D objects can be virtually any size ranging from microscopic to the size of the solar system. For this reason, the program cannot anticipate the size of the object, and the object may be too small to be seen or too large to fit in the window. For this reason, EXPL has an intrinsic that automatically scales the object to fit the screen. The following code scales the object to fit on the screen:
The first argument specifies the object to be scaled. In this case we are using -2, which specifies that the whole scene should be scaled. The second argument specifies the type of scaling. In this case we scaling by moving the camera in our out so the whole scene fits in the window.
Refer to the Templates section for more detailed example programs and code fragments.