0

我在 DirectX 11 中创建 3D 第一人称相机时遇到问题。

我在 (0, 0, -2) 有一个摄像头,正在观察 (0, 0, 100)。(0, 0, 0) 处有一个框,并且该框被正确渲染。请看下面这张图片:

在此处输入图像描述

当盒子(不是相机)的位置发生变化时,它会被正确渲染。例如,下一张图片显示了 (1, 0, 0) 处的盒子,而相机仍然位于 (0, 0, -2) 处:

在此处输入图像描述

但是,只要相机向左或向右移动,盒子就会向相反的方向移动,但它看起来却是扭曲的。这是一个示例,当相机位于 (1, 0, -2) 并注视 (1, 0, 100) 时。盒子仍然在 (0, 0, 0):

在此处输入图像描述

这是我设置相机的方法:

// Set the world transformation matrix.

D3DXMATRIX rotationMatrix;          // A matrix to store the rotation information
D3DXMATRIX scalingMatrix;           // A matrix to store the scaling information
D3DXMATRIX translationMatrix;       // A matrix to store the translation information

D3DXMatrixIdentity(&translationMatrix);

// Make the scene being centered on the camera position.
D3DXMatrixTranslation(&translationMatrix, -camera.GetX(), -camera.GetY(), -camera.GetZ());

m_worldTransformationMatrix = translationMatrix;

// Set the view transformation matrix.

D3DXMatrixIdentity(&m_viewTransformationMatrix);

D3DXVECTOR3 cameraPosition(camera.GetX(), camera.GetY(), camera.GetZ());

// ------------------------
// Compute the lookAt position
// ------------------------

const FLOAT lookAtDistance = 100;

FLOAT lookAtXPosition = camera.GetX() + lookAtDistance * cos((FLOAT)D3DXToRadian(camera.GetXZAngle()));
FLOAT lookAtYPosition = camera.GetY() + lookAtDistance * sin((FLOAT)D3DXToRadian(camera.GetYZAngle()));
FLOAT lookAtZPosition = camera.GetZ() + lookAtDistance * (sin((FLOAT)D3DXToRadian(camera.GetXZAngle())) * cos((FLOAT)D3DXToRadian(camera.GetYZAngle())));

D3DXVECTOR3 lookAtPosition(lookAtXPosition, lookAtYPosition, lookAtZPosition);

D3DXVECTOR3 upDirection(0, 1, 0);

D3DXMatrixLookAtLH(&m_viewTransformationMatrix,
                   &cameraPosition,
                   &lookAtPosition,
                   &upDirection);

RECT windowDimensions = GetWindowDimensions();
FLOAT width = (FLOAT)(windowDimensions.right - windowDimensions.left);
FLOAT height = (FLOAT)(windowDimensions.bottom - windowDimensions.top);

// Set the projection matrix.

D3DXMatrixIdentity(&m_projectionMatrix);
D3DXMatrixPerspectiveFovLH(&m_projectionMatrix,
                           (FLOAT)(D3DXToRadian(45)),  // Horizontal field of view
                           width / height,    // Aspect ratio
                           1.0f,              // Near view-plane
                           100.0f);           // Far view-plane

以下是最终矩阵的设置方式:

D3DXMATRIX finalMatrix = m_worldTransformationMatrix * m_viewTransformationMatrix * m_projectionMatrix;

// Set the new values for the constant buffer
mp_deviceContext->UpdateSubresource(mp_constantBuffer, 0, 0, &finalMatrix, 0, 0);

最后,这是使用常量缓冲区的顶点着色器:

VOut VShader(float4 position : POSITION, float4 color : COLOR, float2 texcoord : TEXCOORD)
{
    VOut output;

    output.color = color;
output.texcoord = texcoord;
output.position = mul(position, finalMatrix);  // Transform the vertex from 3D to 2D

    return output;
}

你看到我做错了吗?如果您需要有关我的代码的更多信息,请随时提出:我真的希望它能够正常工作。

谢谢!

4

1 回答 1

1

The problem is you are setting finalMatrix with a row major matrix, but HLSL expects a column major matrix. The solution is to use D3DXMatrixTranspose before updating the constants, or declare row_major in the HLSL file like this:

cbuffer ConstantBuffer
{
    row_major float4x4 finalMatrix;
}
于 2012-05-04T08:42:18.377 回答