You Have a Sky, but the Tank Can t Drive There


You Have a Sky, but the Tank Can't Drive There

In Blockers, you loaded the levels from a file, and the data that was required to be rendered was generated dynamically based on the contents of that file. For this game, the entire level is loaded from a mesh file. The actual vertex data defining the mesh will also be used to determine the physics of the world and the interaction between the world and the inhabitants it shares (for example, the tank). You once again want this code to be encapsulated in its own file, so create a new code file called level.cs and use the code from Listing 14.2 as the base implementation.

Listing 14.2. The Level Class
 using System; using System.Drawing; using System.Windows.Forms; using Microsoft.DirectX; using Microsoft.DirectX.Direct3D; namespace Tankers {     public class Level : IDisposable     {         private const float Height = 0.0f;         private static readonly Matrix LevelMatrix = Matrix.Translation(                                                       0,Height,0);         private const string LevelMeshName = "Level1.x";         private const float LevelSize = 3000.0f;         private Mesh levelMesh = null;         private Texture[] levelTextures = null;         /// <summary>         /// Create a new instance of the level class         /// </summary>         public Level(Device device)         {             // First create the mesh             ExtendedMaterial[] mtrls = null;             levelMesh = Mesh.FromFile(GameEngine.MediaPath + LevelMeshName,                 MeshFlags.Managed, device, out mtrls);             // With the mesh created, create each of the textures             // used for the level             if ((mtrls != null) && (mtrls.Length > 0))             {                 levelTextures = new Texture[mtrls.Length];                 for(int i = 0; i < mtrls.Length; i++)                 {                     levelTextures[i] = TexturePool.CreateTexture(device,                                          mtrls[i].TextureFilename);                 }             }         }         /// <summary>         /// Render the level         /// </summary>         public void Draw(GameEngine engine, Device device)         { #if (DEBUG)             engine.numberFaces += levelMesh.NumberFaces;             engine.numberVerts += levelMesh.NumberVertices; #endif             // Set the world transform             engine.SetWorldTransform(LevelMatrix);             device.RenderState.Lighting = false;             // Render each subset of the mesh             for(int i = 0; i < levelTextures.Length; i++)             {                 device.SetTexture(0, levelTextures[i]);                 levelMesh.DrawSubset(i);             }             device.RenderState.Lighting = true;         }         /// <summary>         /// Will be used to clean up objects         /// </summary>         public void Dispose()         {             if (levelMesh != null)             {                 levelMesh.Dispose();                 levelMesh = null;             }         }     } } 

The initial code here looks similar to the code for the sky box. You have some constants defined, and a constant transformation matrix is built. The code on the included CD includes two different level mesh files (aptly named level1.x and level2.x), either of which would work fine with this code. Pick which one you like best and use that as the constant. The levels themselves are square with each side having a length of 3,000 units, which is where the last constant comes from.

Creation is exactly the same as creating the sky box, and the first portion of the rendering is as well. Notice that before you render the level mesh, however, you do not turn off the depth buffer. It will be possible to have objects rendered "behind" the level (or at least behind walls in the level), so you want the depth buffer on for this rendering. Lighting is off. You can see the textures used for the models, so you're not driving your tank down some dark hallway of a level where you can barely see anything. After the mesh is rendered, you turn the lighting render state back on.

Construction Cue

Have you noticed that you've stopped setting the material object on the device? Remember from the beginning of the implementation of this game, you created a static "white" material. Because all the objects in the game (so far) require a single white material, it is set in the rendering method in the game engine and doesn't need to be set in these individual rendering methods.




Beginning 3D Game Programming
Beginning 3D Game Programming
ISBN: 0672326612
EAN: 2147483647
Year: 2003
Pages: 191
Authors: Tom Miller

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