天行健 君子当自强而不息

顶点坐标变换(2)

矩阵类型及其操作

在Direct3D中,顶点坐标变换通常是借助于矩阵实现的,因此下面首先介绍在Direct3D中提供的各种矩阵类型和相关的矩阵运算函数。

 

1、D3DMATRIX矩阵类型

D3DMATRIX是Direct3D中最简单的矩阵类型,其定义如下:

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;

显然,D3DMATIX中存放的是一个4x4的二维浮点数组,可以通过_ij的格式访问该数组中的每个元素,i表示该元素的行数,j表示该元素的列数。例如,_34表示第三行、第四列的元素。

 

2、D3DXMATRIX矩阵类型

该类型矩阵定义如下:

#ifdef __cplusplus
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;
#else //!__cplusplus
typedef struct _D3DMATRIX D3DXMATRIX, *LPD3DXMATRIX;
#endif //!__cplusplus

 

3、D3DXMATRIXA16矩阵类型

D3DXMATRIXA16称为16字节对齐矩阵(16-byte aligned matrix),它是从矩阵D3DXMATRIX中继承而来的,其定义如下:

typedef D3DX_ALIGN16 _D3DXMATRIXA16 D3DXMATRIXA16, *LPD3DXMATRIXA16;

//---------------------------------------------------------------------------
// Aligned Matrices
//
// This class helps keep matrices 16-byte aligned as preferred by P4 cpus.
// It aligns matrices on the stack and on the heap or in global scope.
// It does this using __declspec(align(16)) which works on VC7 and on VC 6
// with the processor pack. Unfortunately there is no way to detect the
// latter so this is turned on only on VC7. On other compilers this is the
// the same as D3DXMATRIX.
//
// Using this class on a compiler that does not actually do the alignment
// can be dangerous since it will not expose bugs that ignore alignment.
// E.g if an object of this class in inside a struct or class, and some code
// memcopys data in it assuming tight packing. This could break on a compiler
// that eventually start aligning the matrix.
//---------------------------------------------------------------------------
#ifdef __cplusplus
typedef struct _D3DXMATRIXA16 : public D3DXMATRIX
{
_D3DXMATRIXA16() {}
_D3DXMATRIXA16( CONST FLOAT * );
_D3DXMATRIXA16( CONST D3DMATRIX& );
_D3DXMATRIXA16( CONST D3DXFLOAT16 * );
_D3DXMATRIXA16( 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 );
    // new operators
void* operator new ( size_t );
void* operator new[] ( size_t );
    // delete operators
void operator delete ( void* ); // These are NOT virtual; Do not
void operator delete[] ( void* ); // cast to D3DXMATRIX and delete.
    // assignment operators
_D3DXMATRIXA16& operator = ( CONST D3DXMATRIX& );
} _D3DXMATRIXA16;
#else //!__cplusplus
typedef D3DXMATRIX _D3DXMATRIXA16;
#endif //!__cplusplus

当使用了Intel Pentium 4运行一个D3DX数学函数时,16字节对齐矩阵D3DXMATRIXA16为完成相应操作进行了优化。当使用VC++.net或使用安装了processor pack的VC6++时,将开启字节对齐功能。但不幸的是,编译器无法探测到是否安装了processor pack,所以字节对齐仅仅只对VC++.net默认开启。对于其他编译器,16字节对齐矩阵D3DXMATRIXA16将被当作D3DXMATRIX进行操作。

经过扩展后的结构体D3DXMATRIX和D3DXMATRIXA16对许多运算符进行了重载,所以可以直接进行转换运算、赋值运算以及多种一元、二元运算,大大方便了矩阵类型变量的运算。

 

4、常见的矩阵运算函数

因为矩阵的运算相对比较复杂,所以Direct3D提供了一组矩阵运算函数,例如,通过函数D3DXMatrixTranslation()构造一个平移矩阵;通过函数D3DXMatrixRotationX()、D3DXMatrixRotationY()和D3DXMatrixRotationZ()构造绕x、y和z轴转动一定角度的矩阵;通过函数D3DXMatrixScaling()构造一个缩放矩阵;通过函数D3DXMatrxiIdentity()将一个矩阵单位化;通过函数D3DXMatrixMultiply()计算两个矩阵的积;通过函数D3DXMatrixInverse()求原矩阵的逆矩阵;通过函数D3DXMatrixTranspose()计算原矩阵的转置矩阵。


posted on 2008-05-02 10:00 lovedday 阅读(1192) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

导航

统计

常用链接

随笔分类(178)

3D游戏编程相关链接

搜索

最新评论