Monday 2 January 2012

Models in XNA

Loading content into XNA is simple and 3D content is no different. Just create a Model object and use the content.load method to load the fbx file.

I have some basic knowledge of what a 3D object will need to be of any use.

It needs a position (where it is in the game world). 2D sprites also need a position (an x y coordinate) and in the past I've used a vector2 object to hold this data.

A 3D world has an x and y coordinate as objects can move left, right, up and down. But they also move further and closer to the view or across the Z axis (depending on your point of view of course)

Well I only need to hold an extra position value for the Z position then.

Vector3 will do this :)

I also need some other basic info, scale and rotation.

So I can set these on my model object and at least I can position my object in the game world.

I'll tidy this experiment up later by creating my own custom objects to hold my 3D game objects.

Now I need to draw the object to screen.

2D games use sprites and XNA provides spritebatch to make this A one line call.

3D objects are more complex so I don't have this simple one liner.

Msdn has a very simple draw example of how to render the model.

It basically goes through each of the meshes in the object and sets the relevant parameters on the object and any effects required (lighting and the camera view and projection onto the object)

XNA has some strangeness when using spritebatch as wellso I had to set some parameters on the graphicsdevice before and after rendering 3D objects.

To make sure the 3D object is displayed at the right position, in the right scale and rotated correctly to the 3D world, I need to take all the points and calculate the world variables. This is done with some matrix multiplication and now I have a world aligned object.

Next thing is to position the camera and its viewpoint so it can see the object when the scene is rendered.

And I need a light source to illuminate the object.

Once this basic stuff is added, I can run the code and my object is on screen with basic light shading! That Was easy!

No comments:

Post a Comment