本系列文章由 net_assassin 整理编写,转载请注明出处。
http://blog.csdn.net/net_assassin/article/category/1100363
作者:net_assassin 邮箱: net_assassin@hotmail.com 期待着与志同道合的朋友们相互交流
这一章我们学习使用Direct3D矩阵及一个极为方便D3DX助手函数来旋转、缩放及平移精灵。
2D变换
ID3DXSprite可以处理变换矩阵是它的迷人之处。
介绍完成这些功能的变换函数:
D3DXMATRIX *D3DXMatrixTransformation2D(
D3DXMATRIX *pOut,
CONST D3DXVECTOR2 *pScalingCenter,
FLOAT pScalingRotation,
CONST D3DXVECTOR2 *pScaling,
CONST D3DXVECTOR2 *pRotationCenter,
FLOAT Rotation,
CONST D3DXVECTOR2 *pTranslation
);
这个函数生成一个实际上通过引用来传递的矩阵(第一个参数),然后填满矩阵值返回给用户。
在得到这个矩阵之后,我们就可以告诉精灵对象(ID3DXSprite)依据它来作为当前的变换:
spriteobj->SetTransform(&mat); //假设得到的矩阵是mat。
Rotate_Scale_Demo程序(绘制简单精灵):
void Sprite_Transform_Draw(LPDIRECT3DTEXTURE9 image, int x, int y, int width, int height,
int frame, int columns, float rotation, float scaling, D3DCOLOR color)
{
//create a scale vector
D3DXVECTOR2 scale( scaling, scaling );
//create a translate vector
D3DXVECTOR2 trans( x, y );
//set center by dividing width and height by two
D3DXVECTOR2 center( (float)( width * scaling )/2, (float)( height * scaling )/2);
//create 2D transformation matrix
D3DXMATRIX mat;
D3DXMatrixTransformation2D( &mat, NULL, 0, &scale, ¢er, rotation, &trans );
//tell sprite object to use the transform
spriteobj->SetTransform( &mat );
//calculate frame location in source image
int fx = (frame % columns) * width;
int fy = (frame / columns) * height;
RECT srcRect = {fx, fy, fx + width, fy + height};
//draw the sprite frame
spriteobj->Draw( image, &srcRect, NULL, NULL, color );
}
Rotate_Animate_Demo(绘制动画精灵,使用Sprite_Animate()):