我为我所有的 3d 对象编写了一个通用矩阵类,但翻译似乎真的很糟糕。
这是我的课:
class GenericMatrix
{
public:
GenericMatrix();
virtual void Identity();
virtual void Scale( float x, float y, float z );
virtual void Scale( const DirectX::XMFLOAT3& s );
virtual DirectX::XMFLOAT3 Scaling( void );
virtual void Translate( float x, float y, float z );
virtual void Translate( const DirectX::XMFLOAT3& t );
virtual DirectX::XMFLOAT3 Translations( void );
virtual void Rotate( float x, float y, float z );
virtual void Rotate( const DirectX::XMFLOAT3& r );
virtual DirectX::XMVECTOR Rotations( void );
virtual DirectX::XMFLOAT3 RotationsPYR( void );
virtual DirectX::XMMATRIX Matrix( bool process = true );
virtual operator DirectX::XMMATRIX()
{
return this->Matrix();
}
virtual void UpdateMatrix( void );
DirectX::XMMATRIX matrix;
DirectX::XMFLOAT3 scale;
DirectX::XMFLOAT3 translation;
DirectX::XMVECTOR rotation;
bool matrixNeedsUpdate;
protected:
float yaw, pitch, roll;
};
来源:
#include "pch.h"
#include "GenericMatrix.h"
GenericMatrix::GenericMatrix()
: matrixNeedsUpdate( true )
{
this->Identity();
this->pitch = 90.0f;
this->yaw = 0.0f;
this->roll = 0.0f;
}
void GenericMatrix::Identity()
{
this->scale = DirectX::XMFLOAT3( 1.0f, 1.0f, 1.0f );
this->translation = DirectX::XMFLOAT3( 0.0f, 0.0f, 0.0f );
this->rotation = DirectX::XMQuaternionIdentity();
this->pitch = 90.0f;
this->yaw = 0.0f;
this->roll = 0.0f;
matrixNeedsUpdate = true;
}
void GenericMatrix::Scale( float x, float y, float z )
{
this->scale.x += x;
this->scale.y += y;
this->scale.z += z;
this->matrixNeedsUpdate = true;
}
void GenericMatrix::Scale( const DirectX::XMFLOAT3& s )
{ Scale( s.x, s.y, s.z ); }
DirectX::XMFLOAT3 GenericMatrix::Scaling( void )
{ return this->scale; }
void GenericMatrix::Translate( float x, float y, float z )
{
this->translation.x += x;
this->translation.y += y;
this->translation.z += z;
this->matrixNeedsUpdate = true;
}
void GenericMatrix::Translate( const DirectX::XMFLOAT3& t )
{ Translate( t.x, t.y, t.z ); }
DirectX::XMFLOAT3 GenericMatrix::Translations( void )
{ return this->translation; }
void GenericMatrix::Rotate( float x, float y, float z )
{
pitch = x;
yaw = y;
roll = z;
this->rotation = DirectX::XMQuaternionRotationRollPitchYaw( pitch, yaw, roll );
this->matrixNeedsUpdate = true;
}
void GenericMatrix::Rotate( const DirectX::XMFLOAT3& r )
{ Rotate( r.x, r.y, r.z ); }
DirectX::XMVECTOR GenericMatrix::Rotations( void )
{ return this->rotation; }
DirectX::XMFLOAT3 GenericMatrix::RotationsPYR( void )
{
return DirectX::XMFLOAT3( pitch, yaw, roll );
}
DirectX::XMMATRIX GenericMatrix::Matrix( bool process )
{
if ( process && this->matrixNeedsUpdate )
{
UpdateMatrix();
this->matrixNeedsUpdate = false;
}
return this->matrix;
}
void GenericMatrix::UpdateMatrix( void )
{
DirectX::XMVECTOR scaleVec, translateVec;
scaleVec = DirectX::XMLoadFloat3( &this->scale );
translateVec = DirectX::XMLoadFloat3( &this->translation );
this->matrix = DirectX::XMMatrixScalingFromVector( scaleVec ) * DirectX::XMMatrixRotationQuaternion( this->rotation ) * DirectX::XMMatrixTranslationFromVector( translateVec );
}
当我将其用作模型矩阵时,在执行 Identity(),然后执行 Translate(0.0f, 0.0f, 5.0f) 之后,我的模型仅在我将相机定位在 [0,0,5] 时才可见,即使如此,只是一闪而过。我完成的最后一点 3d 编程是使用 OpenGL 1.x,所以我很有可能对矩阵做了一些奇怪的事情,但还没有找到任何可以告诉我如何做到这一点的东西。如果有人需要更多信息,请询问。
更新:更多细节 - 如果我将矩阵设置为 Identity() 并保留它,我可以移动相机并从任何位置毫无问题地查看模型,同时如果我将它移动到任何地方,它会破坏可见性。