Getting Started with Blender 3D and XNA
Installing Blender
The first thing that you need to do is install the Blender system and get it running, to download the latest version of Blender go to www.blender.org and go to the main downloads page. At the time of this article the current version is Blender 2.44. When you do download the package you do have the choice of downloading the Installer Package for most operating systems for my work I will be focusing all of my work using the Windows Operating System.
Download and install the application, but what you might want to do is to also download the Zip of the package. In the past I have used this to recover script files that I have changed. The install process for Blender is pretty painless, during the process you are asked a couple of questions but normally I just select the default options. In summary the main option you are asked is the location of the working files for the application, you can change this later but for the leave the settings as default.
The last thing that you need to do to complete the install is to install the python system. You do not have to do this step but if you do want to use any plug-ins that are not shipped with the product, or customize the ones that are you will need it.
Setting up Python for use with Blender
You will not be able to download the python system from the Blender Site, you will have to go to http://www.python.org/ and download the package from there. Download the Python 2.5.1 system from here. Once you have finished installing Python you will need to restart your machine.
Final Steps
Ok now you should be able to start Blender and start working. One thing that I do like to do is to change the file locations for some of the data I use. To do this you will have to open the settings window and configure the paths. To do this use your mouse and move it just below the main toolbar. From there you should be able to drag down the toolbar to revel the main settings for the application. I will leave the file locations up to you, but normally change the temp locations and move them to another drive.
Exporting Your First Model from Blender
With Blender now fully up and running we will need to have a model and export it so that we can use XNA to display the model. With this article I do not want to show you how to model (That will come Later) all I would like to do is to show you how simple it is to export a model and display it on the screen using XNA.
When you first start Blender you are presented with a small Cube in the centre of the screen, this is the model that we are going to use. The first step is to make sure we are in “Object Mode”. In object mode you are able to select and manipulate complete objects. You can make sure you are in Object mode by Pressing Tab, and looking at the mode selector on the toolbar for the main design window.
Note: For a list of the Different Keyboard shortcuts used in Blender have a look here.
To start make sure that the cube is selected, you tell that an object is selected because the object will be highlighted with a pick outline. Next select the File -> Export menu item and select “Autodesk FBX” as the export type. This will bring up a dialog that will ask you to select a file location and name. Make sure that you put the files in a place where you will be able to find them later.
That’s it for the Blender part, open up Game Studio Express and create a new Windows Game Project called “BasicModel”, this will be the small example program we will create that will display and rotate our model that we have just exported.
Displaying the Model Using XNA
Start the application by Adding a new Folder called “Content” and then adding another one underneath that called “Models”, now add an existing item and add your Exported model to the Application.
With the content Added here is the Game1 class that I use for the sample.
[code language="C#"]
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion
namespace BasicModel
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
ContentManager content;
// Set the position of the model in world space, and set the rotation.
Vector3 modelPosition = Vector3.Zero;
float modelRotation = 0.0f;
// Set the position of the camera in world space, for our view matrix.
Vector3 cameraPosition = new Vector3(0.0f, 0.0f, 10.0f);
Matrix projectionMatrix;
Matrix viewMatrix;
// Set the 3D model to draw.
Model myModel;
// The aspect ratio determines how to scale 3d to 2d projection.
float aspectRatio;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
content = new ContentManager(Services);
aspectRatio = (float)GraphicsDeviceManager.DefaultBackBufferWidth /
(float)GraphicsDeviceManager.DefaultBackBufferHeight;
}
/// <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()
{
// TODO: Add your initialization logic here
projectionMatrix = Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(45.0f), aspectRatio,
1.0f, 200.0f);
viewMatrix = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up);
base.Initialize();
}
/// <summary>
/// Load your graphics content. If loadAllContent is true, you should
/// load content from both ResourceManagementMode pools. Otherwise, just
/// load ResourceManagementMode.Manual content.
/// </summary>
/// <param name="loadAllContent">Which type of content to load.</param>
protected override void LoadGraphicsContent(bool loadAllContent)
{
if (loadAllContent)
{
// TODO: Load any ResourceManagementMode.Automatic content
myModel = content.Load<Model>(@"Content\Models\Test");
}
// TODO: Load any ResourceManagementMode.Manual content
}
/// <summary>
/// Unload your graphics content. If unloadAllContent is true, you should
/// unload content from both ResourceManagementMode pools. Otherwise, just
/// unload ResourceManagementMode.Manual content. Manual content will get
/// Disposed by the GraphicsDevice during a Reset.
/// </summary>
/// <param name="unloadAllContent">Which type of content to unload.</param>
protected override void UnloadGraphicsContent(bool unloadAllContent)
{
if (unloadAllContent)
{
// TODO: Unload any ResourceManagementMode.Automatic content
content.Unload();
}
// TODO: Unload any ResourceManagementMode.Manual content
}
/// <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();
// TODO: Add your update logic here
modelRotation += (float)gameTime.ElapsedGameTime.TotalMilliseconds * MathHelper.ToRadians(0.1f);
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)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
// Copy any parent transforms.
Matrix[] transforms = new Matrix[myModel.Bones.Count];
myModel.CopyAbsoluteBoneTransformsTo(transforms);
// Draw the model. A model can have multiple meshes, so loop.
foreach (ModelMesh mesh in myModel.Meshes)
{
// This is where the mesh orientation is set, as well as our camera and projection.
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.World = transforms[mesh.ParentBone.Index] *
Matrix.CreateRotationX(modelRotation) *
//Matrix.CreateRotationY(modelRotation) *
Matrix.CreateRotationZ(modelRotation) *
Matrix.CreateTranslation(modelPosition);
effect.View = this.viewMatrix;
effect.Projection = this.projectionMatrix;
}
// Draw the mesh, using the effects set above.
mesh.Draw();
base.Draw(gameTime);
}
}
}
}
[/code]
Cross Post from www.virtualrealm.com.au