• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            天行健 君子當自強而不息

            DirectX 9的一些數(shù)學計算函數(shù):矩陣,坐標變換。


            向量的運算已超了傳統(tǒng)意義上的數(shù)的運算,因為向量并不是一般的數(shù)字。這意味著一切可用符號表示出來的事物,都可以對其進行各種關系運算,而不必關心事物的本身。這種符號運算的觀點,推動了近代數(shù)學對一般的集合元素進行的抽象代數(shù)方面的研究。

            在線性方程組的求解上,變元較少的方程組(如僅有兩個未知數(shù)x和y的方程組)的解很容易表達出來,但是推廣到較為復雜的更多變元的情形,如何推測方程組的解以及如何用確定的式子表達出來,引發(fā)了對行列式的提出和研究。行列式實質上是方陣數(shù)據(jù)的一種運算,由此自然地想到,可對一般行列數(shù)據(jù)定義代數(shù)運算,就像對向量定義運算一樣,這就是矩陣運算的產生歷史。

            由于DirectX三維游戲的開發(fā)僅需要應用4x4矩陣的計算,因此,DirectX 9在d3dx9math.h文件中定義了對應于4x4矩陣的D3DMATRIX結構體和一個提供了4X4矩陣的各種運算處理的D3DXMATRIX類。

            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;

            typedef 
            struct D3DXMATRIX : public D3DMATRIX
            {
            public:
                D3DXMATRIX() {};
                D3DXMATRIX( CONST FLOAT 
            * );
                D3DXMATRIX( CONST D3DMATRIX
            & );
                D3DXMATRIX( CONST D3DXFLOAT16 
            * );
                D3DXMATRIX( FLOAT _11, FLOAT _12, FLOAT _13, FLOAT _14,
                            FLOAT _21, FLOAT _22, FLOAT _23, FLOAT _24,
                            FLOAT _31, FLOAT _32, FLOAT _33, FLOAT _34,
                            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;

            要正確運行以下的示例程序,需要在工程中包含d3dx9.lib,或者在main函數(shù)前加入
            #pragma comment(lib, "d3dx9.lib")

            以指示編譯器鏈接d3dx9.lib。

            矩陣的基本運算:包括加,減,乘。

            函數(shù)原型:
                D3DXMATRIX operator * ( CONST D3DXMATRIX& ) const;
                D3DXMATRIX operator + ( CONST D3DXMATRIX& ) const;
                D3DXMATRIX operator - ( CONST D3DXMATRIX& ) const;
                D3DXMATRIX operator * ( FLOAT ) const;
                D3DXMATRIX operator / ( FLOAT ) const;

            代碼示例:

            #include <stdio.h>
            #include 
            <D3DX9Math.h>

            #pragma warning(disable : 
            4305)

            int main()
            {
                
            // initialize and print matrix A
                D3DXMATRIX A(11.012.013.014.0,
                             
            21.022.023.024.0,
                             
            31.032.033.034.0,
                             
            41.042.043.044.0);

                printf(
            "\n");

                printf(
            "     |%f   %f    %f    %f|\n", A._11,A._12,A._13,A._14);
                printf(
            "     |%f   %f    %f    %f|\n", A._21,A._22,A._23,A._24);
                printf(
            "  A= |%f   %f    %f    %f|\n", A._31,A._32,A._33,A._34);
                printf(
            "     |%f   %f    %f    %f|\n", A._41,A._42,A._43,A._44);

                printf(
            "\n\n\n");

                
            // initialize and print matrix B
                D3DXMATRIX B(-10.0-12.0-13.0-14.0,
                             
            -21.0-21.0-23.0-24.0,
                             
            -31.0-32.0-32.0-34.0,
                             
            -41.0-42.0-43.0-43.0);

                printf(
            "     |%f   %f    %f    %f|\n", B._11,B._12,B._13,B._14);
                printf(
            "     |%f   %f    %f    %f|\n", B._21,B._22,B._23,B._24);
                printf(
            "  B= |%f   %f    %f    %f|\n", B._31,B._32,B._33,B._34);
                printf(
            "     |%f   %f    %f    %f|\n", B._41,B._42,B._43,B._44);

                printf(
            "\n\n\n");

                
            // calculate A+B
                D3DXMATRIX C = A + B;

                
            for(int i=0; i<4; i++)
                {
                    
            if(i != 2)
                        printf(
            "      |%f   %f    %f    %f|\n", C(i,0), C(i,1), C(i,2), C(i,3));
                    
            else 
                        printf(
            "  A+B=|%f   %f    %f    %f|\n", C(i,0), C(i,1), C(i,2), C(i,3));
                }

                printf(
            "\n\n\n");

                
            // calculate A * B
                C = A * B;
                
                
            for(int i=0; i<4; i++)
                {
                    
            if(i != 2)
                        printf(
            "     |%f   %f    %f    %f|\n", C(i,0), C(i,1), C(i,2), C(i,3));
                    
            else 
                        printf(
            "  AB=|%f   %f    %f    %f|\n", C(i,0), C(i,1), C(i,2), C(i,3));
                }
                
                printf(
            "\n");

                
            return 0;
            }

            輸出:



            矩陣求逆:

            // Calculate inverse of matrix.  Inversion my fail, in which case NULL will
            // be returned.  The determinant of pM is also returned it pfDeterminant
            // is non-NULL.
            D3DXMATRIX* WINAPI D3DXMatrixInverse
                ( D3DXMATRIX *pOut, FLOAT *pDeterminant, CONST D3DXMATRIX *pM );

            代碼示例:

            #include <stdio.h>
            #include 
            <D3DX9Math.h>

            #pragma warning(disable : 
            4305)

            int main()
            {
                
            // initialize and print matrix A
                D3DXMATRIX A(1.012.013.014.0,
                             
            0.01.0,  23.024.0,
                             
            0.00.0,  1.0,  34.0,
                             
            0.00.0,  0.0,  1.0);

                printf(
            "       |%-4.6f   %-4.6f    %-4.6f    %-4.6f|\n", A._11, A._12, A._13, A._14);
                printf(
            "       |%04.6f   %04.6f    %04.6f    %04.6f|\n", A._21, A._22, A._23, A._24);
                printf(
            "   A=  |%04.6f   %04.6f    %04.6f    %04.6f|\n", A._31, A._32, A._33, A._34);
                printf(
            "       |%04.6f   %04.6f    %04.6f    %04.6f|\n", A._41, A._42, A._43, A._44);

                printf(
            "\n\n\n");

                
            // calculate inverse matrix for A
                D3DXMATRIX B;

                D3DXMatrixInverse(
            &B, NULL, &A);

                printf(
            "        |%04.6f   %04.6f    %04.6f    %04.6f|\n", B._11, B._12, B._13, B._14);
                printf(
            "        |%04.6f   %04.6f    %04.6f    %04.6f|\n", B._21, B._22, B._23, B._24);
                printf(
            "inv(A)= |%04.6f   %04.6f    %04.6f    %04.6f|\n", B._31, B._32, B._33, B._34);
                printf(
            "        |%04.6f   %04.6f    %04.6f    %04.6f|\n", B._41, B._42, B._43, B._44);

                printf(
            "\n\n\n");

                
            return 0;
            }

            輸出:



            矩陣轉置:

            D3DXMATRIX* WINAPI D3DXMatrixTranspose
                ( D3DXMATRIX *pOut, CONST D3DXMATRIX *pM );

            代碼示例:

            #include <stdio.h>
            #include 
            <D3DX9Math.h>

            #pragma warning(disable : 
            4305)

            int main()
            {
                
            // initialize and print matrix
                D3DXMATRIX A(11.0f12.0f13.0f14.0f,
                            
            21.0f22.0f23.0f24.0f,
                            
            31.0f32.0f33.0f34.0f,
                            
            41.0f42.0f43.0f44.0f);

                printf(
            "          |%f   %f    %f    %f|\n", A._11, A._12, A._13, A._14);
                printf(
            "          |%f   %f    %f    %f|\n", A._21, A._22, A._23, A._24);
                printf(
            "       A= |%f   %f    %f    %f|\n", A._31, A._32, A._33, A._34);
                printf(
            "          |%f   %f    %f    %f|\n", A._41, A._42, A._43, A._44);

                printf(
            "\n\n\n");

                
            // calculate matrix transpose
                D3DXMATRIX C;

                D3DXMatrixTranspose(
            &C, &A);

                printf(
            "              |%f   %f    %f    %f|\n", C._11, C._12, C._13, C._14);
                printf(
            "              |%f   %f    %f    %f|\n", C._21, C._22, C._23, C._24);
                printf(
            " transpose A= |%f   %f    %f    %f|\n", C._31, C._32, C._33, C._34);
                printf(
            "              |%f   %f    %f    %f|\n", C._41, C._42, C._43, C._44);

                printf(
            "\n\n\n");

                
            return 0;
            }

            輸出:



            坐標平移變換:

            1) Builds a matrix using the specified offsets.

            D3DXMATRIX *WINAPI D3DXMatrixTranslation(D3DXMATRIX *pOut, FLOAT x, FLOAT y, FLOAT z);

            2) Transforms a 3-D vector by a given matrix, projecting the result back into w = 1.

            D3DXVECTOR3 *WINAPI D3DXVec3TransformCoord(D3DXVECTOR3 *pOut,
                CONST D3DXVECTOR3 *pV,
                CONST D3DXMATRIX *pM
            );

            This function transforms the vector, pV (x, y, z, 1), by the matrix, pM, projecting the result back into w=1.

            3) Transforms the 3-D vector normal by the given matrix.

            D3DXVECTOR3 *WINAPI D3DXVec3TransformNormal(D3DXVECTOR3 *pOut,
                CONST D3DXVECTOR3 *pV,
                CONST D3DXMATRIX *pM
            );

            This function transforms the vector normal (x, y, z, 0) of the vector, pV, by the matrix, pM.

            If you transform a normal by a non-affine matrix, the matrix you pass to this function should be the transpose of the inverse of the matrix you would use to transform a coordinate.

            D3DXVec3TransformNormal這個函數(shù)似乎執(zhí)行與 D3DXVec3TransformCoord相反的運算,沒搞懂。

            代碼示例:

            #include <stdio.h>
            #include 
            <D3DX9Math.h>

            #pragma warning(disable : 
            4305)

            int main()
            {
                D3DXVECTOR3 v(
            1.02.03.0);
                D3DXMATRIX M;

                D3DXMatrixTranslation(
            &M, v.x, v.y, v.z);

                printf(
            "             |%f   %f    %f    %f|\n", M._11, M._12, M._13, M._14);
                printf(
            "             |%f   %f    %f    %f|\n", M._21, M._22, M._23, M._24);
                printf(
            "Translate M= |%f   %f    %f    %f|\n", M._31, M._32, M._33, M._34);
                printf(
            "             |%f   %f    %f    %f|\n", M._41, M._42, M._43, M._44);

                printf(
            "\n\n");

                D3DXVECTOR3 P(
            20.03.08.0);
                D3DXVECTOR3 new_P;

                D3DXVec3TransformCoord(
            &new_P, &P, &M);

                printf(
            "p coordinate (%f, %f, %f) in new coordinate reference\n\n", new_P.x, new_P.y, new_P.z);

                D3DXVec3TransformNormal(
            &new_P, &P, &M);

                printf(
            "p transform normal (%f, %f, %f)\n\n", new_P.x, new_P.y, new_P.z);

                
            return 0;
            }

            輸出:



            坐標的放大縮小變換:

            Builds a matrix that scales along the x-axis, the y-axis, and the z-axis.

            D3DXMATRIX * D3DXMatrixScaling(
            D3DXMATRIX *pOut,
            FLOAT sx,
            FLOAT sy,
            FLOAT sz
            );
            pOut
            [in, out] Pointer to the D3DXMATRIX structure that is the result of the operation.
            sx
            [in] Scaling factor that is applied along the x-axis.
            sy
            [in] Scaling factor that is applied along the y-axis.
            sz
            [in] Scaling factor that is applied along the z-axis.

            代碼示例:

            #include <stdio.h>
            #include 
            <D3DX9Math.h>

            #pragma warning(disable : 
            4305)

            int main()
            {
                D3DXMATRIX M;
                D3DXMatrixScaling(
            &M, 2.02.02.0);

                printf(
            "                   |%f   %f    %f    %f|\n", M._11, M._12, M._13, M._14);
                printf(
            "                   |%f   %f    %f    %f|\n", M._21, M._22, M._23, M._24);
                printf(
            "Scaling Matrix: M= |%f   %f    %f    %f|\n", M._31, M._32, M._33, M._34);
                printf(
            "                   |%f   %f    %f    %f|\n", M._41, M._42, M._43 ,M._44);

                printf(
            "\n\n\n");

                D3DXVECTOR3  P(
            3.08.033.0);
                D3DXVECTOR3 new_P;

                D3DXVec3TransformCoord(
            &new_P, &P, &M);
                printf(
            "After transform coordinate: (%f, %f, %f)\n\n", new_P.x, new_P.y, new_P.z);

                
            return 0;
            }

            輸出:



            旋轉變換:

            對于旋轉變換的矩陣計算,DirectX提供了 D3DXMatrixRotationX,D3DXMatrixRotationY, D3DXMatrixRotationZ和D3DXMatrixRotationAxis等函數(shù)來計算沿x,y,z軸以及任意軸線的旋轉變換矩陣,這4個函數(shù)的旋轉角都采用弧度作為計量單位.

            // Build a matrix which rotates around the X axis
            D3DXMATRIX* WINAPI D3DXMatrixRotationX
                ( D3DXMATRIX *pOut, FLOAT Angle );

            // Build a matrix which rotates around the Y axis
            D3DXMATRIX* WINAPI D3DXMatrixRotationY
                ( D3DXMATRIX *pOut, FLOAT Angle );

            // Build a matrix which rotates around the Z axis
            D3DXMATRIX* WINAPI D3DXMatrixRotationZ
                ( D3DXMATRIX *pOut, FLOAT Angle );

            // Build a matrix which rotates around an arbitrary axis
            D3DXMATRIX* WINAPI D3DXMatrixRotationAxis
                ( D3DXMATRIX *pOut, CONST D3DXVECTOR3 *pV, FLOAT Angle );

            代碼示例:

            #include <stdio.h>
            #include 
            <D3DX9Math.h>

            #pragma warning(disable : 
            4305)

            int main()
            {
                D3DXMATRIX M;
                D3DXVECTOR3 vec_rotate(
            5.05.05.0);
                
            float angle = D3DX_PI;

                D3DXMatrixRotationAxis(
            &M, &vec_rotate, angle);

                printf(
            "                    |%f   %f    %f    %f|\n", M._11, M._12, M._13, M._14);
                printf(
            "                    |%f   %f    %f    %f|\n", M._21, M._22, M._23, M._24);
                printf(
            "Rotation Matrix M = |%f   %f    %f    %f|\n", M._31, M._32, M._33, M._34);
                printf(
            "                    |%f   %f    %f    %f|\n", M._41, M._42, M._43, M._44);

                printf(
            "\n\n\n");

                D3DXVECTOR3 P(
            2.03.06.0);
                D3DXVec3TransformCoord(
            &P, &P, &M);

                printf(
            "After rotation: P(%f, %f, %f)\n\n", P.x, P.y, P.z);

                
            return 0;
            }

            輸出:



            還有一個函數(shù) D3DXMatrixRotationYawPitchRoll,可以直接計算沿x,y,z軸旋轉的變換矩陣。

            Builds a matrix with a specified yaw, pitch, and roll.

            D3DXMATRIX * D3DXMatrixRotationYawPitchRoll(
            D3DXMATRIX *pOut,
            FLOAT Yaw,
            FLOAT Pitch,
            FLOAT Roll
            );

            Parameters

            pOut
            [in, out] Pointer to the D3DXMATRIX structure that is the result of the operation.
            Yaw
            [in] Yaw around the y-axis, in radians.
            Pitch
            [in] Pitch around the x-axis, in radians.
            Roll
            [in] Roll around the z-axis, in radians.

            Return Values

            Pointer to a D3DXMATRIX structure with the specified yaw, pitch, and roll.

            Remarks

            The return value for this function is the same value returned in the pOut parameter. In this way, the D3DXMatrixRotationYawPitchRoll function can be used as a parameter for another function.

            The order of transformations is roll first, then pitch, then yaw. Relative to the object's local coordinate axis, this is equivalent to rotation around the z-axis, followed by rotation around the x-axis, followed by rotation around the y-axis.
             

            Figure 1.  Roll, pitch, yaw diagram




            代碼示例:

            #include <stdio.h>
            #include 
            <D3DX9Math.h>

            #pragma comment(lib, 
            "d3dx9.lib")

            #pragma warning(disable : 
            4305)

            int main()
            {
                D3DXMATRIX M;
                D3DXVECTOR3 P(
            2.03.06.0);

                printf(
            "Before rotation: P(%f, %f, %f)\n\n", P.x, P.y, P.z);

                
            // Rotate around y-axis with 0 degree, around x-axis with PI degree, around z-axis with PI degree.
                D3DXMatrixRotationYawPitchRoll(&M, 0, D3DX_PI, D3DX_PI);

                printf(
            "                    |%f   %f    %f    %f|\n", M._11, M._12, M._13, M._14);
                printf(
            "                    |%f   %f    %f    %f|\n", M._21, M._22, M._23, M._24);
                printf(
            "Rotation Matrix M = |%f   %f    %f    %f|\n", M._31, M._32, M._33, M._34);
                printf(
            "                    |%f   %f    %f    %f|\n", M._41, M._42, M._43, M._44);

                printf(
            "\n\n\n");
                
                D3DXVec3TransformCoord(
            &P, &P, &M);

                printf(
            "After rotation: P(%f, %f, %f)\n\n", P.x, P.y, P.z);

                
            return 0;
            }

            運行截圖:



            posted on 2007-04-27 20:04 lovedday 閱讀(2369) 評論(0)  編輯 收藏 引用 所屬分類: ■ DirectX 9 Program

            公告

            導航

            統(tǒng)計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關鏈接

            搜索

            最新評論

            国内精品久久久久久不卡影院| 色综合久久久久综合体桃花网 | 狠狠人妻久久久久久综合| 亚洲欧美一级久久精品| 无码人妻少妇久久中文字幕蜜桃 | 欧美综合天天夜夜久久| 亚洲欧美精品一区久久中文字幕| 久久久久久人妻无码| 久久精品国产99久久丝袜| 国产精品无码久久久久久| 久久久久99精品成人片三人毛片| 久久这里只有精品18| 狠狠色综合网站久久久久久久| 久久久久久亚洲AV无码专区| 欧美日韩成人精品久久久免费看| 丰满少妇人妻久久久久久| 亚洲精品成人久久久| 久久人妻少妇嫩草AV无码蜜桃| 99精品国产在热久久无毒不卡| 久久人人爽人人爽人人片av麻烦| 久久精品国产亚洲5555| 久久久青草青青亚洲国产免观| 亚洲狠狠婷婷综合久久蜜芽| 少妇久久久久久被弄到高潮| 精品视频久久久久| 激情综合色综合久久综合| 1000部精品久久久久久久久| 国产成人精品白浆久久69| 日日噜噜夜夜狠狠久久丁香五月| 久久精品国产亚洲av麻豆图片| 区亚洲欧美一级久久精品亚洲精品成人网久久久久 | 人妻系列无码专区久久五月天| 国产成人无码精品久久久免费| 中文字幕亚洲综合久久2| 久久免费视频观看| 99久久精品九九亚洲精品| 国产精品综合久久第一页| 久久久国产精品| 久久亚洲精品国产精品婷婷| 欧美精品乱码99久久蜜桃| 久久久久免费精品国产|