0

我正在尝试迁移到 directxmath,但是新代码给我带来了一些麻烦..

 class Vec3 : public XMFLOAT3
{
public:
    inline float Length() { return XMVector3Length(this); }
    inline Vec3 *Normalize() { return static_cast<Vec3 *>(  XMVector3Normalize(this, this)); }
    inline float Dot(const Vec3 &b) { return  XMVector3Dot(this, &b); }
    inline Vec3 Cross(const Vec3 &b) const;

    Vec3(XMFLOAT3 &v3) { x = v3.x; y = v3.y; z = v3.z; }
    Vec3() : XMFLOAT3() { XMVectorZero(); }
    Vec3(const float _x, const float _y, const float _z) { x=_x; y=_y; z=_z; }
     Vec3(const double _x, const double _y, const double _z) { x = (float)_x; y = (float)_y; z = (float)_z; }
    inline Vec3(const class Vec4 &v4);
 };

旧代码如下所示:

 class Vec3 : public D3DXVECTOR3 
     {
    public:
        inline float Length() { return D3DXVec3Length(this); }
        inline Vec3 *Normalize() { return static_cast<Vec3 *(D3DXVec3Normalize(this, this)); }
        inline float Dot(const Vec3 &b) { return D3DXVec3Dot(this, &b); }
        inline Vec3 Cross(const Vec3 &b) const;

        Vec3(D3DXVECTOR3 &v3) { x = v3.x; y = v3.y; z = v3.z; }
        Vec3() : D3DXVECTOR3() { x = 0; y = 0; z = 0; }
        Vec3(const float _x, const float _y, const float _z) { x=_x; y=_y; z=_z; }
         Vec3(const double _x, const double _y, const double _z) { x = (float)_x; y = (float)_y; z = (float)_z; }
        inline Vec3(const class Vec4 &v4);
     };

所以,我现在遇到的问题是 XMVector3Length 无法从 Vec3* 转换为 _m128

编辑:

https://msdn.microsoft.com/en-us/library/windows/desktop/microsoft.directx_sdk.geometric.xmvector3length.aspx

https://msdn.microsoft.com/en-us/library/windows/desktop/bb205510%28v=vs.85%29.aspx

似乎返回类型更改为向量,结果相同,而不仅仅是一个浮点数。

4

1 回答 1

0

XMFLOAT3不会XMVECTORDirectXMath库中隐式转换为。你必须使用XMLoadFloat3. 上面的Length方法是:

inline float Length() const
    { XMVECTOR v = XMLoadFloat3(this);
      return XMVectorGetX( XMVector3Length(v) ); }

我建议看一下DirectX Tool Kit中 DirectXMath 的SimpleMath包装器。通过大量使用 C++ 隐式转换,就像您在上面假设的那样,它使使用这些类型更加宽容。该课程本质上就是您在上面尝试编写的内容。SimpleMath 版本具有转换为本机 DirectXMath 类型的优势,因此理论上您可以比使用上述抽象更有效地使用 SIMD。SimpleMath::Vector3

于 2015-02-18T17:38:54.547 回答