PS2 Linux Programming
A 3D Model Loader
Introduction
In this tutorial a simple
3D model format will be used to load and display a textured 3D model of a cube.
It has been seen from
previous tutorials that in order to draw objects on screen, information is
required about the vertices of the object. Typical information is the position,
colour and texture coordinates of all the vertices in the model. Hand coding
this information is possible when the model has only a few vertices but this
becomes impractical for models of any size.
A 3D modelling package is
used to create models and write the vertex information out to a file. This file
can then be read by the rendering program and the model can be displayed and
manipulated on screen.
There are many 3D
modelling packages such as 3D Studio Max and Maya, but as well as being very
expensive to purchase they can also be complex to use, offering much more
functionality than is normally required for making a games. There are also very
many different 3D modelling file formats that are used to represent the
vertices (and other information such as animation details) of a model, but once
again some formats are difficult to understand and complex to use.
As an introduction to
using 3D models, the MilkShape 3D modelling package has been used to create a
simple model in the MilkShape 3D ASCII format. MilkShape 3D can be found at http://www.milkshape3d.com/ and the
ASCII format is relatively easy to understand, use and debug. MilkShape 3D is
inexpensive to purchase costing around £20.
An extract from a
MilkShape 3D ASCII file is given below.
// MilkShape 3D ASCII
Frames: 30
Frame: 1
Meshes: 6
"FaceL" 0 -1
4
0 -20.000000 20.000000
-20.000000 0.000000 0.000000 -1
0 -20.000000 -20.000000
-20.000000 0.000000 1.000000 -1
0 -20.000000 20.000000
20.000000 1.000000 0.000000 -1
0 -20.000000 -20.000000
20.000000 1.000000 1.000000 -1
1
-1.000000 0.000000
0.000000
2
0 0 1 2 0 0 0 2
0 1 3 2 0 0 0 2
Basically, the model is
divided up into meshes and each mesh has (among other things) vertex
information, normals and triangles associated with it. In the above example,
there are four vertices which are specified in terms of position and texture
coordinates, there is one normal and there are two triangles. The first
triangle is made from vertices 0, 1 and 2 and the second triangle from vertices
1, 3 and 2.
In order to use this
format, the triangle information must be expanded so as to obtain the vertex
information for each triangle in the mesh. This, plus other pre-processing is
performed in the Load() method of the CMs3dModel class. The CMs3dModel class is
contained within the files ms3dmodel.cpp and ms3dmodel.h.
This program is written in
a similar manner to those in the previous tutorials. There is no intention to
make the program efficient or streamlined, but rather it is written in a manner
that should help explain the processes that are being undertaken in order to
render the model. It is up to the reader to modify and improve the performance
of the code as required.
Within main() an instance
of the model class is created and the cube model file is loaded and
pre-processed with the Load() method. The required textures are loaded and the
model is positions at location (0, 0, -100) within the virtual world. Within
the game loop, the textures are loaded into GS memory as required and the model
is rendered with the BuildGSPacket() method. This method transforms and clips
all of the model vertices and builds the data that is required for the graphics
synthesiser. Note that the model size is not known until run time so most of
the memory required by the class methods is allocated dynamically.
Once the model packet has been
created, it is sent off to the GS with the SPS2 wrapper class method
FirePacketToGs(). This method takes as parameters, the start and end addresses
of the GS Packet, and a pointer to an area of memory which can be used to build
the DMAC chain consisting of DMAC tags. Note that FirePacketToGS() performs
stitching to overcome the SPS2 4k page size.
Within the program some
simple game logic is performed to rotate the model round it’s x, y and z axes.
There is no user
interaction with this program. The program can be terminated by pressing Select
and Start at the same time on controller 1.
In this tutorial a simple
3D model has been loaded and rendered. Using the tutorial code it is possible
to load and display other 3D models which are in the MilkShape3D ASCII format.
Dr Henry S Fortuna
University of Abertay
Dundee