DirectX mathematical Basics

Source: Internet
Author: User
Vector (also called vector, or vector)

A vector is a volume that contains the size (length) and direction. A vector has two or even four dimensions. In all DX struct, there is a struct used to represent a three-dimensional vector, Which is d3dvector. This struct is very simple and has only three members: X, Y, and Z. Generally, if vector operations are not involved, use this struct to define a vector. We can use it to represent the direction and the position of the vertex in the 3D world. If you want to perform some operations on those vectors, it is inconvenient to use d3dvector, because the d3dvector struct does not overload any operators. If you want to perform an addition operation, you have to calculate each member in the struct respectively. Hey hey, don't worry. There is a d3dx header file in dx (including the d3dx. h header file), which defines a lot of functions and structures that facilitate our mathematical computation. There are three struct types: d3dxvector2, d3dxvector3, and d3dxvector4. Let's look at their names to know their functions. I will not talk about structures of 2 and 4 dimensions here. They are actually very simple, similar to d3dxvector3. However, it should be noted that d3dxvector3 is derived from d3dvector, indicating that it has three members, x, y, and z, like d3dvector. In addition, d3dxvector3 also loads a small number of arithmetic operators, so that we can perform addition, subtraction, multiplication, division, and determine whether the d3dxvector3 object is equal as an integer. At the same time, because d3dxvector3 is derived from d3dvector, the objects of the two can be assigned values to each other and can be converted randomly in the two types.

Let's simply talk about the mathematical operation of vectors. The addition and subtraction of vectors is simple, that is, the addition and subtraction of each component of the two vectors respectively. The multiplication and division of a vector is also very simple. It can only perform multiplication and division on a single value. The result of the calculation is the result of multiplication and division of the value by each component in the vector. The model of a vector is the length of the vector, that is, the square and square of each component. Vector standardization is to make the model of the Vector 1, which is very useful for implementing illumination in the 3D world. There are also two multiplication methods for vector operations, namely, dot multiplication and cross multiplication. The result of point multiplication is that the modulus of the two vectors are multiplied by the cosine of the angle between the two vectors. Or the sum of the result of the multiplication of each component of two vectors. Obviously, the result of dot multiplication is a number, which is very helpful for us to analyze the characteristics of these two vectors. If the result of point multiplication is 0, the two vectors are perpendicular to each other. If the result is greater than 0, the angle between the two vectors is smaller than 90 degrees. If the result is less than 0, then the angle between the two vectors is greater than 90 degrees. As for the cross-multiplication, its formula is dizzy, so I will not mention it. Let's take a look at the formula below for your understanding ......

// V3 = V1 x v2
V3.x = v1.y * v2.z-v1.z * v2.y
V3.y = v1.z * v2.x-v1.x * v2.z
V3.z = v1.x * v2.y-v1.y * v2.x

Is it hard to remember? If you cannot remember it for the moment, forget it. In fact, we mainly need to know the meaning of the Cross multiplication. Unlike the result of dot multiplication, the result of cross multiplication is a new vector, which is perpendicular to the original two vectors. As for its direction, I wonder if you still remember the left-hand rule. Come, stretch out your left hand and follow the first vector (V1) to point to the second vector (V2) to bend your palm. Then your thumb points to the new vector (V3). Through cross multiplication, we can easily obtain the normal of a certain plane (determined by two vectors.

Finally, after writing the above text, it was really hard to describe the mathematical problem, and I was reluctant to draw pictures. It doesn't matter if you think the text above is boring. Because the above is not the focus, the functions described below will be remembered.

D3dx has many useful functions that can help us implement all the operations mentioned above. But below I will only talk about the functions related to d3dxvector3:

Compute point multiplication: Float d3dxvec3dot (
Const d3dxvector3 * pv1,
Const d3dxvector3 * pv2)

Calculate the cross multiplication: d3dxvector3 * d3dxvec3cross (
D3dxvector3 * pout,
Const d3dxvector3 * pv1,
Const d3dxvector3 * pv2)

Computing Mode: Float d3dxvec3length (
Const d3dxvector3 * PV)

Standardized vector: d3dxvector3 * d3dxvec3normalize (
D3dxvector3 * pout,
Const d3dxvector3 PV)

For the addition, subtraction, multiplication, division, and Division operations of d3dxvector3, as mentioned above, use +.

Matrix and matrix operations

What is a matrix? This concept is really hard to explain, but those who have studied linear algebra certainly know what the matrix looks like, so I will not explain it here. In d3d, the struct of the defined matrix is d3dmatrix:

Typedef struct _ d3dmatrix {
Union {
Struct {
Float _ 11, _ 12, _ 13, _ 14;
Float _ 21, _ 22, _ 23, _ 24;
Float _ 31, _ 32, _ 33, _ 34;
Float _ 41, _ 42, _ 43, _ 44;
};
Float M [4] [4];
};
} D3dmatrix;

Looking at this structure, you should know how to use it to define a matrix. Here I will introduce the Union feature in C ++. As shown in the struct defined above, there are two parts in Union: one is the struct and the other is a two-dimensional array, which has 16 elements. In union, all members share a memory block. What does this mean? Looking at the code above, the first element of the member _ 11 in the struct and the member M array share the same memory space, that is, their values are the same, you assign a value to _ 11 and M [0] [0]. The values of _ 11 and M [0] [0] are the same. What are the benefits? For example, you have defined a matrix variable d3dmatrix mat. To access the elements in the fourth column of the third row of the matrix, you can do this: mat. _ 34; alternatively, Mat. M [2] [3] (the array is stored from position 0 ). It seems that using the latter is troublesome, but when you replace the numbers in brackets with I and j, use mat. M [I] [J] is used to access the elements in the Matrix. You should know its advantages.

In fact, there are not many cases of using d3dmatrix directly, because there is a better struct in d3dx, that is, d3dxmatrix. Similar to d3dxvector3, d3dxmatrix is inherited from d3dmatrix. It overload many operators and makes the matrix operation very simple. I am not going to talk about the calculation method of the matrix. The following describes only the three functions related to the matrix nature.

Generate a matrix of units: d3dxmatrix * d3dxmatrixidentity (
D3dxmatrix * pout); // return the result

Transpose matrix: d3dxmatrix * d3dxmatrixtranspose (
D3dxmatrix * pout, // returned result
Const d3dxmatrix * PM); // target matrix

Inverse Matrix: d3dxmatrix * d3dxmatrixinverse (
D3dxmatrix * pout, // returned result
Float * pdeterminant, // set to 0
Const d3dxmatrix * PM); // target matrix

As for what is a matrix of units, what is a transpose matrix, and what is an inverse matrix, I will not talk about it. You can read the linear algebra book and understand it at a glance. The simple addition, subtraction, multiplication, division, can use the overloaded operators in the d3dxmatrix struct. The multiplication of two matrices can also be implemented using functions, which will be discussed in the next matrix transformation.

Matrix Transformation

There are three basic transformations of a matrix: translation, rotation, and scaling.

Translation:
D3dxmatrix * d3dxmatrixtranslation (
D3dxmatrix * pout, // returned result
Float X, // The translation volume on the X axis
Float y, // The translation volume on the Y axis
Float Z) // The translation volume on the Z axis

Rotate around the X axis:
D3dxmatrix * d3dxmatrixrotationx (
D3dxmatrix * pout, // returned result
Float angle // rotating radians
);

Rotate around Y axis:
D3dxmatrix * d3dxmatrixrotationy (
D3dxmatrix * pout, // returned result
Float angle // rotating radians
);

Rotate around the Z axis:
D3dxmatrix * d3dxmatrixrotationz (
D3dxmatrix * pout, // returned result
Float angle // rotating radians
);

Rotate around the specified axis:
D3dxmatrix * d3dxmatrixrotationaxis (
D3dxmatrix * pout, // returned result
Const d3dxvector3 * PV, // specify the axis Vector
Float angle // rotating radians
);

Scaling:
D3dxmatrix * d3dxmatrixscaling (
D3dxmatrix * pout, // returned result
Float Sx, // The amount of scaling on the X axis
Float Sy, // The amount of scaling on the Y axis
Float SZ // Number of zooming on the Z axis
);
 

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.