Principle of vertex format Index View Matrix Projection Matrix
Vertex format
Custom two vertex formats
Struct ColorVertex {
Float _ x, _ y, _ z; // location
DWORD _ color; // color
};
Struct NormalTecVertex {
Float _ x,-y, _ z; // location
Float _ nx, _ ny, _ nz; // vertex normal
Float _ u, _ v; // texture coordinate
};
After the Vertex structure is defined, the desired combination of Flexible Vertex formats (Flexible Vertex Format, FVF) is used to describe the Vertex structure.
# Define FVF_COLOR (D3DFVF_XYZ | D3DFVF_DIFFUSE)
The vertex structure in the vertex format contains the position attribute and the diffuse color attribute.
# Define FVF_NORMAL_TEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1)
A convention must be followed when defining flexible vertex formats: the specified sequence of flexible vertex formats must be consistent with the sequence defined by the corresponding class of data in the vertex structure.
Indexing principles
The index is introduced to solve the problem of multi-faceted shared vertices.
The principle is as follows: Create a vertex list and an index list vertex list that contains all independent vertices, these indexes stipulate that triangle units should be organized in a certain way with each vertex.
For example
Vetex VertecList [4] = {v0, v1, v2, v3 };
The index list is used to define the organization of vertex lists to form two triangle units.
Word indexlist [6] = {0, 1, 2, // triangle0
0, 2 3}; // triangle1
Define the view Matrix
The view matrix defines the position and view of the viewpoint. The viewport matrix is the scene camera.
D3dxmatrix * d3dxmatrixlookatlh {
D3dxmatrix * pout,
Const d3dxmector3 * peeys,
Const d3dxmector3 * Pat,
Const d3dxmector3 * pup,
}
The peye parameter specifies the camera position in the world coordinate system. The Pat parameter specifies that the pup parameter in the world coordinate system is a vector in the world coordinate system that represents the "up" direction.
The following snippet creates a matrix of the view and sets the current matrix to the direct3d device.
D3dxvector3 veyept (0.0f, 3.0f,-5.0f );
D3dxvector3 vlookatpt (0.0f, 0.0f, 0.0f );
D3dxvector3 vupvec (0.0f, 1.0f, 0.0f );
D3dxmatrixa16 matview;
D3dxmatrixlookatlh (& matview, & veyept, & vlookatpt, & vupvec );
G_pd3ddevice-> settransform (d3dts_view, & matveiw );
The first step is to call D3DXMatrixLookAtLH () to define the PV matrix. The first parameter is the result of the pointer to the D3DXMATRIX struct to save the operation. Parameters 2, 3, and 4 define the eye position, orientation, and upward direction in sequence. Here, the eyes are moved up to three units after the Z axis. The viewpoint orientation is set to the origin point, and the upward direction is the positive direction of the Y axis.
The next step is to call IDirect3DDevice9: SetTransform () to apply the viewport matrix to the Direct3D device. The first parameter accepted by Idirect3DDevice9: SetTransform () tells Direct3D which type of matrix to set. In this example, use the D3DTS_VIEW flag to specify which type of matrix will be set, the second parameter is the pointer to the currently set viewport matrix.
View Transform (Direct3D9)
After defining the PV matrix, you can prepare to set the projection matrix. Declaring the definition sequence of various matrices again does not affect the program, but Direct3D uses the sequence mentioned above.
Define Projection Matrix
D3DXMATRIX * D3DMatrixPerspectiveFovLH {
D3DXMATRIX & pOut, // return the save operation result
FLOAT fovY, // defines the range of the view.
FLOAT Aspect, // Aspect Ratio
FLOAT zn ,//
FlLOAT zf ,//
};
The projection matrix defines how to convert a 3D view to a 2D view space.
The same Code sets the projection matrix and then sets the Direct3D device for the current matrix.
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH (& matProj, D3DX_PI/4, 1.0f, 1.0f, 100366f );
G_pd3dDevice-> SetTransform (D3DTS_PROJECTION, & matProj );
Step 1 call D3DXMatrixPerspectiveFovLH () to set the projection matrix. The first parameter is the pointer of the D3DXMATRIX struct to save the operation result. The second parameter defines the range of the view and shows how to reduce the distant object. In this example, a typical 90 degree (1/4 PI) is used ), the third parameter defines the aspect ratio (Aspect Ratio) of the screen ). In this example, the common ratios of 1. 4 and 5 are used to define the near and far shear surfaces. This will determine a certain distance from the geometric chart will not be displayed. In the Matrices project, set the near shear surface to 1 far shear surface to 100.
The next step is to call IDirect3DDevice9: SetTransform () to apply the projection matrix to the Direct3D device. The first parameter accepted by IDirect3DDevice9: SetTransfomr () tells Direct3D which matrix will be set, in this example, D3DTS_PROJECTION is used to mark the specified projection matrix, and the second parameter is a pointer to the current matrix.
DirectX9.0 3D development and programming Basics
Jiangxi University of Technology FangSH 2010-4-8