Funding for 'IT Lab' Project, Phase 1: Progress of sticker sales. Purchase a sticker to help us reach our target.Updated: 2010-02-04 05:34
Getting Started with XNA Game Studio 3.0 to develop video games -Part IV

This month we`ll
discuss about the code and steps to draw a 3d model on the XNA game.

It could be
either .x or .fbx.

Then load that 3D model to the content folder.
Right click on the content folder and Add->existing items
And select that
file form the hard drive.
It could be .x
and .fbx.
Now in the Game
1.cs you need to write code to draw 3D model in the screen.
Model car;
Then in the LoadContent() methord
you can load the image.
car =
Content.Load<Model>("Car");
then in the Draw()method you can draw the model in the
screen.
//create a new matrix and copy that tranfomation to 3d model
Matrix[] transforms = new
Matrix[car.Bones.Count];
//copy the absolute transforms to the car model.
car.CopyAbsoluteBoneTransformsTo(transforms);
//use two foreach loops to loop through the mesh and mesh effect
foreach (ModelMesh
mesh in car.Meshes)
{
foreach (BasicEffect
effect in mesh.Effects)
{
//enable default lighting provides
by XNA.
effect.EnableDefaultLighting();
//define the world matrix to locate the position
effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateTranslation(30.0f, -40.0f, -400.0f);
//define view matrix to set up the camara location
effect.View = Matrix.CreateLookAt(Vector3.Zero, Vector3.Forward,
Vector3.Up);
and near and far plane distance.
effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(80.0f), 800.0f / 600.0f,
10.0f, 10000.0f);
}
mesh.Draw();
//Draw the 3d mesh
now you can run
the project by pressing F5 or by clicking the run button.

Final code of Game1.cs
using System;
using
System.Collections.Generic;
using
System.Linq;
using
Microsoft.Xna.Framework;
using
Microsoft.Xna.Framework.Audio;
using
Microsoft.Xna.Framework.Content;
using
Microsoft.Xna.Framework.GamerServices;
using
Microsoft.Xna.Framework.Graphics;
using
Microsoft.Xna.Framework.Input;
using
Microsoft.Xna.Framework.Media;
using
Microsoft.Xna.Framework.Net;
using
Microsoft.Xna.Framework.Storage;
namespace
Draw_3d_model
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 :
Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager
graphics;
SpriteBatch
spriteBatch;
Model car;
public
Game1()
{
graphics = new
GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to
before starting to run.
/// This is where it can query for any required services and
load any non-graphic
/// related content.
Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected
override void
Initialize()
{
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place
to load
/// all of your content.
/// </summary>
protected
override void
LoadContent()
{
// Create
a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
car = Content.Load<Model>("Car");
}
/// <summary>
/// UnloadContent will be called once per game and is the
place to unload
/// all content.
/// </summary>
protected
override void
UnloadContent()
{
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing
audio.
/// </summary>
/// <param
name="gameTime">Provides a
snapshot of timing values.</param>
protected
override void
Update(GameTime gameTime)
{
// Allows
the game to exit
if
(GamePad.GetState(PlayerIndex.One).Buttons.Back
== ButtonState.Pressed)
this.Exit();
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param
name="gameTime">Provides a
snapshot of timing values.</param>
protected
override void
Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
Matrix[]
transforms = new Matrix[car.Bones.Count];
car.CopyAbsoluteBoneTransformsTo(transforms);
foreach
(ModelMesh mesh in
car.Meshes)
{
foreach
(BasicEffect effect in
mesh.Effects)
{
effect.EnableDefaultLighting();
effect.World =
transforms[mesh.ParentBone.Index] * Matrix.CreateTranslation(30.0f,
-40.0f, -400.0f);
effect.View = Matrix.CreateLookAt(Vector3.Zero,
Vector3.Forward, Vector3.Up);
effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(80.0f), 800.0f / 600.0f,
10.0f, 10000.0f);
}
mesh.Draw();
}
base.Draw(gameTime);
}
}
}
Modify the code for 3d animation.
Also you can
create a 3d animation to this example using these lines of codes.
First initilize a
float varible to the animation.
float val = 0.0f;
then in Update()
metord increment the valuve
to change the position.
val = val + 0.8f;
finally in the Draw()
metord add the val variable to change
the position.
effect.World
= transforms[mesh.ParentBone.Index] * Matrix.CreateTranslation(30.0f+val,
-40.0f, -400.0f-val);
then after you
press F5 you are ready to play the animation.
Final code of Game1.cs with a 3d animation
using System;
using
System.Collections.Generic;
using System.Linq;
using
Microsoft.Xna.Framework;
using
Microsoft.Xna.Framework.Audio;
using
Microsoft.Xna.Framework.Content;
using
Microsoft.Xna.Framework.GamerServices;
using
Microsoft.Xna.Framework.Graphics;
using
Microsoft.Xna.Framework.Input;
using
Microsoft.Xna.Framework.Media;
using
Microsoft.Xna.Framework.Net;
using
Microsoft.Xna.Framework.Storage;
namespace
Draw_3d_model
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 :
Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager
graphics;
SpriteBatch
spriteBatch;
Model
car;
float
val = 0.0f;
public
Game1()
{
graphics = new
GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to
before starting to run.
/// This is where it can query for any required services and
load any non-graphic
/// related content.
Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected
override void
Initialize()
{
base.Initialize();
}
/// <summary>
/// LoadContent will be
called once per game and is the place to load
/// all of your content.
/// </summary>
protected
override void
LoadContent()
{
// Create
a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
car = Content.Load<Model>("Car");
}
/// <summary>
/// UnloadContent will be called once per game and is the
place to unload
/// all content.
/// </summary>
protected
override void
UnloadContent()
{
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing
audio.
/// </summary>
/// <param
name="gameTime">Provides a
snapshot of timing values.</param>
protected
override void
Update(GameTime gameTime)
{
// Allows
the game to exit
if
(GamePad.GetState(PlayerIndex.One).Buttons.Back
== ButtonState.Pressed)
this.Exit();
val = val + 0.8f;
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param
name="gameTime">Provides a
snapshot of timing values.</param>
protected
override void
Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
Matrix[]
transforms = new Matrix[car.Bones.Count];
car.CopyAbsoluteBoneTransformsTo(transforms);
foreach
(ModelMesh mesh in
car.Meshes)
{
foreach
(BasicEffect effect in
mesh.Effects)
{
effect.EnableDefaultLighting();
effect.World =
transforms[mesh.ParentBone.Index] * Matrix.CreateTranslation(30.0f+val,
-40.0f, -400.0f-val);
effect.View = Matrix.CreateLookAt(Vector3.Zero,
Vector3.Forward, Vector3.Up);
effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(80.0f), 800.0f / 600.0f,
10.0f, 10000.0f);
}
mesh.Draw();
}
base.Draw(gameTime);
}
}
}
This is the end
of the draw 3d model and 3d animation tutorial.
Also source
project is attach with this article. Download
Form next
tutorial you learn about working with inputs in XNA.