Managed DirectX + C # Development (Getting Started) (6)

Source: Internet
Author: User

Chapter 5 matrix transformation I. Why is a 4x4 matrix used?During 3D programming, a 4 × 4 matrix is used for matrix transformation. Beginners often think that since it is a three-dimensional space, why not use a 3 × 3 matrix? This is because the matrix of 3x3 cannot represent some transformations, such as translation, projection, and reflection. Therefore, it is increased to 4x4, so that more transformations can be described. But the matrix is changed to 4 × 4. to multiply the vector and matrix, we need to increase the vector to 1 × 4, because the line matrix of 1 × 3 cannot be multiplied by the 4 × 4 matrix, how can we use the fourth member (represented by W? When a vertex is placed in a 1 × 4 row matrix, W = 1 indicates that the vertex can be properly translated. If W = 0, the vector cannot be translated. For example P= (1, 2, 3) place it in a row vector, like this [1, 2, 3, 1 ]. V= (21, 32, 33) place it in a row vector like this [21, 32, 33, 0]; Ii. Matrix TranslationParallel movement of graphics is the most basic operation: You can use the following matrix ( X, Y, Z,1) Move along the X axis P XUnits, moving along the Y axis PYUnits, moving along the Z axis PZUnits: the following code sets a vector and applies a translation transformation to it to obtain a new vector. For the complete code, see program: Private void button#click (Object sender, system. eventargs e) {clear (); // define a test vector; vector3 myvect = new vector3 (, 8); // use this vector to save subsequent results; vector4 resultvect = new vector4 (); // generates a translation matrix. Four units are added to the XYZ direction, matrix matrixa = matrix. translation (, 4); // apply the translation transformation to the vector; resultvect = vector3.transform (myvect, matrixa); // display the returned vector; label9.text = resultvect. tostring (); // display the Matrix. Di Splaymatrix (matrixa, label1);} program execution result. Execute the program and click the button. The execution result is as follows: apply a specific instance: 1: Create a project, generate a XOZ horizontal plane (Code omitted): 2: define two mesh objects: mesh mesh1 = NULL, mesh2 = NULL; 3: instantiate it in the graphics initialization function, here, two balls of the same radius are generated: Public bool initializegraphics (){...... Mesh1 = mesh. sphere (device, 0.5f, 20,20); mesh2 = mesh. sphere (device, 0.5f, 20,20 );
.......} 4: In the Rendering scenario function: One Ball performs translation transformation, and the other does not perform any transformation: Private void render () {If (device = NULL) return; device. clear (clearflags. target, system. drawing. color. black, 1.0f, 0); setupcamera (); device. beginscene (); displayplane (); device. transform. world = matrix. translation (10, 0, 0); mesh1.drawsubset (0); device. transform. world = matrix. identity; mesh2.drawsubset (0); device. endscene (); device. present ();} program execution result: Iii. Matrix RotationIf a graph is rotated around a fixed point or line, the result is as follows: the matrix in Figure 9 rotates a vector around X, Y, and Z axes. Note that the value here should be a radian value. When you look down the source of the axis, the angle refers to the clockwise angle. Example 1: The following test results after Rotation Transformation of a constant application: Private void button2_click (Object sender, system. eventargs e) {clear (); vector3 myvect = new vector3 (3,6, 8); vector4 resultvect = new vector4 (); matrix matrixa = matrix. rotationx (float) math. PI/2); resultvect = vector3.transform (myvect, matrixa); label9.text = resultvect. tostring (); displaymatrix (matrixa, label1) ;}here, we make this vector Rotate 90 degrees around the X axis, as you can imagine a straight line OA rotates 90 degrees around the X axis, the straight line OB is obtained. The point B value is the vector value. The execution result is as follows: Example 2: Use a specific example: 1: Create a project and generate a XOZ horizontal plane (Code omitted): 2: define two mesh objects: mesh mesh1 = NULL, mesh2 = NULL; 3: instantiate the image in the initialization function. Here, two cubes are generated: Public bool initializegraphics (){...... Mesh1 = mesh. Box (device, 0.5f, 0.5f, 16); mesh2 = mesh. Box (device, 0.5f, 0.5f, 16 );......} 4: In the Rendering scenario function: rotate a cube 45 degrees around the X axis, and the other cube is drawn at the origin: Private void render (){...... Device. transform. world = matrix. rotationx (float) math. PI/4); mesh1.drawsubset (0); device. transform. world = matrix. identity; mesh2.drawsubset (0 );......} The program execution result is as follows: The above is about rotating around the X axis, similar to rotating around the Y and Z axes, and the matrix is: Iv. Matrix ScalingThe following matrix scales the vector along the X axis: Q XUnits, zooming along the Y axis QYUnits, zooming along the Z axis QzUnits: Note: if the value in each direction is greater than 1, it is scaled down if the value is smaller than 1. Example 1: Private void button3_click_1 (Object sender, system. eventargs e) {clear (); vector3 myvect = new vector3 (3,6, 8); vector4 resultvect = new vector4 (); matrix matrixa = matrix. scaling (2, 0.5f, 0.5f); resultvect = vector3.transform (myvect, matrixa); label9.text = resultvect. tostring (); displaymatrix (matrixa, label1);} in the Code, the vector x direction is expanded to 2 times, and the Y and Z directions are reduced to 2 times. The vector value is: example 2: The following Code creates one of the two cubes Apply the scaling transformation, in which the zooming in the X and Y directions is twice the original, and the zooming in the z direction is twice the original. The related code is as follows :...... Device. transform. world = matrix. scaling (2,2, 0.5f) * matrix. rotationx (float) math. PI/4); mesh1.drawsubset (0); device. transform. world = matrix. identity; mesh2.drawsubset (0 );...... The program execution result is as follows: V. combination transformationIn general, we need to perform a series of transformations on the vector. For example, in order not to merge the two cubes shown above, the two are scaled first and then rotated. In fact, other transformations, such as translation, can be applied. Assume that the vector is P= [5, 0, 0, 1] scale down to 1/5 of all the original axes, then rotate π/4 along the Y axis, and finally move one unit on the X axis, move two units on the Y axis and three units on the Z axis. Set the scaling, rotating, and moving transformation matrices S, R Y, TAs follows: Therefore, the final result is obtained. Another simple method is to combine the three transformation matrices into a matrix by multiplying the matrices. Note that the order should not be reversed, otherwise the results will be different. So PQ= [1.707, 2,-3.707, 1]. The result is the same as that obtained by the first method; 6. Other common TransformationsThere are two matrices, which are often used to set the field shadows and cameras: 1: device. Transform. projection is used to obtain or set the projection transformation matrix. It is also a matrix variable. 2: matrix. perspectivefovlh method: public static matrix perspectivefovlh (float fieldofviewy, // visual range in Y direction, expressed in radians, that is, float aspectratio in Y direction, // the aspect ratio of Visual Range float znearplane, // near crop plane float zfarplane // far crop plane). The function of defining the aspect ratio is to correctly display the object size if the window size is changed. The last two parameters are the far and near cropping planes. Two z values must be specified for the two planes. Generally, if an object is moving between 1.0f and 1000f on the Z axis, set these two parameters to 1.0f and 1000.0f respectively. 3: matrix. lookatlh method. After using the observation matrix to create a three-dimensional camera model, you need to set different observation points. You can use this method to set the camera. Public static matrix lookatlh (vector3 cameraposition, // camera position vector3 cameratarget, // camera direction vector3 cameraupvector // camera up vector); the second parameter is the face direction of the camera, if the camera is facing the Z axis direction, set it to (0, 0, 1 ).

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.