XNA, Managed DirectX and Game Programming

Rotating a Simple Mesh using the MDX2 Sample Framework

I know that there is still a lot of buzz about the XNA Framework, but I was asked a small question on how to rotate a simple mesh with the Sample Framework that is included with the Managed DirectX System, so instead of just concentrating on XNA I thought I would post this.

The first step is to make sure that you have the empty project running from the latest SDK. Once this is done and working open the emptyproject.cs and add the following few lines of code directly after it.

// HUD Ui Control constants
private const int ToggleFullscreen = 1;
private const int ToggleReference = 3;
private const int ChangeDevice = 4;

Add this Code...

// Teapot Mesh
private Mesh teapotMesh = null;
private Material teapotMaterial;

Next you will need to ad the following few lines of code to the OnCreateDevice call, make sure you add the code to the end of the function.

// Create the teapot mesh
this.teapotMesh = Mesh.Teapot(e.Device);
this.teapotMaterial = new Material();
this.teapotMaterial.DiffuseColor = new ColorValue(1.0f, 1.0f, 1.0f, 1.0f);

To make sure that you can see the mesh we will need to add some light to the application. Add the following code block to the end of the OnResetDevice function.

// Setup Lights
e.Device.Lights[0].DiffuseColor = new ColorValue(1.0f, 1.0f, 1.0f, 1.0f);
e.Device.Lights[0].Direction = new Vector3(0, -1, 0);
e.Device.Lights[0].LightType = LightType.Directional;
e.Device.Lights[0].Enabled = true;

The last step is to actually render the Mesh to the Graphics Device, add the following code block to the OnFrameRender Call directly after the BeginSceneCalled=true is called.

device.Transform.View = camera.ViewMatrix;
device.Transform.Projection = camera.ProjectionMatrix;
device.Transform.World = Matrix.RotationX((float)appTime);
device.Material = this.teapotMaterial;
this.teapotMesh.DrawSubset(0);

If every thing has been done correctly you should now see a rotating Teapot in the centre of the screen. Also as we are using the Sample Framework you should also be able to use the built in movement functions of the model viewer camera that the empty project uses to move the camera around the scene. To do this hold the right mouse button down and move the mouse... You can also use the wheel of the mouse to move the camera back and forth.

Hope this helps...

Cross Post from Virtual Realm - Mykre's Space
Leave a Comment

(required) 

(required) 

(optional)

(required)