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

Banner

 

The left, right and top and bottom faces are created in the same way but after rotating the initial face through 90 degrees about the Y and X axis respectively. Again we need to pay attention to the order of the vertexes to make sure that they are anticlockwise on side of the face the faces out of the cube.

For example for the left face we need to rotate each point 90 degrees around the Y axis and then move the face in the X negative direction:

//left face
Matrix RotY90 = Matrix.CreateRotationY(
-(float)Math.PI / 2f);
for (int i = 0; i <= 2; i++)
{
vertexes[i + 12] =
new VertexPositionNormalTexture(
Vector3.Transform(face[i], RotY90)
- Vector3.UnitX,
-Vector3.UnitX, Texcoords);
vertexes[i + 12 + 3] =
new VertexPositionNormalTexture(
Vector3.Transform(face[i + 3], RotY90)
  - Vector3.UnitX,
-Vector3.UnitX, Texcoords);
}

The rest of the faces are computed in the same way - a rotation and a translation:

//Right face
for (int i = 0; i <= 2; i++)
{
vertexes[i + 18] =
new VertexPositionNormalTexture(
Vector3.Transform(face[2 - i], RotY90)
- Vector3.UnitX,
Vector3.UnitX, Texcoords);
  vertexes[i + 18 + 3] =
new VertexPositionNormalTexture(
Vector3.Transform(face[5 - i], RotY90)
- Vector3.UnitX, 
Vector3.UnitX, Texcoords);
}

//Top face
Matrix RotX90 = Matrix.CreateRotationX(
-(float)Math.PI / 2f);
for (int i = 0; i <= 2; i++)
{
vertexes[i + 24] =
new VertexPositionNormalTexture(
Vector3.Transform(face[i], RotX90)
+ Vector3.UnitY,
Vector3.UnitY, Texcoords);
  vertexes[i + 24 + 3] =
new VertexPositionNormalTexture(
Vector3.Transform(face[i + 3], RotX90)
+ Vector3.UnitY,
Vector3.UnitY, Texcoords);
}
//Bottom face

for (int i = 0; i <= 2; i++)
{
vertexes[i + 30] =
new VertexPositionNormalTexture(
Vector3.Transform(face[2 - i], RotX90)
- Vector3.UnitY,
-Vector3.UnitY, Texcoords);
  vertexes[i + 30 + 3] =
new VertexPositionNormalTexture(
Vector3.Transform(face[5 - i], RotX90)
- Vector3.UnitY,
-Vector3.UnitY, Texcoords);
  }
  return vertexes;
}

There a few subtle points to notice. The first is that XNA provides a lot of useful classes and methods to make working with vectors and matrices easy.

It is very important that you understand how vectors and matrices work if you want to do 3D programming. The surface normals have been defined to point out of the cube and, as we will see later effect the way light reflects off each surface. In more sophisticated examples the surface normals don't always have to be the true geometric normals to the surface. Think of surface normals as defining the angle that light would have to hit the surface and bounce off at the same angle. By manipulating the surface normals you can make flat surfaces look curved. 

Banner

In case you are wondering for large shapes you wouldn’t “hand code” the vertex array but use a modelling program to create a “mesh” that you would load into you XNA program.  This said - every 3D programmer should know how to create a mesh programmatically and the cube is a good illustration of how this can be done and how geometry can be created.

<ASIN:0735627134>

<ASIN:B001VNC6A2@ALL>

<ASIN:1430218177>

<ASIN:0735626588>

<ASIN:B003BGUOKQ@ALL>

<ASIN:0470912332>

<ASIN:1435454278>



Last Updated ( Friday, 30 July 2010 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.