Microsoft XNA Game Studio Creator’s Guide P10
lượt xem 9
download
Microsoft XNA Game Studio Creator’s Guide P10
Microsoft XNA Game Studio Creator’s Guide P10:The release of the XNA platform and specifically the ability for anyone to write Xbox 360 console games was truly a major progression in the gameprogramming world. Before XNA, it was simply too complicated and costly for a student, software hobbyist, or independent game developer to gain access to a decent development kit for a major console platform.
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: Microsoft XNA Game Studio Creator’s Guide P10
 math is a branch of linear algebra, and all 3D graph MATRIX ics programmers can benefit from understanding it. In video game development, matrices are used to store data such as vertices and infor mation about how to transform an object. Matrices are simply grids of rows and col umns, but they are essential for scaling, rotating, and translating objects in 3D space. You will have noticed by now that matrix calculations are used throughout your XNA and shader code for performing transformations, controlling your camera, and even drawing 3D models. Understanding how these matrix methods work will pro vide you with a better understanding of 3D game engines. Most of the time, you can get away with just using XNA matrix methods to automatically create matrices and to implement your transformations. However, for complex vector transformations, you may need to be able to build your own matrices for the calculations. In Chapter 8, a matrix is manually created to compute the flight path of an air plane. In Chapter 19, a matrix is manually built to implement a vector transforma tion that determines the starting position and direction of a rocket. In cases like these, understanding the matrix math can definitely help to simplify your transformations. M ATRIX MULTIPLICATION This section introduces matrix multiplication and prepares you for performing man ual transformations later in the chapter. The product of two matrices is obtained by multiplying the rows of matrix A by the columns of matrix B (where matrix A is on the left side of the operator). For the multiplication to be possible, the total number of columns in matrix A must equal the total number of rows in matrix B. Matrix Types XNA’s Matrix type enables storage of 3×3 matrices (3 rows by 3 columns) and 4×4 matrices (4 rows by 4 columns). Each cell in the matrix grid can be accessed by refer encing the matrix and suffixing it with the cell’s row and column, where the topleft cell begins at row 1, column 1. Each cell stores a float: float cellvalue = Matrix matrix.MRC For example, matrix.M11 represents the value in row 1, column 1. Ma trix.M13 represents the value in row 1, column 3. Matrix Multiplication Example: 1×4 Matrix * 4×4 Matrix This example shows how to multiply a 1×4 matrix by a 4×4 matrix. We’ll first show the multiplication done by hand so that you can see each step of the calculation. 248
 C H A P T E R 1 6 249 Matrices Later, the same operation will be shown in code. For this example, a vector with X=2, Y=1, Z=0, and W=0 will be multiplied by a 4×4 matrix. Manual Calculation To set up the equation, the vector is placed on the left side of the multiplication operator, and the 4×4 matrix is placed on the right, as shown here:  2 1 0 0  X  2 1 3 1   1 2 4 1   0 3 5 1   2 1 2 1  The row on the left is multiplied by each column on the right. The following for mula is used for each of the four columns of vector C, where A represents the matrix on the left and B represents the matrix on the right: for(int c=1; c
 250 MICROSOFT XNA GAME STUDIO CREATOR’S GUIDE each cell. Cell() does this by adding extra spaces until the total character count for the string matches the number of spaces allotted for each cell. When the string has been created, it is returned to the calling function: public string Cell(float cell){ string cellDisplay = cell.ToString("N2"); // 2 decimals const int CELL_WIDTH = 8; // 8 chars wide int numDigits = cellDisplay.Length; // right align text and add padding on left for (int i = 0; i < CELL_WIDTH  numDigits; i++) cellDisplay = " " + cellDisplay; return cellDisplay; } To display the cell data (for the product matrix) as text in the game window, you will require the DrawMatrix() method. Add it to your game class so that you can convert each cell of the product matrix to a string, combine cells to form each row of the matrix, and then draw each row of the matrix in the window. public void DrawMatrix(Matrix C){ String[] row = new String[4]; // output strings row[0] = Cell(C.M11) + Cell(C.M12) + Cell(C.M13) + Cell(C.M14); row[1] = Cell(C.M21) + Cell(C.M22) + Cell(C.M23) + Cell(C.M24); row[2] = Cell(C.M31) + Cell(C.M32) + Cell(C.M33) + Cell(C.M34); row[3] = Cell(C.M41) + Cell(C.M42) + Cell(C.M43) + Cell(C.M44); spriteBatch.Begin(SpriteBlendMode.AlphaBlend, // enable transparency SpriteSortMode.Immediate, // use manual order SaveStateMode.SaveState); // preserve 3D settings for (int i = 0; i < 4; i++){ // draw 4 matrix rows Rectangle safeArea = TitleSafeRegion(row[i], spriteFont); float height = spriteFont.MeasureString(row[i]).Y; spriteBatch.DrawString( spriteFont, // font row[i], // row string new Vector2(safeArea.Left, // top left pixel safeArea.Top+(float)i*height), Color.Yellow); // color
 C H A P T E R 1 6 251 Matrices } spriteBatch.End(); } When you open the solution, you must add the MultiplyMatrix() method to the game class so it can initialize two matrices and calculate their product. For this example, the code declares matrix A and initializes it to store the vector in the first row. Initially, when the constructor for the Matrix type is referenced, all cells in ma trix A are initialized to 0. The vector’s X, Y, Z, and W components are assigned to the four cells of the first row of matrix A. The cell data for the matrix on the right side of the operator is assigned to matrix B; then A and B are multiplied together to generate the product matrix. public Matrix MultiplyMatrix(){ Matrix A = new Matrix(); Matrix B = new Matrix(); // store vector in first row  all other cells equal 0 A.M11 = 2.0f; A.M12 = 1.0f; A.M13 = 0.0f; A.M14 = 0.0f; // initialize matrix B B.M11 = 2.0f; B.M12 = 1.0f; B.M13 = 3.0f; B.M14 = 1.0f; B.M21 = 1.0f; B.M22 = 2.0f; B.M23 = 4.0f; B.M24 = 1.0f; B.M31 = 0.0f; B.M32 = 3.0f; B.M33 = 5.0f; B.M34 = 1.0f; B.M41 = 2.0f; B.M42 = 1.0f; B.M43 = 2.0f; B.M44 = 1.0f; return A * B; } To trigger the methods that calculate the matrix product and display the output, replace the line DrawFonts(gameTime); inside Draw() with the following: DrawMatrix(MultiplyMatrix()); When you run this code, the product matrix will appear in the window: 5.00 4.00 10.00 3.00 0.00 0.00 0.00 0.00
 252 MICROSOFT XNA GAME STUDIO CREATOR’S GUIDE 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 This result verifies that the code, C = A * B (where A, B, and C are Matrix ob jects), generates the same product as shown in the lengthy manual calculation. Matrix Multiplication Example: 4×4 Matrix * 4×4 Matrix This next example demonstrates how to multiply a 4×4 matrix by a 4×4 matrix. Knowing how to do this by hand is very useful because all of the transformations you have been implementing in your XNA code involve multiplying 4×4 matrices by 4×4 matrices. You will first see how the multiplication can be performed manually, and then how you can do it in code. Manual Calculation For this case, the following two matrices, A and B, are to be multiplied: A X B =  2 1 0 0  X  2 1 3 1  1 2 0 0   1 2 4 1   3 1 0 0   0 3 5 1  3 2 2 0   2 1 2 1  When you’re calculating the product of a 4× 4 matrix by a 4× 4 matrix, the for mula to multiply the rows of matrix A by the columns of matrix B is for(r=1; r
 C H A P T E R 1 6 253 Matrices  5 4 10 3  4 5 11 3   7 5 13 4  4 7 9 1  Calculation in Code After performing the longwinded manual calculation, you can appreciate the simplicity of being able to compute the same result with the instruc tion C = A * B. Using the code solution from the previous example, in MultiplyMatrix(), re place the instructions that set the individual cell values for matrix A with the follow ing version (matrix B remains the same as the previous example, so no changes are required to it): A.M11 = 2.0f; A.M12 = 1.0f; A.M13 = 0.0f; A.M14 = 0.0f; A.M21 =1.0f; A.M22 =2.0f; A.M23 = 0.0f; A.M24 = 0.0f; A.M31 = 3.0f; A.M32 = 1.0f; A.M33 = 0.0f; A.M34 = 0.0f; A.M41 =3.0f; A.M42 = 2.0f; A.M43 = 2.0f; A.M44 = 0.0f; When you run the code, you will see the result does indeed match the manual cal culation:  5.00 4.00 10.00 3.00  4.00 5.00 11.00 3.00   7.00 5.00 13.00 4.00  4.00 7.00 9.00 1.00  At this point, we can say when multiplying a 4×4 matrix by a 4×4 matrix that the manual calculation can be executed in one line with the following instruction: Matrix C = A * B T RANSFORMATION MATRICES As mentioned earlier in this chapter, when drawing primitive shapes and 3D models, you use matrices to transform sets of vertices. Through the study of linear algebra, specific matrices have been defined to scale, rotate, and translate sets of vertices. In Chapter 7, the I.S.R.O.T. (Identity, Scale, Revolve, Orbit [translation and rotation], Translate) sequence of matrices is used to ensure balanced transformations. The same logic applies when you are using transformation matrices that have been cre ated manually. If the matrices are multiplied in an incorrect order, the transforma tions will also be incorrect.
 254 MICROSOFT XNA GAME STUDIO CREATOR’S GUIDE When matrix calculations are performed in XNA, they are applied using the Right Hand Rule perspective, which was explained in Chapter 7. This chapter applies the transformation matrices from a Right Hand Rule perspective to suit the XNA frame work. When you perform transformations on an object, the data matrix containing the X, Y, Z, and W coordinates is located on the left of the multiplication operator. The transformation matrix is located on the right. Translation Matrix Translation matrices store lateral transformations along the X, Y, and Z planes. Here is the format for the translation matrix:  1 0 0 0   0 1 0 0   0 0 1 0   X Y Z 1  When you are presented with a 4×4 matrix with 1s along the diagonal, values for X, Y, Z at the bottom, and 0s elsewhere, you can conclude the matrix will perform a translation of X units along the X plane, Y units along the Y plane, and Z units along the Z plane. Handling the W Component When a vector representing the X, Y, and Z coordinates of an object is transformed using a translation matrix, the W component in the fourth column of the data matrix must be set to 1. Failing to set all values in the fourth column of the data matrix to 1 will lead to inaccurate translations. Translation Matrix Example Imagine that the vertex (X=2, Y=1, Z=0) is transformed by the matrix on the right. The vector data matrix is located on the left. Note that the fourth column represent ing the W component is set to 1. The translation matrix for the format described here must be located on the right side of the operator for the calculation to work properly.  2 1 0 1  X  1 0 0 0   0 1 0 0   0 0 1 0   3 5 0 1 
 C H A P T E R 1 6 255 Matrices Viewing this vertex and translation matrix gives you enough information to deter mine that the vertex with the coordinates X=2, Y=1, and Z=0 will be transformed by three units in the positive X direction, and five units in the positive Y direction. If this is correct, the product of the vertex and translation matrix should move the vertex to X=5, Y=6, and Z=0. Figure 161 shows the coordinate in its original position before the predicted translation (on the left) and after the predicted translation (on the right). To verify the prediction, you can try this calculation in code. To set up the data matrix, replace the code that initializes the cells in matrix A with this revision to ini tialize the vector data. The remaining rows will take on the default of 0 in each cell. // store vector in first row  all other cells equal 0 by default A.M11 = 2.0f; A.M12 = 1.0f; A.M13 = 0.0f; A.M14 = 1.0f; Next, to set up the translation matrix, replace the code that assigns matrix B with this revision: B.M11 = 1.0f; B.M12 = 0.0f; B.M13 = 0.0f; B.M14 = 0.0f; B.M21 = 0.0f; B.M22 = 1.0f; B.M23 = 0.0f; B.M24 = 0.0f; B.M31 = 0.0f; B.M32 = 0.0f; B.M33 = 1.0f; B.M34 = 0.0f; B.M41 = 3.0f; B.M42 = 5.0f; B.M43 = 0.0f; B.M44 = 1.0f; If you run this code, the output that appears in the window matches the prediction that the new coordinates are X=5, Y=6, and Z=0: 5.00 6.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 FIGURE 161 Translating an object with the translation matrix
 256 MICROSOFT XNA GAME STUDIO CREATOR’S GUIDE The translation moved the original vertex three units in the positive X direction, and five units in the positive Y direction. Translation Matrix Example Using the CreateTranslation() Method Since Chapter 7, we have used the method CreateTranslation(float x, float y, float z) to automatically generate the translation matrix. This method actually generates a translation matrix that is identical to the translation ma trix we just created manually. If you replace the code inside MultiplyMatrix() that assigns cell values to matrix B with the following instruction, you will generate an identical matrix: B = Matrix.CreateTranslation(3.0f, 5.0f, 0.0f); Therefore, when you compile and run the code, the product matrix will also be identical. Scaling Matrix Scaling matrices are used any time an object needs to be resized. You will often need to scale your 3D models because modeling tools usually generate them in a size that is different from the size needed for your game project. The following matrix represents a standard matrix for performing scaling operations. At a glance, this scaling matrix contains information to expand or shrink an object in the X, Y, and Z planes. The X, Y, and Z scaling factors are set on the diagonal down from the top left to the bottom right. The digit, one, is needed in the bottomright corner, and zeros are placed else where to make this matrix a scaling matrix. X 0 0 0 0 Y 0 0 0 0 Z 0 0 0 0 1 Scaling Matrix Example In this example, you will use a scaling matrix to double the size of a triangle. A trian gle is represented with the matrix containing the triangle vertices on the left. The ver tex coordinates used to build the triangle are ( (0, 0, 0), (1, 4, 0), (4, 2, 0) ). The scaling matrix that doubles the size of the triangle is on the right. In the first three rows of the data matrix on the left, the X, Y, and Z coordinates for the three triangle vertices are
 C H A P T E R 1 6 257 Matrices stored. One triangle vertex is stored in each of the first three rows. When multiplying the triangle vertices by the scaling matrix (to double the size), you can use the follow ing matrix equation:  0 0 0 0  X  2 0 0 0   1 4 0 0   0 2 0 0   4 2 0 0   0 0 2 0   0 0 0 0   0 0 0 1  By looking at the scaling matrix—and without performing any calculations—it is apparent that the size of the existing triangle is going to be doubled. In Figure 162, you can see that the size of the triangle has doubled when a vector set was trans formed with the scaling matrix. Inside MultiplyMatrix(), replace the code that assigns values to the cells of matrix A with the following revision to initialize the data matrix for the triangle: A.M11 = 0.0f; A.M12 = 0.0f; A.M13 = 0.0f; A.M14 = 0.0f; A.M21 = 1.0f; A.M22 = 4.0f; A.M23 = 0.0f; A.M24 = 0.0f; A.M31 = 4.0f; A.M32 = 2.0f; A.M33 = 0.0f; A.M34 = 0.0f; A.M41 = 0.0f; A.M42 = 0.0f; A.M43 = 0.0f; A.M44 = 0.0f; FIGURE 162 Before scaling and after scaling
 258 MICROSOFT XNA GAME STUDIO CREATOR’S GUIDE Next, replace the code that initializes matrix B with this version to initialize a scal ing matrix: B.M11 = 2.0f; B.M12 = 0.0f; B.M13 = 0.0f; B.M14 = 0.0f; B.M21 = 0.0f; B.M22 = 2.0f; B.M23 = 0.0f; B.M24 = 0.0f; B.M31 = 0.0f; B.M32 = 0.0f; B.M33 = 2.0f; B.M34 = 0.0f; B.M41 = 0.0f; B.M42 = 0.0f; B.M43 = 0.0f; B.M44 = 1.0f; When the program is run, the output displays coordinates for the triangle that has been doubled: 0.00 0.00 0.00 0.00 2.00 8.00 0.00 0.00 8.00 4.00 0.00 0.00 0.00 0.00 0.00 0.00 The triangle coordinates in the output matrix are graphed on the right side of Fig ure 162. Scaling Matrix Example Using the CreateScale() Method In Chapter 7, the CreateScale(float x, float y, float z) method was introduced as a way to automatically generate the scaling matrix. Replace the in structions that manually assign the scaling matrix with this simpler revision to gener ate an identical matrix: B = Matrix.CreateScale(2.0f, 2.0f, 2.0f); When you run the code, the output will be the same as before. Rotation Matrix X Axis The X rotation matrix is used to transform sets of vertices by an angle of θradians about the X axis:  1 0 0 0   0 cosθ sinθ 0   0 sinθ cosθ 0   0 0 0 1 
 C H A P T E R 1 6 259 Matrices Rotation Matrix X Axis Example This example applies the X rotation matrix to rotate a triangle by 45 degrees (π/4). The original set of coordinates (before the rotation) is in the left matrix, and the X ro tation matrix is located on the right:  0 0 0 0  X  1 0 0 0   1 4 0 0   0 cos(π/4) sin(π/4) 0   4 2 0 0   0 sin(π/4) cos(π/4) 0   0 0 0 0   0 0 0 1  If you were to multiply this by hand, the result would be 0.00 0.00 0.00 0.00 1.00 2.83 2.83 0.00 4.00 1.41 1.41 0.00 0.00 0.00 0.00 0.00 Figure 163 shows how the triangle would be positioned before and after the ro tation. Now we will show this implementation of the rotation matrix in code by using the solution from the previous example. To create a rotation matrix of π/4 radians about FIGURE 163 Rotation of a triangle using the X rotation matrix
 260 MICROSOFT XNA GAME STUDIO CREATOR’S GUIDE the X axis, replace the instructions that initialize matrix B with the following version inside MultiplyMatrix(): float sin = (float)Math.Sin(Math.PI / 4.0); float cos = (float)Math.Cos(Math.PI / 4.0); B.M11 = 1.0f; B.M12 = 0.0f; B.M13 = 0.0f; B.M14 = 0.0f; B.M21 = 0.0f; B.M22 = cos; B.M23 = sin; B.M24 = 0.0f; B.M31 = 0.0f; B.M32 = sin; B.M33 = cos; B.M34 = 0.0f; B.M41 = 0.0f; B.M42 = 0.0f; B.M43 = 0.0f; B.M44 = 1.0f; When you compile and run this code, the product matrix equals the result that you computed by hand: 0.00 0.00 0.00 0.00 1.00 2.83 2.83 0.00 4.00 1.41 1.41 0.00 0.00 0.00 0.00 0.00 This matrix stores the coordinates of the triangle after it has been rotated about the X axis, as shown in Figure 163. X Axis Rotation Example Using the CreateRotationX() Method Prior to this chapter, the CreateRotationX(float radians) method has been used to generate the same X rotation matrix as the manually created matrix. To cal culate the same transformation for the triangle, replace the initial declaration for the X rotation matrix with a matrix that is generated using the CreateRotationX() method: B = Matrix.CreateRotationX((float)(Math.PI / 4.0)); The resulting product is obviously the same, but the calculation requires less code. Rotation Matrix Y Axis The matrix shown here is a predefined matrix that rotates a set of vertices around the Y axis by θ radians:  cosθ 0 sinθ 0   0 1 0 0   sinθ 0 cosθ 0   0 0 0 1 
 C H A P T E R 1 6 261 Matrices Rotation Matrix Y Axis Example This example demonstrates the use of the Y rotation matrix to rotate a set of triangle coordinates by π/4 radians about the Y axis. The data matrix is on the left, and the Y rotation matrix is on the right:  0 0 0 0  X  cos(π/4) 0 sin(π/4) 0   1 4 0 0   0 1 0 0   4 2 0 0   sin(π/4) 0 cos(π/4) 0   0 0 0 0   0 0 0 1  If you multiplied this out by hand, the result would be  0 0 0 0   0.71 4 0.71 0   2.83 2 2.83 0   0 0 0 0  Figure 164 shows the triangle coordinates before and after the multiplication that performs the rotation. To implement the Y rotation in code, replace the code that initializes matrix B with a rotation matrix to rotate the vertices by π/4 radians: float sin = (float)Math.Sin(Math.PI / 4.0); float cos = (float)Math.Cos(Math.PI / 4.0); B.M11 = cos; B.M12 = 0.0f; B.M13 = sin; B.M14 = 0.0f; B.M21 = 0.0f; B.M22 = 1.0f; B.M23 = 0.0f; B.M24 = 0.0f; B.M31 = sin; B.M32 = 0.0f; B.M33 = cos; B.M34 = 0.0f; B.M41 = 0.0f; B.M42 = 0.0f; B.M43 = 0.0f; B.M44 = 0.0f; FIGURE 164 Y axis rotation before and after the transformation matrix is applied
 262 MICROSOFT XNA GAME STUDIO CREATOR’S GUIDE When you run this program, the product matrix stores the triangle’s new coordi nates after they are rotated by π/4 units around the Y axis (see Figure 164). Y Axis Rotation Example Using the CreateRotationY() Method In Chapter 7, the CreateRotationY(float radians) method was used to gen erate an identical Y rotation matrix as the one presented in this chapter. You can re place the code that initializes matrix B with the following instruction and it will produce the same result: B = Matrix.CreateRotationY(MathHelper.Pi/4.0f); When you run this code, the product matrix will be the same as before, but this version requires less code. Rotation Matrix Z Axis The following matrix is the classic matrix for rotations of θ radians on the Z axis:  cosθ sinθ 0 0   sinθ cosθ 0 0   0 0 1 0   0 0 0 1  Rotation Matrix Z Axis Example In this example, the triangle coordinates on the left are transformed with the Z rota tion matrix by π/4 radians (45 degrees) about the Z axis:  0 0 0 0  X  cos(π/4) sin(π/4) 0 0   1 4 0 0   sin(π/4) cos(π/4) 0 0   4 2 0 0   0 0 1 0   0 0 0 0   0 0 0 1  When you calculate the multiplication by hand, the new triangle coordinates—af ter the rotation—will appear in the product matrix: 0.00 0.00 0.00 0.00 2.12 3.54 0.00 0.00 1.41 4.24 0.00 0.00 0.00 0.00 0.00 0.00
 C H A P T E R 1 6 263 Matrices FIGURE 165 Z axis rotation before and after the transformation matrix is applied Figure 165 shows the triangle before and after the rotation. To try this in code, replace the assignment of matrix B with the following code to create a rotation about the Z axis of π/4 radians: float sin = (float)Math.Sin(Math.PI / 4.0); float cos = (float)Math.Cos(Math.PI / 4.0); B.M11 = cos; B.M12 = sin; B.M13 = 0.0f; B.M14 = 0.0f; B.M21 = sin; B.M22 = cos; B.M23 = 0.0f; B.M24 = 0.0f; B.M31 = 0.0f; B.M32 = 0.0f; B.M33 = 1.0f; B.M34 = 0.0f; B.M41 = 0.0f; B.M42 = 0.0f; B.M43 = 0.0f; B.M44 = 1.0f; Z Axis Rotation Example Using the CreateRotationZ() Method The CreateRotationZ(float radians) matrix will generate a matrix identi cal to the one just declared for matrix B. Replacing the existing matrix assignment with this instruction will generate the same result: B = Matrix.CreateRotationZ(MathHelper.Pi/4.0f); Identity Matrix When a set of vertices is multiplied by the identity matrix, the product equals the original vertex matrix. In other words, nothing changes in the original data matrix. It may seem pointless to use the identity matrix since it does not actually perform a transformation. However, the identity matrix is included in the recommended I.S.R.O.T. sequence of transformations to ensure that the World matrix is initialized
 264 MICROSOFT XNA GAME STUDIO CREATOR’S GUIDE properly when no other transformation matrix is applied. By default, an identity ma trix is used in the World matrix to initialize it. The World matrix is explained in more detail in Chapter 17. The identity matrix is defined for a matrix that has 1s on the diagonal from the top left to the bottom right, and 0s elsewhere, as shown here:  1 0 0 0   0 1 0 0   0 0 1 0   0 0 0 1  Identity Matrix Example This example shows that when a data matrix is multiplied by an identity matrix, the result equals the data matrix. In other words, A*B=A (where B is an identity matrix). In this case, the vertices for a triangle are multiplied by the identity matrix. The prod uct equals the original set of vertices for the triangle:  0 0 0 0 X 1 0 0 0 = 0 0 0 0   1 4 0 0   0 1 0 0 = 1 4 0 0   4 2 0 0   0 0 1 0 = 4 2 0 0   0 0 0 0   0 0 0 1 = 0 0 0 0  To perform this calculation in code, replace the assignment for matrix B with this revision: B.M11 = 1.0f; B.M12 = 0.0f; B.M13 = 0.0f; B.M14 = 0.0f; B.M21 = 0.0f; B.M22 = 1.0f; B.M23 = 0.0f; B.M24 = 0.0f; B.M31 = 0.0f; B.M32 = 0.0f; B.M33 = 1.0f; B.M34 = 0.0f; B.M41 = 0.0f; B.M42 = 0.0f; B.M43 = 0.0f; B.M44 = 1.0f; When you run this code, the product matrix displayed in the game window equals matrix A (which defines the triangle). Identity Matrix Example Using Matrix.Identity Until now, the predefined matrix (Matrix.Identity) has been used for the iden tity matrix. This matrix is equivalent to the one you just created manually. If you re place the assignment for matrix B with B = Matrix.Identity; the outcome will be the same.
 C H A P T E R 1 6 265 Matrices Matrices enable transformations in 3D space. Understanding linear algebra and the defined transformation matrices will allow you to develop better graphics algorithms and have deeper control of your graphics engine. This will be especially helpful when you need to build your own matrices to perform transformations for vectors. See Chapter 8 and Chapter 19 for examples of when this technique is necessary. C HAPTER 16 REVIEW EXERCISES 1. Try the stepbystep examples discussed in this chapter. 2. Starting with a triangle with the coordinates A{0.23f, 0.2f, 0.1f) B{ 0.23f, 0.2f, 0.1f) C{ 0.0f, 0.2ff, 0.1f) manually compute the unit normal. Then manually translate the triangle, together with its unit normal, 2 units on Z, and –0.35 units on X. Scale the triangle and normal by 3.5 on X, Y, and Z. Rotate the triangle and normal by π/3 radians on X and π/4 radians on Z. When performing this transformation, do not use any variations of the following methods: CreateScale(float X, float Y, float Z); CreateRotationX(float radians); CreateRotationY(float radians); CreateRotationZ(float radians); CreateTranslation(float X, float Y, float Z); Cross(); Normalize(); When the program is run, the final result shows both the triangle and the triangle’s unit normal pointing out from it. Both the triangle and normal vector are Translated 2 units on Z and –0.35 units on X Scaled by 3.5 on X, Y, and Z Rotated π/3 radians on X Rotated π/4 radians on Z
 This page intentionally left blank
 CHAPTER 17 Building a Graphics Engine Camera
CÓ THỂ BẠN MUỐN DOWNLOAD

Microsoft XNA Game Studio Creator’s Guide P1
30 p  75  17

Microsoft XNA Game Studio Creator’s Guide P2
30 p  66  14

Microsoft XNA Game Studio Creator’s Guide P3
30 p  70  13

Microsoft XNA Game Studio Creator’s Guide P4
30 p  51  11

Microsoft XNA Game Studio Creator’s Guide P5
30 p  51  11

Microsoft XNA Game Studio Creator’s Guide P7
30 p  58  10

Microsoft XNA Game Studio Creator’s Guide P16
30 p  62  9

Microsoft XNA Game Studio Creator’s Guide P9
30 p  52  9

Microsoft XNA Game Studio Creator’s Guide P8
30 p  71  9

Microsoft XNA Game Studio Creator’s Guide P6
30 p  63  9

Microsoft XNA Game Studio Creator’s Guide P11
30 p  50  8

Microsoft XNA Game Studio Creator’s Guide P12
30 p  54  8

Microsoft XNA Game Studio Creator’s Guide P13
30 p  56  8

Microsoft XNA Game Studio Creator’s Guide P14
30 p  44  7

Microsoft XNA Game Studio Creator’s Guide P15
30 p  38  7

Microsoft XNA Game Studio Creator’s Guide P17
30 p  38  6

Microsoft XNA Game Studio Creator’s Guide P18
30 p  50  6