Click here to Skip to main content
12,505,341 members (64,035 online)
Click here to Skip to main content
Add your own
alternative version

Stats

153.2K views
1.8K downloads
127 bookmarked
Posted

XNA: A Beginner's Introduction

, 17 Jul 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
Getting started with XNA, Blender, and 3D modeling
Screenshot - img7.png

Introduction

This is an introductory article for XNA development, discussing tools and the minimum amount of code to start using the XNA Game Studio with simple models created in Blender.

Tools

To write applications (such as games) using the XNA Game Studio, you must download and install a variety of tools. Since Microsoft changes its website frequently, these links are not guaranteed to work. If you don't get to the download pages, use Google to search for the new links using the appropriate keywords.

This article is written using:

  • Visual Studio C# 2005 Express Edition with SP1
  • XNA Game Studio Express 1.0 Refresh
  • XNA Framework 1.0 Refresh
  • Blender 2.44
  • Python 2.51

Visual Studio Express

XNA development, as of the writing of this article, can only be done using Visual Studio Express.

  • Download Visual Studio C# 2005 Express Edition here
  • Download SP1 for VS Expression Edition here (scroll down a bit)

XNA

XNA development requires both the Game Studio Express (GSE) and the XNA Framework:

  • Download XNA Game Studio Express here
  • Download XNA Framework here

Modeling Tool

If you're going to be doing anything "serious" with XNA, you will quickly discover that you need a modeling tool to create your 3D models. I recommend Blender (which is free) or AutoDesk Maya, which is $2000. Guess which tool I'll be mentioning in this and other articles?

  • Download Blender here
  • Drool over Maya using the Maya Personal Learning Edition here

The Maya PLE cannot be used for creating models for XNA (as far as I know) because it cannot export to the format that XNA requires.

Plug-Ins

You will probably want to take advantage of plug-ins and scripts in Blender, which use Python.

  • Download Python here

Blender automatically detects the Python installation, so no further work on your part is necessary.

Tutorials

Tutorials on XNA and Blender are quite abundant and the Microsoft and Blender sites respectively and on numerous third party sites. Simply Google and you will find many resources. I started by going through the Microsoft tutorial on displaying a 3D model. Be aware of the aspect ratio bug which I describe here. It's not fatal, but it will lead to stretched objects if you use the Microsoft tutorial as a basis for working with models created in Blender.

Creating a Windows XNA Project

Launch Visual Studio C# 2005 Express Edition.

Creating the Windows Game Project

Create a Windows Game project by selecting the Windows Game project icon:

Enter a name for the project and click on OK.

Create a Model Folder

Models (spaceships, people, objects, etc.) typically live in a Content\Models folder. Right click on the project and add the Content folder and Models sub-folder, so your project tree looks like this:

If you build and run this project, you will get a window with a blue field.

Create a Model in Blender

Launch Blender. Blender starts with a cube as an initial model, although since you're looking at the cube from the top, it looks like a square:

To see that it's actually a cube, click on the numpad 0 (with numlock on) and you will see:

Export the Model

XNA cannot load Blender files, so rather, you must export your Blender model to an FBX format. You can read a little bit about FBX here. Basically it's an open standard, platform independent 3D file format created by AutoDesk. You can also export to DirectX (.x) format, however this brings up a complicated options dialog, whereas the FBX export does not.

The Blender UI will be a bit weird to Windows users, so here's the walkthrough:

  • On the menu bar at the top, click on File (do not try to use the keyboard with the typical Alt-F keystroke combo!)
  • Move the mouse down to Export
  • Move the mouse over to Autodesk FBX
  • A dialog will appear with two textboxes, the first showing the path and the second for entering the filename. Click on the second edit box and type in "cube" (without the quotes).
  • Click on the Export FBX button or press the Enter key twice.

Import the Model into XNA

The model is now ready for importing into XNA:

  • Go back to the XNA Game Studio and right click on the Models folder and select "Add / Existing Item...".
  • In the file types, select "Content Pipeline Files"
  • Navigate to the Blender folder and select the cube.fbx file.

Add the Code to Render the Model

Rendering the model involves:

  • Defining where the model is
  • Defining where the camera is
  • Defining the camera orientation
  • Loading the model into the content pipeline
  • Drawing the model

All of this work will be done in the game1.cs file and the Game1 class.

Defining Where the Model Is

For simplicity, the model is placed at world coordinates (0, 0, 0). Create a field for the model position:

protected Vector3 modelPosition = Vector3.Zero;

Defining Where the Camera Is

Create a field for the camera position:

protected Vector3 cameraPosition = new Vector3(0.0f, 0.0f, 10.0f);

How did I arrive at these values? The vector consists of the x, y, and z axis position of the model. X is left-right, Y is up-down, and Z is in-out. If you look at the screenshots of the cube above, you will notice a grid. Each grid line represents one unit, therefore the cube is a 2 units wide, 2 units long, and 2 units tall. The camera is position ten units away from the world center (0, 0, 0) which creates a reasonably sized rendering of the cube.

Loading the Model into the Content Pipeline

Create a field for your model:

protected Model myModel;

In the LoadGraphicsContent method, load your cube model:

protected override void LoadGraphicsContent(bool loadAllContent)
{
  if (loadAllContent)
  {
    myModel = content.Load<Model>("Content\\Models\\cube");
  }
}

Note that you do not specify the file extension. The XNA Game Studio will load the appropriate file, as the model is actually compiled to a different file extension from the FBX file that you added to the Content\Models folders.

While we're here, let's also get the aspect ratio of the viewport (our window) as this is necessary for rendering the model so that it looks like a cube. Add the field:

protected float aspectRatio;

and, to the above method, calculate the aspect ratio:

protected override void LoadGraphicsContent(bool loadAllContent)
{
  if (loadAllContent)
  {
    myModel = content.Load<Model>("Content\\Models\\cube1");
  }

  aspectRatio = ((float)graphics.GraphicsDevice.Viewport.Width) / 
     ((float)graphics.GraphicsDevice.Viewport.Height);
}

Drawing the Model

Modify the Draw method so that it iterates through the model meshes, translating and rotating them appropriately, etc.

protected override void Draw(GameTime gameTime)
{
  graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

  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];
      effect.View = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, 
         Vector3.Up);
      effect.Projection = Matrix.CreatePerspectiveFieldOfView(
         MathHelper.ToRadians(45.0f), aspectRatio, 1.0f, 10000.0f);
    }

    // Draw the mesh, using the effects set above.
    mesh.Draw();
  }

  base.Draw(gameTime);
}

When you run the program, you will get a cube rendered on the blue field:

The following is a brief description of what is going on in the above method. Each of these subtopics deserves considerably more description that I have provided, but I feel that is outside the scope of this beginner article.

Bones

The code above is the basic rendering loop for all models in the game. XNA uses a skeletal animation technique which you can read about here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Marc Clifton
United States United States
Marc is the creator of two open source projects, MyXaml, a declarative (XML) instantiation engine and the Advanced Unit Testing framework, and Interacx, a commercial n-tier RAD application suite.  Visit his website, www.marcclifton.com, where you will find many of his articles and his blog.

Marc lives in Philmont, NY.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 47289746-Aug-10 6:40
memberMember 47289746-Aug-10 6:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160927.1 | Last Updated 17 Jul 2007
Article Copyright 2007 by Marc Clifton
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid