如何自己实现D3DXMatrixRotationAix和D3DXMatrixRotationYawPitchRoll??

lyzcom 2003-11-27 11:43:44
如何自己实现这两个函数的功能?要完全和D3DX库的这两个函数实现的功能一样。
因为我数学不太好,弄这两个函数三天了,但是总是有问题。不是有偏差就是完全不对。哪位大哥帮帮忙吧,我实在不行了。

另外,能不能推荐一本专门讲3D数学与物理学的书?我只要有一本这样的参考书就可以马上学上来,毕竟数学基本不是很差。
...全文
194 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
ttmmdd 2003-11-27
  • 打赏
  • 举报
回复
推荐"线性代数"和"计算机图型学"(电子工业出版社的?就是从老外的教材翻译的那个)
我手上只有轴角<->四元式 欧拉角<->四元式 矩阵(行矩阵 3列X4行 )<->四元式的转换的代码.
你可以再参考下网上3DMAX的开原部分,和网上的一些3D引擎.
附代码

void Quat::ConvertFromEulerAngle(const float fYaw,const float fPitch,const float fRoll)
{
float fSinYaw = sinf( fYaw/2.0f );
float fSinPitch = sinf( fPitch/2.0f );
float fSinRoll = sinf( fRoll/2.0f );
float fCosYaw = cosf( fYaw/2.0f );
float fCosPitch = cosf( fPitch/2.0f );
float fCosRoll = cosf( fRoll/2.0f );

x = fSinRoll * fCosPitch * fCosYaw - fCosRoll * fSinPitch * fSinYaw;
y = fCosRoll * fSinPitch * fCosYaw + fSinRoll * fCosPitch * fSinYaw;
z = fCosRoll * fCosPitch * fSinYaw - fSinRoll * fSinPitch * fCosYaw;
w = fCosRoll * fCosPitch * fCosYaw + fSinRoll * fSinPitch * fSinYaw;

}

void Quat::ConvertFromMatrix(const Matrix3 &mat)
{
float tr, su;
int i;



tr=mat.m[0][0]+mat.m[1][1]+mat.m[2][2];
if(tr>0)
{
su = sqrtf(tr + 1.0f);
w = 0.5f * su;
su =0.5f/su;
x = (mat.m[2][1] - mat.m[1][2]) * su;
y = (mat.m[0][2] - mat.m[2][0]) * su;
z = (mat.m[1][0] - mat.m[0][1]) * su;
}
else
{
i =0;
if(mat.m[1][1] > mat.m[0][0]) i = 1;
if(mat.m[2][2] > mat.m[i][i]) i = 2;
switch (i)
{
case 0:
su = sqrtf((mat.m[0][0] - (mat.m[1][1] + mat.m[2][2])) + 1);
x = 0.5f * su;
su =0.5f/su;
y = (mat.m[0][1] + mat.m[1][0]) * su;
z = (mat.m[2][0] + mat.m[0][2]) * su;
w=(mat.m[2][1] - mat.m[1][2]) * su;
break;
case 1:
su = sqrtf((mat.m[1][1] - (mat.m[2][2] + mat.m[0][0])) + 1);
y = 0.5f*su;
su = 0.5f/su;
z = (mat.m[1][2] + mat.m[2][1]) * su;
x = (mat.m[0][1] + mat.m[1][0]) * su;
w = (mat.m[0][2] - mat.m[2][0]) * su;
break;
case 2:
su = sqrtf((mat.m[2][2]-(mat.m[0][0]+mat.m[1][1]))+1);
z=0.5f*su;
su=0.5f/su;
x = (mat.m[2][0]+mat.m[0][2]) * su;
y = (mat.m[1][2]+mat.m[2][1]) * su;
w = (mat.m[1][0]-mat.m[0][1]) * su;
break;
}
}

}

void Quat::ConvertToMatrix ( Matrix3 &tm)
{
float xx = x*x; float yy = y*y; float zz = z*z;
float xy = x*y; float xz = x*z; float yz = y*z;
float wx = w*x; float wy = w*y; float wz = w*z;

tm.m[0][0] = 1 - 2 * ( yy + zz );
tm.m[0][1] = 2 * ( xy - wz );
tm.m[0][2] = 2 * ( xz + wy );

tm.m[1][0] = 2 * ( xy + wz );
tm.m[1][1] = 1 - 2 * ( xx + zz );
tm.m[1][2] = 2 * ( yz - wx );

tm.m[2][0] = 2 * ( xz - wy );
tm.m[2][1] = 2 * ( yz + wx );
tm.m[2][2] = 1 - 2 * ( xx + yy );

tm.m[3][0] = tm.m[3][1] = tm.m[3][2] = 0.0f;

}

void Quat::ConvertToAxisAngle (Point3 &vector ,float & angle )
{
float fTheta = acosf(w) * 2.0f;
vector.x = x / sinf( fTheta/2.0f );
vector.y = y / sinf( fTheta/2.0f );
vector.z = z / sinf( fTheta/2.0f );
angle=fTheta;
}


void Quat::ConvertFromAxisAngle(Point3 &vector , float & angle )
{


this->w=cosf (angle/2.0f);
this->x=sinf(angle/2.0f)*vector.x;
this->y=sinf(angle/2.0f)*vector.y;
this->z=sinf(angle/2.0f)*vector.z;


}


8,325

社区成员

发帖
与我相关
我的任务
社区描述
游戏开发相关内容讨论专区
社区管理员
  • 游戏开发
  • 呆呆敲代码的小Y
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧