为什么这个基本"rotate around the origin"不起作用?



我已经做过一百次了,但这是我第一次用"棒"手工构建立方体,这是3D线条。它是围绕原点构建的,在X, Y和Z方向上都离原点5。

当我旋转它时,我仍然"在它里面",它围绕着我(相机)旋转。我正在应用平移和旋转,所以我不知道我做错了什么。

下面是旋转盒子的基本代码,我的意思是生成它的世界矩阵:

float rotateX = 0.0f, rotateY = 0.0f, rotateZ = 0.0f;
XMFLOAT4 positionBox = XMFLOAT4(0, 0, -50, 1);   // Camera at origin looking at this
XMMATRIX matrixCubeWorld;
void CALLBACK OnFrameMove( double fTime, float fElapsedTime, void* pUserContext )
{
    auto pCamera = g_GameServices.GetService<CWorldCamera>();
    XMMATRIX translation = XMMatrixTranslationFromVector(XMLoadFloat4(&positionBox));
    XMMATRIX rotation = XMMatrixRotationRollPitchYaw(rotateX, rotateY, rotateZ);
    matrixCubeWorld = rotation * translation;
    if (GetKeyState('X') < 0)
        rotateX = RotateAround(rotateX, fElapsedTime);
    if (GetKeyState('Y') < 0)
        rotateY = RotateAround(rotateY, fElapsedTime);
}

当我设置绘图时,我使用这个矩阵:

    D3D11_MAPPED_SUBRESOURCE MappedResource;
    V(pd3dImmediateContext->Map(_pVertexShaderVariables, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource));
    auto pCB = reinterpret_cast<VSCB3DLineChangesEveryFrame *>(MappedResource.pData);
    pCB->_gWorldViewProj = matrixCubeWorld * pCamera->GetViewMatrix() * pCamera->GetProjMatrix();
    pd3dImmediateContext->Unmap(_pVertexShaderVariables, 0);
    return hr;

…着色器是尽可能简单:

VertexShaderOutput Line3DVertexShaderFunction(float3 position : POSITION, float4 color : COLOR, float2 tex : TEXCOORD0)
{
    VertexShaderOutput output;
    output.position = mul(float4(position, 1), _gWorldViewProj);
    output.color    = color;
    output.tex      = tex;
    return output;
}

所以我有一个错误还是一个误解?我已经尝试了反向翻译,认为这将"在旋转之前将其带回原点",但并没有改善它。

转换看起来不错。也许是因为" XMMatrixTranslationFromVector "如文档(msdn)所述,仅支持3d-vector。

还要确保RotateAround函数和相机视图/投影矩阵给出正确的结果。

问好。

最新更新