• <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游戲編程相關鏈接

            搜索

            最新評論

            久久精品aⅴ无码中文字字幕不卡| 久久精品欧美日韩精品| 精品免费tv久久久久久久| 亚洲国产成人久久综合一| 色偷偷91久久综合噜噜噜噜| 欧美亚洲色综久久精品国产| 国产精品热久久毛片| 久久综合狠狠综合久久| 日本免费久久久久久久网站| 久久影视综合亚洲| 99久久免费国产精品| 亚洲日韩欧美一区久久久久我| 国产精品久久久久久久久久免费| 无码人妻久久一区二区三区蜜桃| 久久精品毛片免费观看| 一本久久免费视频| 91麻精品国产91久久久久| 久久香综合精品久久伊人| 一本久道久久综合狠狠躁AV| 久久国产精品二国产精品| 国产精品无码久久四虎| 日韩精品久久无码人妻中文字幕| 久久www免费人成看国产片| 国产精品久久午夜夜伦鲁鲁| 丰满少妇高潮惨叫久久久| 一个色综合久久| 久久午夜夜伦鲁鲁片免费无码影视| 久久露脸国产精品| av无码久久久久久不卡网站| 狠狠色婷婷久久综合频道日韩| 久久99精品国产麻豆宅宅| 欧美激情精品久久久久久| 久久久人妻精品无码一区| 国产精品日韩欧美久久综合| 18岁日韩内射颜射午夜久久成人| 国内精品久久久久影院一蜜桃| 亚洲国产精品无码久久久秋霞2| 韩国免费A级毛片久久| 国内精品人妻无码久久久影院| 丰满少妇高潮惨叫久久久| 1000部精品久久久久久久久|