Windows runtime DirectX 11.1矩阵乘法问题
我为我所有的3d对象写了一个通用的矩阵类,但是翻译似乎真的不正确 这是我的班级:Windows runtime DirectX 11.1矩阵乘法问题,windows-runtime,matrix-multiplication,directx-11,c++-cx,Windows Runtime,Matrix Multiplication,Directx 11,C++ Cx,我为我所有的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 Dire
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()之后,然后平移(0.0f,0.0f,5.0f),我的模型仅在我将相机定位在[0,0,5]时才可见,即使如此,它也只是一个闪烁。我做的最后一点3d编程是使用OpenGL 1.x,所以我很有可能对矩阵做了一些奇怪的事情,但是没有发现任何东西可以告诉我如何做。如果有人需要更多的信息,尽管问
更新:更多详细信息-如果我将矩阵设置为Identity(),并将其保留,我可以从任何位置移动相机并查看模型,而不会出现任何问题,同时,如果我将其移动到任何位置,则会扰乱可见性。我在几次测试后发现了问题。看起来我的相机使用的是左手坐标系,而我的透视图使用的是右手坐标系。在z 0.9到-0.9之间,左手和右手系统都同意模型应该是可见的,但是当我把它们移出那个空间时,两个矩阵都不能同意什么是可见的,除非我的相机在完全相同的坐标上 故事的寓意——记住在代码中始终使用相同的坐标系