Direct3D Vertex Coordinate transformation

Source: Internet
Author: User

start with: Vertex coordinate transformation when Direct3D learn the basics of getting started, here will detail its principles:

The process of rendering three-dimensional objects in Direct3D is divided into two stages: "1" t&l (transforming and Lighting), i.e. coordinate transformation and illumination;

"2" grating processing stage.

One, t&l assembly line:

The following figure:

1, world transformation and world coordinate system (local coordinates become world coordinates):

The deformation and movement of objects in three-dimensional space is called the World transformation (translation, rotation, scaling), this three-dimensional space is the world space, and its coordinate system is three-dimensional coordinate system

The world transformation is in fact the object vertex from the model space to the world space, model space is actually in three-dimensional design software (such as 3DSMAX) for the object set in the coordinate system, also known as the local coordinate system. The world coordinate is that all objects use the coordinate system of the same world coordinate origin, the transformation refers to the translation of the model, rotation, scaling and any combination of their transformations.

Use the following formula to make a world transformation for Point P1.

P2 (x,y,z,1) =dot (P1 (x,y,z,1), M)

M is the world change matrix, it realizes the object's translation, rotation, scaling, dot is the point multiplication, the P2 is the transformed coordinates, the P1 is the coordinate before the transformation.

The following is the DirectX code (c + +) that implements the world Coordinate matrix transformation: D3dxmatrix matworld; World transformation matrix
D3dxmatrix Mattranlate,matrotation,matscale; Transformation matrix, rotation matrix, scaling matrix
D3dx matrixscaling (& Matscale, 1.0f, 1.0f, 5.0); 5 times x magnification on Z axis
FLOAT fangle = * (2.0f * d3dx_pi)/360.0f; Calculate the angle that needs to be rotated
D3dxmatrixrotationy (& Matrotation,fangle); Rotate 60 degrees around the y-axis
D3DXMatrixMultiply (& Mattranlate, & Matscale, & Matrotation); Combination of two matrices
D3DXMatrixTranslation (& Mattranlate, 30.0f, 0.0f, 0.0f); Pan 30 units along X
D3DXMatrixMultiply (& Matworld, & Matworld, & Mattranlate); Combine to get the world changing matrix
g_pd3ddevice-SetTransform (D3dts_world, & Matworld); Set the world transformation matrix for direct devices

With the above code, each object that will be output is placed in the world coordinate system by the same transformation matrix (there is a chance to make up for the detailed calculation method of no transformation)

2, observe the transformation and observation coordinate system (world coordinates change to the coordinates of the camera position as the origin):

Observing the coordinate system is the camera (the picture shown on the screen is the scene of the virtual camera shot on the film) for the camera position as the origin, the camera to observe the direction of the z axis to establish a coordinate system, he changed from the world coordinates to observe the coordinates is to observe the transformation.

The principle is as shown in the figure below (three pictures from left to right)

By observing transformation matrices using world coordinate points, the transformation matrices are observed to reposition all the objects in the world coordinate system according to the orientation of the camera in the world coordinate system and the direction of its observation. The following code is a method for setting the observed transformation matrix: D3dxvector3 veyept (0.0f, 3.0f,-5.0f); Where the camera is located in the world coordinate system
D3dxvector3 vlookatpt (0.0f, 0.0f, 0.0f); Location of the camera's observer point
D3dxvector3 Vupvec (0.0f, 1.0f, 0.0f); The upward amount of the camera, usually (0,1,0) refers to the y-axis
D3dxmatrix Matview;
D3DXMATRIXLOOKATLH (& Matview, & Veyept, & Vlookatpt, & Vupvec); Generate an observation transformation matrix, which is a dedicated function for left-handed coordinate system
G_od3ddevice-Settransfrom (D3dts_view, & Matview); Set Observation transformation matrix

3, Light:

Because the coordinate transformation is not involved (temporary)

4, the projection transformation and the projected coordinate system (the point of the go-to-cut head body into the front rectangle of the body):

Projecting a three-dimensional object on the observed coordinate system onto a two-dimensional surface is a projection transformation whose coordinate system takes the film Center as the reference origin (coordinates of this coordinate system are floating point coordinates).

Projection transformations have two basic methods in DirectX: orthographic and Perspective projections.

(1) Orthogonal projection (feeling less used):

Object coordinates are projected parallel to the observation plane along the z axis of the observing coordinate system, and the distance between the observer and the observation plane does not affect the size of the object. The framing range is a rectangular

Code: D3DXMATRIX matproject;
D3DXMatrixOrthoLH (& Matproject,w,h,znear,zfar); W,h are wide and high, znear and Zfar are the nearest and longest distances, and the transformation matrix is obtained after use.
g_pd3ddevice-SetTransform (d3dts_projection, & Matproject);

(2) Perspective projection:

The principle is to convert a framing head body into a cube, since the proximal end of the truncated body is smaller than the remote, so the object at the proximal end is magnified when it becomes a cube. The farther away the object is from the camera the smaller the image.

A function is provided here: D3dxmatrix ProjectionMatrix (const float Near_plane,//distance to near Trim
Const float Far_plane,//distance to the far cutting surface
Const float Fov_horiz,//horizontal range of viewing angle (radians)
const FLOAT Fov_vert)//Vertical range (radians) of the viewing angle
{
float h,w,q;
W = (float) 1/tan (Fov_horis * 0.5); Cot (FOV_HORIS/2)
h = (float) 1/tan (Fov_vert * 0.5); Cot (FOV_HORIS/2)
Q = Far_plane/(Far_plane-near_plane);
D3dxmatrix Matproject;
ZeroMemory (& Matproject, sizeof (Matproject));
Matproject (0, 0) = W;
Matproject (1, 1) = h;
Matproject (2, 2) = Q;
Matproject (3, 2) =-Q * Near_plane;
Matproject (2, 3) = 1;
return matproject;
}

Get the matrix (the parameters of this matrix are obtained from the above function, because the drawing can be said to understand, so forget, interested can go to see the relevant information)

W 0 0 0

0 H 0 0

0 0 Q 1

0 0-QZ 0

This matrix is the projection transformation matrix. You can use Directxapi

D3DXMATRIXPERSPECTIVEFOVLH () to construct a projection matrix.

5, viewport transformations and screen coordinates (to screen pixels):

Converting projected coordinates to pixel-based screen coordinates requires you to define the size of the viewport.

This first gives a struct:

typedef struct _ D3dviewport9
{
DWORD x; X-coordinate of the upper-left corner of the viewport
DWORD y; The y-coordinate of the upper-left corner of the viewport
DWORD Width; Viewport width
DWORD Height; Viewport height
float MinZ; The minimum depth of the scene within the viewport, 0.0f~1.0f
float MaxZ; The maximum depth of the scene within the viewport, 0.0f~1.0f
}

The following is a code that sets the viewport:

Rect rect;
GetClientRect (hWnd, & rect); HWND is a handle to the drawing window
D3dviewport9 Pviewport = {0, 0, Rect.right,rect.bottom, 0.0f, 1.0f};//definition D3dviewport
if (successed (Pd3ddevice-SetViewport (& Pviewport))//Set viewport
{

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.