我正试图迁移到Directx数学系,但是新的代码给我带来了一些麻烦。
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 3*转换为_m128
编辑:
sdk.geometric.xmvector3length.aspx https://msdn.microsoft.com/en-us/library/windows/desktop/bb205510%28v=vs.85%29.aspx
似乎返回类型改变为向量,结果是相同的,而不仅仅是一个浮点。
发布于 2015-02-18 17:38:54
XMFLOAT3不会在DirectXMath库中隐式转换为XMVECTOR。你必须使用XMLoadFloat3。上面的Length方法是:
inline float Length() const
{ XMVECTOR v = XMLoadFloat3(this);
return XMVectorGetX( XMVector3Length(v) ); }我建议查看一下DirectXMath在DirectX工具包中的DirectXMath包装器。它通过大量使用C++隐式转换,使这些类型的使用更加宽容,就像您在上面假设的那样。SimpleMath::Vector3类本质上就是您在上面所写的内容。SimpleMath版本的优点是可以转换为本机DirectXMath类型,因此理论上您可以比上面的抽象更有效地使用SIMD。
https://stackoverflow.com/questions/28526362
复制相似问题