首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >DirectX 11.1矩阵乘法问题

DirectX 11.1矩阵乘法问题
EN

Stack Overflow用户
提问于 2013-02-05 20:20:57
回答 1查看 1K关注 0票数 1

我为我所有的3d对象编写了一个泛型矩阵类,但是翻译似乎真的是不对的。

这是我的班级:

代码语言:javascript
运行
复制
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;
};

来文提交人:

代码语言:javascript
运行
复制
#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,所以我很有可能对矩阵做了一些奇怪的事情,但是没有发现任何东西可以告诉我如何不这样做。如果有人需要更多的信息,就问一问。

update:提供了更多的细节--如果我将矩阵设置为Identity()并离开它,我可以移动相机并从任何位置看到模型,而如果我将其移动到任何位置,则会使可见性受到影响。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-07 14:42:15

经过几次测试,我发现了这个问题。我的相机似乎使用了左手坐标系,而右手则是我的视角。在z 0.9到-0.9之间,左右手系统都能同意模型应该是可见的,但是当我将它们移出这个空间时,两个矩阵都不能就可见的东西达成一致,除非我的相机在完全相同的坐标上。

故事的寓意--记住在整个代码中始终使用相同的坐标系统。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14716198

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档