Hi, In my Xna 3D Game. I made my own Terrain.cs Class in which I did the follows:
class Terrain
{
String LocationOfFile;
GraphicsDevice Device;
Viewport Viewport;
Model Ground;
Vector3 Position;
Matrix World = Matrix.Identity;
public Terrain()
{
}
public void Initialize(GraphicsDevice D, Model Mesh, String LocationOfFile)
{
Ground = Mesh;
Device = D;
this.LocationOfFile = LocationOfFile;
Viewport = Device.Viewport;
}
public void LoadContent(ContentManager Content, Vector3 Pos)
{
Ground = Content.Load<Model>(LocationOfFile);
Position = Pos;
World = Matrix.CreateTranslation(Position);
}
public void Draw(SpriteBatch spriteBatch, Camera.Camera Camera)
{
Matrix[] Transformations = new Matrix[Ground.Bones.Count];
Ground.CopyAbsoluteBoneTransformsTo(Transformations);
foreach (ModelMesh Mesh in Ground.Meshes)
{
foreach (BasicEffect effect in Mesh.Effects)
{
effect.EnableDefaultLighting();
effect.View = Camera.View;
effect.Projection = Camera.Projection;
effect.World = Transformations[Ground.Bones.Count] * World;
}
Mesh.Draw();
}
}
}
And from my Game1.cs Class I did this:
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Camera.Camera Camera;
Terrain.Terrain Terrain;
Model Land;
public LifeAsWeKnowIt()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
graphics.PreferredBackBufferHeight = 600;
graphics.PreferredBackBufferWidth = 800;
Camera = new Camera.Camera(this.GraphicsDevice, Vector3.Zero, new Vector3(0.0f, 20.0f, 0.0f), 1.0f, 1000.0f);
Terrain = new Life_As_We_Know_It.Terrain.Terrain();
Terrain.Initialize(this.GraphicsDevice, Land, "Terrains\\Land");
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
Terrain.Update(gameTime);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
Terrain.Draw(spriteBatch, Camera);
base.Draw(gameTime);
}
}
But when I try to draw my Terrain Object derived from Terrain.cs Class I get the following error:
Object Reference Not Set To An Instance Of An Object In the Terrain.cs Class on Line "Matrix[] Transformations = new Matrix[Ground.Bones.Count];"