为什么在以下代码中调用 D3DXQuaternionToAxisAngle?
Why is D3DXQuaternionToAxisAngle being called in the following code?
我正在尝试将一些代码转换为最初使用 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, 0, 0 };
}
if(d < (1e-6f - 1.0f))
{
Vec3 axis = Vec3(1, 0, 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
。
As a reminder, these are 'D3DXMath' functions which were in the now deprecated D3DX9/D3DX10 utility libraries. The modern solution is DirectXMath. There's a list of D3DXMath equivalents in DirectXMath here.
我正在尝试将一些代码转换为最初使用 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, 0, 0 };
}
if(d < (1e-6f - 1.0f))
{
Vec3 axis = Vec3(1, 0, 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
。
As a reminder, these are 'D3DXMath' functions which were in the now deprecated D3DX9/D3DX10 utility libraries. The modern solution is DirectXMath. There's a list of D3DXMath equivalents in DirectXMath here.