DirectX III - Rendering Pipeline and Coordinate System (Direct3D9)
Direct3D rendering pipeline
Drawing a model using Direct3D requires a process called
Rendering pipeline Or
Rendering pipeline The rendering pipeline can be divided into the following processes:
Additionally, it should be noted that Direct3D uses a left-hand coordinate system, as shown in the following figure:
.
Local coordinates It represents the coordinates required for a single model, usually using the center of the model as the origin of the local coordinate system. And the model generally stores the vertices of the model, but some vertex formats have normals, some have texture left, Direct3D uses Flexible Vertex Formats To represent vertices, such as:
struct Vertex
{
Vertex(){}
Vertex(float x, float y, float z){
_x = x; _y = y; _z = z;
}
float _x, _y, _z;
static const DWORD FVF;
};
const DWORD Vertex::FVF = D3DFVF_XYZ;
- Only vertices are used here, and FVF represents the representation of vertices. Specific optional parameters can refer to MSDN
World Coordinate System
World coordinates In fact, it is the position of the model in the world coordinate system. In the case of multiple models, there must be a unified coordinate system to represent the relative positions between the models. This coordinate system is World coordinate system . The conversion of local coordinates to world coordinates can be achieved through the following function:
D3DXMATRIX worldMatrix;
D3DXMatrixTranslation(&worldMatrix, 1.0f, 0.0f, 0.0f);
Device->SetTransform(D3DTS_WORLD, &worldMatrix);
- Here
worldMarix
is a matrix object ofD3DXMATRIX
type.D3DXMatrixTranslation
is a translation transformation function, similar to rotation and scaling functions. Here is a list:
D3DXMATRIX *D3DXMatrixRotationX(D3DXMATRIX* pOut, FLOAT Angle);
D3DXMATRIX *D3DXMatrixRotationY(D3DXMATRIX* pOut, FLOAT Angle);
D3DXMATRIX *D3DXMatrixRotationZ(D3DXMATRIX* pOut, FLOAT Angle);
D3DXMATRIX *D3DXMatrixScaling( D3DXMATRIX* pOut, FLOAT sx, FLOAT sy, FLOAT sz);
Observing the coordinate system
Observing the coordinate system can be said to refer to the world coordinate system with the camera as the origin, which is actually the process of Camera transformation to world origin Place and face in the positive direction of the Z-axis. Because the images we ultimately see are calculated through the camera, placing the camera at the origin facilitates subsequent calculations. The setting process is as follows:
D3DXVECTOR3 position( 0.0f, 0.0f, -5.0f );
D3DXVECTOR3 target( 0.0f, 0.0f, 0.0f );
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f );
D3DXMATRIX V;
D3DXMatrixLookAtLH(&V, &position, &target, &up);
Device->SetTransform(D3DTS_VIEW, &V);
Back Hidden
Due to the fact that a polygon has two sides, one called the front and the other called the back, back culling is the process of eliminating polygons with the back facing the camera. By default, Direct3D considers
Vertex clockwise The arranged polygons are facing the camera from the front (observing the coordinate system). It can be achieved through Device->SetRenderState(D3DRS_CULLMODE, Value);
Control back culling . The value is as follows:
- DDCULL_ NONE Disable back culling;
- DDCULL_ CW Only perform hidden operations on triangles that wrap clockwise;
- D3DCULL_ CCW The default value only applies hidden edges to triangles in counterclockwise order;
Lighting is the calculation of surface color based on the light source, without going into detail for now. The camera simulates the human eye and can only see objects within a conical range, which becomes the perspective cone. Cropping is the process of cutting out objects that the camera cannot see.
ProjectionProjection is the process of converting the coordinates of objects within the perspective cone into coordinates on a two-dimensional plane; The process is as follows:
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI * 0.5f, float(screenWidth) / screenHeight, 1.0f, 1000.0f);
Device->SetTransform(D3DTS_PROJECTION, &proj);
- Obtained after transformation The range of coordinates (x, y) is (-1~1, -1~1) This process can also obtain deep caching, which is actually Z cache, with a range of (0-1) .
Viewport transformation is the process of converting the projected 2D left side into the actual coordinates on the screen. The screen coordinates here refer to the pixel coordinates relative to the current window. The process is as follows:
D3DVIEWPORT9 vp = {0, 0, screenWidth, screenHeight, 0, 1};
Device->SetViewport(&vp);
- The final parameter 0,1 is the depth limit range, where 0-1 indicates that all objects within the viewing cone will be displayed.
The so-called rasterization is to rasterize a polygon into pixels one by one, only in this way can the pixels on the screen be colored. This step only requires understanding.
Attention
Here are some details that are prone to errors:
- Direct3D is created using [1,4] or [1,3] Row vector To store point coordinates, which means, Vertex transformation is achieved by transforming a matrix through a row vector To complete. If there are two transformation matrices, in order of order, they should be PreMatrix nextMatrix ;
- In the viewport coordinate system, is represented by The top left corner of the window is the origin (0,0) The horizontal axis is the x-axis, and the right axis is positive; The vertical axis is the y-axis, and the downward axis is positive;
- If there is a vertex format that uses Texture coordinates , then The upper left corner of the texture is the origin (0,0) At the bottom right corner (1,1), the horizontal axis is the x-axis and the vertical axis is the y-axis.