0

我正在尝试将一些代码转换为最初使用 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;
}

链接到 microsofts api 文档

我的结论是否正确,即 if 块是多余的?或者我可能错过了什么?

4

1 回答 1

1

正如您所注意到的,第一个 if 案例中的代码被破坏了。他们可能打算使用D3DXQuaternionRotationAxis具有相同签名的。

提醒一下,这些是现在已弃用的 D3DX9/D3DX10 实用程序库中的“D3DXMath”函数。现代解决方案是DirectXMath此处有 DirectXMath 中的 D3DXMath 等效项列表。

于 2021-05-08T20:07:24.327 回答