我正在尝试将一些代码转换为最初使用 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;
}
我的结论是否正确,即 if 块是多余的?或者我可能错过了什么?