Getting started with 3D XNA
Written by David Conrad   
Friday, 30 July 2010
Article Index
Getting started with 3D XNA
Building the cube
Buidling the faces
More faces
The effect
Projection and view transformations
Update and rotation

Pointing the camera

The next job is to set up the projection and view transformations. You can think of this as selecting a lens for the camera and pointing it at what we want to look at:

Matrix projection = Matrix.
CreatePerspectiveFieldOfView(
(float)Math.PI / 4.0f,
(float)this.Window.ClientBounds.Width /
(float)this.Window.ClientBounds.Height,
1f, 10f);
effect.Projection = projection;
Matrix V =
Matrix.CreateTranslation(0f,0f,-10f);
effect.View = V;

The projection matrix is set to a 45 degree view which is a moderately wide angle lens and it will render anything within 1 to 10 units of the camera – anything close or further away is ignored.  The view matrix moves the cameras location to (0,0,10) and doesn’t change its orientation so it is still looking straight down the z axis toward the origin.

Banner

Beginners are often confused that a translation of minus 10 moves the camera to 10 – the reason is that the view transformation actually moves the 3D world and the camera stays put!

Now that we have an effect object created and initialize we can move to the Draw method.

The next few steps in rendering the scene are fairly standard.

First we clear the display window to a nice colour and use the BeginScene method to start drawing:

 

protected override void Draw(
GameTime gameTime)
{
GraphicsDevice.Clear(
Color.CornflowerBlue);
Finally we have to set the cull mode so that faces that have their vertexes listed in clockwise order aren’t drawn i.e they are culled, because they are faces pointing into the cube and never visible:
RasterizerState rs = new RasterizerState();
graphics.GraphicsDevice.RasterizerState = rs;

As we are using an effect we have to start processing vertexes using it. The only complication is that an effect can modify different stages in the rendering pipeline and all have to be processed. In the case of BasicEffect it only apply to one pass and we could perform this without a for loop, however the following code is completely generally and works with any effect:

 

foreach (EffectPass pass in 
effect.CurrentTechnique.Passes)
{
 pass.Apply();
 graphics.GraphicsDevice.DrawUserPrimitives
<VertexPositionNormalTexture>(
PrimitiveType.TriangleList, cube,
0, 12);
}

Now you DrawUserPrimitives "knows" how to draw vertexes as specified by the generic type parameter i.e. it will draw an array of VertexPositionNormalTexture vertexes. Notice that it is the number of triangles that you specify to draw i.e. 12, rather than the number of vertexes.

Banner

Finally we have to create an instance of the cube to draw and this is best done in the LoadContent method, even though the content isn't going to be loaded but generated:

private VertexPositionNormalTexture[] cube;
protected override void LoadContent()
{
// Create a new SpriteBatch,
// which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
cube = MakeCube();
}

If you now run the program you will see the overall result - a white square in the middle of the view area. This is all that you can expect as the cube is face on to the viewer, has no texture properties and so defaults to white under the white ambient lighting.

 

cube1

The first view of the cube

<ASIN:1430232161>

<ASIN:0735643350>

<ASIN:0470922443>

<ASIN:1584505370>

<ASIN:0672330067>

<ASIN:0672330229>



Last Updated ( Friday, 30 July 2010 )