Building the Fractal Land


Chapter 26. Fractal Land

Chapter 25 looked at how to create a realistic setting for a first-person game (a 3D maze). The emphasis was on techniques needed to navigate through the environment, such as multiple views, spotlights, and collision detection. Though the maze's floor was textured, it was quite flat.

This chapter continues the first-person gaming theme but focuses on making the ground more interesting. The terrain can vary from rolling hills to craggy mountains. The key elements of this chapter include:


A fractal landscape

Each time the application begins, a different landscape is generated using a fractal-based algorithm to produce terrain heights. The variation in the heights are controlled by a flatness value entered at the command line when the program is started. The heights are translated into a patchwork of quadrilaterals (quads) which form the landscape.

Figure 26-1 shows two terrains, generated by two separate calls to the application (FractalLand3D), with different flatness values.

The ground is surrounded by walls, which prevent the user from leaving the terrain area.


Texturing

Each quad is covered with a texture, the choice of texture being determined by the quad's average height. A highly textured object can start to shimmer when viewed from far away since the texture is being mapped to too small an area of screen pixels. I avoid this problem by using minification filtering and mipmapping, which reduce a texture's resolution depending on the viewpoint's distance from the textured object.


Lighting effects

The quads are affected by lighting, adding interesting shadows to their surfaces. This requires normals to be calculated for each quad, which is done automatically with the aid of Java 3D's GeometryInfo and NormalGenerator classes. Part of the normal generation includes smoothing the creases between the quad's edges, making the edges less visible.

The user's view is obscured by Java 3D fog, which obscures distant parts of the landscape. (I've commented this feature out of the code for the examples in Figure 26-1 so you can see the terrains clearly.)


Key-based navigation (again)

I use another variation of the KeyBehavior class (first seen in Chapter 24) to control navigation from the keyboard. This time the variation allows the viewpoint to follow the lie of the land automatically, moving up hills and down into valleys. This terrain-following technique is implemented using Java 3D's picking.


Landscape construction

The implementation groups the individual quads together based on their average heights, which allows the program to use less geometry and texture objects. At the last stage of landscape creation, the quads are converted into triangle strips by a process called stripification, using Java 3D's Stripifier class. This conversion allows the graphics engine to optimize the rendering of the terrain.

Figure 26-1. Fractal lands


This chapter is the first of two on terrain. Chapter 27 takes a different approach to landscape generation, using data supplied by Terragen, a popular scenery-generation package. This allows me to create a detailed landscape outside of Java 3D, which can be used every time the program is executed.

Another major theme of Chapter 27 is adding scenery to the landscape (3D and 2D objects). The terrain in this chapter is devoid of scenery.



Killer Game Programming in Java
Killer Game Programming in Java
ISBN: 0596007302
EAN: 2147483647
Year: 2006
Pages: 340

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net