View Matrix
Also known as the camera matrix, it is composed of three parts, namely, the camera position, the target position and the camera up and down direction. In DirectX, a method is provided to form a view matrix through these three parts, all three of which are space vectors (VECTOR3). In the process of forming the view matrix, the following figure shows the the unit vector which takes the camera position as the starting point and the camera target position as the endpoint is the z ' axis coordinate of the camera space (relative to the World space), then calculates the x ' axis coordinate of the camera space according to the left-hand rule or the right hand rule, and calculates the y ' axis coordinate of the camera So the view matrix holds the component of the camera Space X ', y ' and z ' axis coordinates in the x, y, and z directions, as well as the position of the world coordinate origin in the camera space.
In DirectX, there are two ways to create a view matrix, namely MATRIX.LOOKATLH () and MATRIX.LOOKATRH (), which are used to create the view matrix of the left-hand rule and the view matrix of the right-hand rule, and the following is a description of the left-hand rule's view matrix, which is defined as follows:
public static Matrix Lookatlh (Vector3 cameraposition, Vector3 cameratarget, Vector3 cameraupvector);
Where the parameter cameraposition is used to specify the camera position, the parameter cameratarget is used to specify the camera target position, and the parameter cameraupvector is used to specify the direction of the current world coordinate upward, generally (0,1,0).
The vectors for each axis of the view space can be computed, based on the principles described earlier, as follows:
Camerazaxis = Normalize (cameratarget-cameraposition)
Cameraxaxis = Normalize (Cross (Cameraupvector, Camerazaxis))
Camerayaxis = Cross (Camerazaxis, Cameraxaxis)
Where normalize represents the unit vector of the calculated vector, cross represents the vector product that computes the two vectors. The values corresponding to the individual elements in the view matrix are:
Cameraxaxis.x camerayaxis.x camerazaxis.x 0
Cameraxaxis.y camerayaxis.y Camerazaxis.y 0
Cameraxaxis.z camerayaxis.z camerazaxis.z 0
DXY DYP DZP 1
which
DXY =-dot (cameraxaxis,cameraposition)
DYP =-dot (Camerayaxis, cameraposition)
DZP =-dot (Camerazaxis, cameraposition)
The dot represents the quantity product that computes the two vectors. The following code is used to compute the view matrix:
Vector3 cameraposition = new Vector3 (0.0f, 5.0f, 10.0f);
Vector3 cameratarget = new Vector3 (0.2f, 0.0f, 0.0f);
Vector3 cameraupvector = new Vector3 (0, 1, 0);
Vector3 Camerazaxis = vector3.normalize (cameratarget-cameraposition);
Vector3 Cameraxaxis = vector3.normalize (Vector3.cross (Cameraupvector, Camerazaxis));
Vector3 Camerayaxis = Vector3.cross (Camerazaxis, Cameraxaxis);
Matrix Viewmatrix=matrix.zero;
VIEWMATRIX.M11 = cameraxaxis.x; VIEWMATRIX.M12 = camerayaxis.x; Viewmatrix.m13 = camerazaxis.x;
Viewmatrix.m21 = Cameraxaxis.y; VIEWMATRIX.M22 = Camerayaxis.y; VIEWMATRIX.M23 = Camerazaxis.y;
VIEWMATRIX.M31 = cameraxaxis.z; Viewmatrix.m32 = camerayaxis.z; Viewmatrix.m33 = camerazaxis.z;
Viewmatrix.m41 =-vector3.dot (Cameraxaxis, cameraposition);
VIEWMATRIX.M42 =-vector3.dot (Camerayaxis, cameraposition);
VIEWMATRIX.M43 =-vector3.dot (Camerazaxis, cameraposition);
VIEWMATRIX.M44 = 1;
Console.WriteLine ("View Matrix:");
Showmatrix (Viewmatrix)//Display view matrix
The Vector3.normalize () method is used to compute the unit vector of vectors, the Vector3.cross () method is used to compute the vector product of two vectors, and the Vector3.dot () method is used to compute the quantity product of two vectors. Run the program with the results shown in the following illustration:
If you use the MATRIX.LOOKATLH () method in DirectX to create the view matrix, the code is as follows:
Vector3 cameraposition = new Vector3 (0.0f, 5.0f, 10.0f);
Vector3 cameratarget = new Vector3 (0.2f, 0.0f, 0.0f);
Vector3 cameraupvector = new Vector3 (0, 1, 0);
Matrix Viewmatrixcomp = MATRIX.LOOKATLH (cameraposition, Cameratarget, cameraupvector);
Console.WriteLine ("View Matrix:");
Showmatrix (Viewmatrixcomp)//Display view matrix
Run the program with the results shown in the following illustration: