天行健 君子当自强而不息

顶点坐标变换(3)

世界变换

世界变换就是将物体顶点坐标从模型空间转换到世界空间。在模型空间里,顶点位置坐标依据模型的本地坐标系的原点而定,在世界空间里,所有模型的顶点共用一个原点,即世界坐标系原点。事实上,世界变换就是将一个模型从本地空间重新定位到世界空间内。从模型空间到世界空间的转换实际上就是对模型进行平移、旋转、缩放以及它们的任意组合变换。

使用三维模型制作软件,例如3dmax,制作三维模型时,首先需要为模型设定一个坐标系,模型上的顶点坐标就是设定的模型自身坐标系下的坐标,这个坐标系也就是上面提到的本地坐标系或模型空间。

 

1、世界变换矩阵

在处理三维图像的应用程序中,可使用世界变换完成一个物体(确切的说是一个坐标或一系列坐标)的平移、旋转和缩放。当然也可以完成这三种变换的任意组合。具体的方法就是通过下式:

将任意一点P(x, y, z)转换到p'(x', y', z'),上式也可以表示为以下形式:

p'(x', y', z') = P(x, y, z) . Mworld

Mworld就是世界变换矩阵。也就是它实现了物体的平移、旋转、缩放和它们的复合变换。在定义好世界变换矩阵后,调用函数IDirect3DDevice9::SetTransform()并指定第一个参数为D3DTS_WORLD,第二个参数为相应的世界变换矩阵即可。

 

2、平移

可以通过下式(也就是下面的平移变换矩阵):

将点(x, y, z)沿x、y和z轴分别移动Tx、Ty、Tz,到另一点(x',y',z')。很显然,只要得到了这个平移矩阵,平移工作就可以完成。

为方便起见,D3DX扩展函数库d3dx9.lib提供了函数D3DXMatrixTranslation(),用它可以很方便地生成一个平移世界矩阵。该函数的声明如下:

Builds a matrix using the specified offsets.

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

Parameters

pOut
[in, out] Pointer to the D3DXMATRIX structure that is the result of the operation.
x
[in] X-coordinate offset.
y
[in] Y-coordinate offset.
z
[in] Z-coordinate offset.

Return Values

Pointer to a D3DXMATRIX structure that contains a translated transformation matrix.

Remarks

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

 

3、旋转

与平移类似,使用下面的四阶矩阵可以将点(x, y, z)绕x轴旋转θ角,到新点(x', y', z'):

绕y轴旋转θ角时的矩阵为:

绕z轴旋转θ角时的矩阵为:

θ指旋转角度,单位是弧度,具体是指沿着旋转轴的指向(即正方向)向坐标原点看去顺指针旋转过的角度。

同样可以使用D3DX扩展函数库d3dx9.lib提供的函数D3DXMatrixRotationX()、D3DXMatrixRotationY()和D3DXMatrixRotationZ()方便地创建旋转矩阵,这三个函数的声明如下,因声明类似,只列出D3DXMatrixRotationX()的使用说明:

Builds a matrix that rotates around the x-axis.

D3DXMATRIX * D3DXMatrixRotationX(
D3DXMATRIX * pOut,
FLOAT Angle
);

Parameters

pOut
[in, out] Pointer to the D3DXMATRIX structure that is the result of the operation.
Angle
[in] Angle of rotation in radians. Angles are measured clockwise when looking along the rotation axis toward the origin.

Return Values

Pointer to a D3DXMATRIX structure rotated around the x-axis.

Remarks

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

 

4、缩放

使用下面的四阶矩阵可以将点(x, y, z)在x、y、z轴上各缩放Sx、Sy、Sz,到另一点(x', y', z')。

同样,可以使用Direct3D扩展实用库中的函数D3DXMatrixScaling()来生成缩放矩阵,该函数的声明如下:

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
);

Parameters

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.

Return Values

Pointer to the scaling transformation D3DXMATRIX.

Remarks

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

 

5、矩阵连接与复合变换

在大多数情况下,Direct3D中的物体需要进行的世界变换不止一个,而往往是多个世界变换的组合,这时可以使用矩阵连接来实现这种复合变换。因为矩阵的一个优点是通过矩阵的相乘,将两个或更多矩阵的作用合并在一起实现。为了先后实现一个模型的旋转和移动,不需要使用两个矩阵,可以将旋转矩阵和平移矩阵相乘得到一个复合矩阵以实现所有功能。这个过程叫做矩阵连接(matrix concatention),可以用下面的公式表示:

C = M1 * M2 * ... * Mn-1 * Mn

在这个公式里,C是实现复合变换的复合矩阵,从M1到Mn是只能实现某一种世界变换的单独矩阵(individual matrices)。大多数情况下是两到三个矩阵连接,但这个数量没有限制。

使用函数D3DXMatrixMultiply() 可完成矩阵的乘法,该函数的说明如下:

Determines the product of two matrices.

D3DXMATRIX * D3DXMatrixMultiply(
D3DXMATRIX * pOut,
CONST D3DXMATRIX * pM1,
CONST D3DXMATRIX * pM2
);

Parameters

pOut
[in, out] Pointer to the D3DXMATRIX structure that is the result of the operation.
pM1
[in] Pointer to a source D3DXMATRIX structure.
pM2
[in] Pointer to a source D3DXMATRIX structure.

Return Values

Pointer to a D3DXMATRIX structure that is the product of two matrices.

Remarks

The result represents the transformation M1 followed by the transformation M2 (Out = M1 * M2).

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

矩阵pOut表示最终的复合变换,也就是先进行矩阵pM1表示的变换,然后又进行矩阵pM2表示的变换。

在矩阵的乘法中,顺序是很关键的。无论要创建什么样的世界变换矩阵,记住从左到右的原则才能确保实现想要的效果,也就是说,一个复合矩阵的视觉效果是按从左到右的顺序各单独矩阵视觉效果的组合。假设一个物体先绕y轴旋转,然后把它移动到场景内的另一个位置。为实现这个效果,首先创建一个旋转矩阵Ry,然后乘以一个平移矩阵Tw

W = Ry * Tw

在这个公式里,Ry表示绕y轴的旋转矩阵,Tw实现世界坐标系内的一次平移。矩阵的乘法不满足交换律。如果将这两个矩阵以相反的顺序相乘,效果是先平移,然后旋转。


posted on 2008-05-02 11:13 lovedday 阅读(3491) 评论(0)  编辑 收藏 引用


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


公告

导航

统计

常用链接

随笔分类(178)

3D游戏编程相关链接

搜索

最新评论