Game Development Reference
In-Depth Information
Using the OBJ Loader
To demonstrate the OBJ loader, let's rewrite the previous example and create a new test called
ObjTest
along with an
ObjScreen
. Copy over all the code from the previous example and only
change the line in the constructor of the
ObjScreen
class that's responsible for creating the cube:
cube = ObjLoader.
load
(glGame, "cube.obj");
Instead of using the
createCube()
method (which we removed), we are now directly loading
a model from an OBJ file, called
cube.obj
. We created a replica of the cube we previously
specified programmatically in
createCube()
in Wings3D. You can find it in the SVN repository
just like all other assets. It has the same vertex positions, texture coordinates, and normals as
the handcrafted version. It should come as no surprise that, when you run
ObjTest
, it will look
exactly like our
EulerCameraTest
. We'll therefore spare you the obligatory screenshot.
Some Notes on Loading Models
For the game we are going to write in the next chapter, our loader is sufficient, but it is far from
robust. There are some caveats:
ï?®
String processing in Android is inherently slow. The OBJ format is a plain-text
format and, as such, it needs a lot of parsing. This will have a negative
influence on load times. We can work around this issue by converting our
OBJ models to a custom binary format. We could, for example, just serialize
the
verts
array that we fill in the
ObjLoader.load()
method.
The OBJ format has a lot more features that we don't exploit. If you want
ï?®
to extend our simple loader, look up the format specification on the Web. It
should be easy to add more functionality.
ï?®
An OBJ file is usually accompanied by what's called a
material file
. This file
defines the colors and textures to be used by groups of vertices in the OBJ
file. We will not need this functionality since we know which texture to use
for a specific OBJ file. For a more robust loader, you'll want to look into the
material file specification as well.
A Little Physics in 3D
In Chapter 8, we developed a very simple point mass-based physics model in 2D. Here's the
good news: everything works the same in 3D!
ï?®
Positions are now 3D vectors instead of 2D vectors. We just add a
z coordinate.
ï?®
Velocities are still expressed in meters per second on each axis. We just add
one more component for the z axis!
ï?®
Accelerations are also still expressed in meters per second squared (m/s
2
)
on each axis. Again, we just add another coordinate.