Converting Model Coordinates to the View Frame


All of the preceding steps have been geared towards establishing and minimizing the number of polygons that are visible in the current scene. The vertices that describe these polygons are stored in coordinates that are local to each object. For our 3D scene to look like something, we need to transform all of these vertices into their proper screen coordinates. This is accomplished using three transformation matrices. The first two are the projection and view matrices. These two are related to the camera and will be addressed in detail in Chapter 6. For now just think of the two matrices as defining the position and lens for the camera. It is the third matrix that is important to objects.

The third matrix is called the world transform matrix. This matrix will be used to convert each vertex from the object s local coordinates into world coordinates. The video card will then use the two camera-based matrices to perform the final transformation of the points into screen coordinates. Now that we have established what the matrix does, let s look at how it works.

Each matrix is a four-by-four array of floating point values. By performing a vector/matrix multiplication between this matrix and the vertex vector, we end up with a second transformed vector in the other coordinate system. This multiplication allows us to perform three operations at once on the vertices: translation, rotation, and scaling. Figure 3 “2 illustrates the matrix we will be discussing with letters standing in for the 16 matrix elements for easier description.

click to expand
Figure 3 “2: Reference world transform matrix

We ll start with a matrix that doesn t change anything as our starting point. This is referred to as an identity matrix. An identity matrix is all zeros except for the four diagonal elements (A, F, K, P) that are set to one. Multiplying this matrix against any vertex vector will result in a new vector identical to the original. In other words, an identity matrix is the equivalent of multiplying by a scalar one.

To translate or move a point during the transformation, three members of the matrix represent the amount to move in each axis. Element M is the amount to translate along the X-axis. Element N is the translation factor along the Y-axis, and element O is the translation factor along the Z-axis.

By including scaling factors within the transform matrix, we can make the transformed object look smaller when viewed at a distance compared to when it is viewed close at hand. The three elements A, F, and K are the scaling factors along the X-, Y-, and Z-axes.

The third operation performed by the matrix is rotation. This operation is a bit more complicated, since there are actually three separate rotations to be handled, one around each of the axes. Let s look at them one at a time, starting with rotation around the X-axis. In these examples, the letter Q will represent the angle of the rotation in radians. All rotations will be in radians because the trigonometric functions for sine and cosine (sin and cos) require radians. For a rotation around the X-axis, various matrix elements would be set as shown in Listing 3 “9.

Listing 3.9: Transform X Rotation
start example
 F = cos(Q)  G = sin(Q)  J = 2* sin(Q)  K=cos(Q) 
end example
 

A rotation around the Y-axis would set the elements shown in Listing 3 “10.

Listing 3.10: Transform Y Rotation
start example
 A = cos(Q)  C = 2 * sin(Q)  I = sin(Q)  K = cos(Q) 
end example
 

Finally, the elements for the Z-axis rotation are shown in Listing 3 “11.

Listing 3.11: Transform Z Rotation
start example
 A = cos(Q)  B = sin(Q)  E = 2 * sin(Q)  F = cos(Q) 
end example
 

If we put all of this together, we get the nasty-looking matrix shown in Figure 3 “3.

click to expand
Figure 3 “3: World transform matrix formulas

Luckily we do not need to build up this matrix manually for each object in every frame. Microsoft supplies a number of methods for manipulating matrices within the Matrix class. Table 3 “1 lists these methods along with a short description of how they are used.

Table 3 “1: Matrix Manipulation Methods

METHOD

DESCRIPTION

Matrix.Identity

Indicates a static property identity matrix

Translate

Sets the translation factors along the three axes

Scale

Sets the transform scaling factor

RotateX

Adds a rotation factor around the X-axis

RotateY

Adds a rotation factor around the Y-axis

RotateZ

Adds a rotation factor around the Z-axis

RotateYawPitchRoll

Rotates around all three axes using the three supplied angles

Multiply

Combines two matrices by multiplying them together

You will see these methods in action in the following chapters as we begin working with 3D objects.

Once we have established the transform matrix for the current object, it is time to convert all of the object s vertices to the world coordinate system. Ass recently as a few years ago, we would have had to iterate through the vertices in our own code and perform the multiplications ourselves . Modern video cards come with hardware transformation and lighting. To use this feature, we simply have to supply the card with a list of vertices and set the matrix to use for world transformation. Listing 3 “12 shows an example from Microsoft s billboard demo program.

Listing 3.12: Matrix Manipulation Example
start example
 foreach(Tree t in trees)  {     // Set the tree texture.     device.SetTexture(0, treeTextures[t.treeTextureIndex] );     // Translate the billboard into place.     billboardMatrix.M41 = t.vPos.X;     billboardMatrix.M42 = t.vPos.Y;     billboardMatrix.M43 = t.vPos.Z;     device.SetTransform(TransformType.World , billboardMatrix);     // Render the billboard.     device.DrawPrimitive( PrimitiveType.TriangleStrip, t.offsetIndex, 2 );  }     // Restore state.     Matrix matWorld;     matWorld = Matrix.Identity;     device.Transform.World = matWorld; 
end example
 

Let s walk through this code to see what is happening. The software loops through each of the trees in the sample. For each tree, the texture for the tree billboard is set as the current texture. In this example, the matrix is adjusted by setting the tree s position directly in the translation members of the matrix. The SetTransform method gives the matrix to the device for use in the rendering process. The final call in the loop performs the actual rendering of the tree billboards using the DrawPrimitive method. The first argument of the method states that the vertices are arranged to form a triangle strip. This format allows a rectangle (two attached triangles) to be specified using only four vertices. The second argument is the offset within the vertex buffer to begin the rendering. The last argument says that two triangles will be rendered.

Allowing DirectX to perform the transformations by passing the data down to the video card and letting it perform the transformations in hardware is a great boost in rendering performance. If the video card does not support hardware transformation and lighting, it is still better to let DirectX perform the calculations than to do them yourself. The software transformation support in DirectX is highly optimized.




Introduction to 3D Game Engine Design Using DirectX 9 and C#
Introduction to 3D Game Engine Design Using DirectX 9 and C#
ISBN: 1590590813
EAN: 2147483647
Year: 2005
Pages: 98

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net