3D games from beginner to proficient-7

Source: Internet
Author: User
1. World Coordinate transformation matrix After you set the vertex buffer, you need to set the world coordinate transformation matrix for the object. It can be known from analytic geometry that both two-dimensional and three-dimensional images can be transformed by homogeneous coordinates. Currently in three-dimensional transformation, the matrix used is a 4x4 matrix to achieve transformation processing. Define the D3dxmatrix type in D3D, use it to express a matrix, and define a series of functions that can implement the operation of the Matrix. It is defined as follows:   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; The above code defines four elements of the 4x4 matrix, all using floating-point arithmetic. Then we add some action methods, as follows: Typedef struct D3dxmatrix:public d3dmatrix {public:     D3dxmatrix () {};     D3dxmatrix (C Onst FLOAT *);     D3dxmatrix (CONST d3dmatrix&);     D3dxmatrix (CONST d3dxfloat16 *);     D3dxmatrix (float _11, float _12, float _13, float _14,        & nbsp;        float _21, float _22, float _23, float _24,     & nbsp;           float _31, float _32, float _33, float _34,  & nbsp;              float _41, float _42, float _43, FLOAT _44);        //Access grants     float& operator (UINT Row, uint Col);     float operator () (UINT Row, uint Col) const;      //Casting operators     operator float* ();     operator Const float* () const;      //assignment operators     d3dxmatrix& operator *= (CONST d3dxmatrix&) ;     d3dxmatrix& operator + = (CONST d3dxmatrix&);     d3dxmatrix& Operator-= (CONST d3dxmatrix&);     d3dxmatrix& operator *= (FLOAT);     d3dxmatrix& operator/= (FLOAT);      //unary operators     D3dxmatrix operator + () const;     D3dxmatrix Operator-() const;      //binary operators     D3DXMATRIX operator * (const d3dxmatrix&) const;     D3DXMATRIX operator + (const d3dxmatrix&) const;     D3DXMATRIX operator-(const d3dxmatrix&) const;     d3dxmatrix operator * (FLOAT) const;     d3dxmatrix operator/(FLOAT) const;       FRiend d3dxmatrix operator * (FLOAT, CONST d3dxmatrix&);       BOOL operator = = (const d3dxmatrix&) const;     BOOL Operator! = (const d3dxmatrix&) const;  } d3dxmatrix, *lpd3dxmatrix;   The above method, is the operation of the Matrix, the use of more convenient. With the definition of the matrix and the operation of the Matrix, it is easy to construct a matrix from the model coordinates to the world coordinate transformation. Since I do not need to transform it, directly using a unit matrix, it is possible. By the unit matrix, any data multiplied by the unit matrix is unchanged. The code is as follows:  //Set the world coordinate matrix. D3dxmatrix matidentity; D3DXMatrixIdentity (&matidentity); M_pd3ddevice->settransform (d3dts_world, &matidentity);   This code first defines a matrix, then calls the function D3DXMatrixIdentity initializes the matrix as the unit matrix, and finally the function SetTransform is set to the World coordinate transformation matrix. The triangles shown in this way are displayed in the world coordinate system in the model coordinate system, that is, at the origin of the world coordinate system. In the game, all 3D models are done outside, so all are model coordinate systems, that is, the local coordinate system, to display them in the world coordinate system, generally need to pass the transformation. Because all model coordinate systems are the same, they are the origin of the origin in the world coordinate system. If it is not shown by the transformation, all objects overlap at the origin, so that they are not clearly displayed. For example, to show a football field, two goals are shown separately. A goal at the origin, a goal at a distance from the origin of the display, but also need to pass the appropriate rotation transformation to display. Since two goals are made up of objects of the same local coordinate system, they must be displayed by the World coordinate transformation matrix.       This ebookMm3d Engine source programExample Source programShared theYuan set of contacts: Maomei Contact: qq:9073204 email:caimouse1976 at Sina.com

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.