问题描述
我正在尝试将一些代码转换为最初使用 direct3d 的 glm/opengl,并且根据我在 microsoft 网站上的文档中找到的内容,遇到了一个没有意义的块。有问题的块在下面的评论中有详细说明:
Gx::Quaternion Gx::Quaternion::rotationBetween(const Gx::Vec3 &a,const Gx::Vec3 &b)
{
Quaternion q;
Vec3 v0 = a.normalized();
Vec3 v1 = b.normalized();
float d = v0.dot(v1);
if(d >= 1.0f)
{
return Quaternion{ 0,0 };
}
if(d < (1e-6f - 1.0f))
{
Vec3 axis = Vec3(1,0).cross(a);
if(axis.dot(axis) == 0)
{
axis = Vec3(0,1,0).cross(a);
}
axis = axis.normalized();
float ang = static_cast<float>(M_PI);
D3DXQuaternionToAxisAngle(&q,&axis,&ang);
// This block does not appear to be doing anything as
// according to microsofts documentation on D3DXQuaternionToAxisAngle,// the function "Computes a quaternion's axis and angle of rotation" and
// does not modify the quaternion value passed as it's passed as const.
// Therefore I am confused as to why this block exists as it does not
// affect the returned quaternion,and the variables axis and ang are
// scoped to this block and not taken into account anywhere else in this
// function.
}
else
{
float s = std::sqrt((1 + d) * 2);
float invs = 1 / s;
Vec3 c = v0.cross(v1);
q.x = c.x * invs;
q.y = c.y * invs;
q.z = c.z * invs;
q.w = s * 0.5f;
D3DXQuaternionnormalize(&q,&q);
}
return q;
}
Link to microsofts api documentation
我的结论是这个 if 块是多余的吗?还是我可能遗漏了什么?
解决方法
如您所见,第一个 if 案例中的代码已损坏。他们可能打算使用具有相同签名的 D3DXQuaternionRotationAxis
。
提醒一下,这些是现在已弃用的 D3DX9/D3DX10 实用程序库中的“D3DXMath”函数。现代解决方案是DirectXMath。 DirectXMath here 中有 D3DXMath 等效项的列表。