为了能够确定用户是否点击了我的3D对象中的任何一个,我正在尝试将点击的屏幕坐标转换为向量,然后使用它来检查是否有任何三角形被击中。为此,我使用DirectX提供的XMVector3Unproject方法,并在C++/CX中实现所有内容。
我面临的问题是,从未投影的屏幕坐标得到的向量与我的预期完全不同。下面的图像说明了这一点:
单击发生时的光标位置(突出显示为黄色)在左侧的等轴测视图中可见。一旦我单击,从未投影的向量就会出现在模型后面,如图中表示为穿过模型的白线。因此,它并不是从等轴测视图中光标位置开始并进入屏幕,而是出现在完全不同的位置。
当我在等距视图中水平移动鼠标并单击后,再垂直移动鼠标并点击时,下方的图案会出现。两张图片中的所有线条都代表从点击中得出的向量。为了更好地展示,模型已被移除。
如上图所示,可以看出所有向量似乎都起源于同一位置。如果我改变视角并重复这个过程,则相同的图案将出现,但向量的起点不同。
这是我用来实现此功能的代码片段。首先,我使用以下代码获取光标位置,并将其与绘图区域的宽度和高度一起传递给我的“SelectObject”方法:
最终计算出的向量将被DirectX::TriangleTests命名空间下的Intersects方法使用,以检测三角形是否被击中。由于这个问题与代码无关,因此我省略了以上片段中的代码。
为了渲染这些图像,我使用正交投影矩阵和一个可以沿着其本地x和y轴旋转的摄像机来生成视图矩阵。世界矩阵始终保持不变,即它只是一个身份矩阵。
视图矩阵的计算方法如下(基于Frank Luna的书3D Game Programming中的示例):
世界/模型矩阵和投影矩阵的计算如下所示:
坦率地说,我还不理解我面临的问题。如果有更深入的了解的人能给我一些提示,告诉我在哪里需要进行更改,使得从未投影的向量起始于光标位置并进入屏幕,我将不胜感激。
编辑1: 我认为这可能与我的相机在世界坐标系中位于(0, 0, 0)有关。相机围绕其本地X和Y轴旋转。据我所知,由相机创建的视图矩阵构建了图像投影的平面。如果是这样,那么就可以解释为什么射线位于某种程度上“意外”的位置。
我的假设是,我需要将相机移出中心,使其位于对象之外。然而,如果我简单地修改相机的成员变量m_cameraPosition,我的模型就会完全失真。
是否有人能够提供帮助?
我面临的问题是,从未投影的屏幕坐标得到的向量与我的预期完全不同。下面的图像说明了这一点:
![Vector resulting from unprojection](https://istack.dev59.com/A0xbr.webp)
当我在等距视图中水平移动鼠标并单击后,再垂直移动鼠标并点击时,下方的图案会出现。两张图片中的所有线条都代表从点击中得出的向量。为了更好地展示,模型已被移除。
![Vector resulting from unprojection originate in same location](https://istack.dev59.com/8xP6v.webp)
void Demo::OnPointerPressed(Object^ sender, PointerEventArgs^ e)
{
Point currentPosition = e->CurrentPoint->Position;
if(m_model->SelectObject(currentPosition.X, currentPosition.Y, m_renderTargetWidth, m_renderTargetHeight))
{
m_RefreshImage = true;
}
}
"SelectObject"方法如下:
bool Model::SelectObject(float screenX, float screenY, float screenWidth, float screenHeight)
{
XMMATRIX projectionMatrix = XMLoadFloat4x4(&m_modelViewProjectionConstantBufferData->projection);
XMMATRIX viewMatrix = XMLoadFloat4x4(&m_modelViewProjectionConstantBufferData->view);
XMMATRIX modelMatrix = XMLoadFloat4x4(&m_modelViewProjectionConstantBufferData->model);
XMVECTOR v = XMVector3Unproject(XMVectorSet(screenX, screenY, 5.0f, 0.0f),
0.0f,
0.0f,
screenWidth,
screenHeight,
0.0f,
1.0f,
projectionMatrix,
viewMatrix,
modelMatrix);
XMVECTOR rayOrigin = XMVector3Unproject(XMVectorSet(screenX, screenY, 0.0f, 0.0f),
0.0f,
0.0f,
screenWidth,
screenHeight,
0.0f,
1.0f,
projectionMatrix,
viewMatrix,
modelMatrix);
// Code to retrieve v0, v1 and v2 is omitted
if(Intersects(rayOrigin, XMVector3Normalize(v - rayOrigin), v0, v1, v2, depth))
{
return true;
}
}
最终计算出的向量将被DirectX::TriangleTests命名空间下的Intersects方法使用,以检测三角形是否被击中。由于这个问题与代码无关,因此我省略了以上片段中的代码。
为了渲染这些图像,我使用正交投影矩阵和一个可以沿着其本地x和y轴旋转的摄像机来生成视图矩阵。世界矩阵始终保持不变,即它只是一个身份矩阵。
视图矩阵的计算方法如下(基于Frank Luna的书3D Game Programming中的示例):
void Camera::SetViewMatrix()
{
XMFLOAT3 cameraPosition;
XMFLOAT3 cameraXAxis;
XMFLOAT3 cameraYAxis;
XMFLOAT3 cameraZAxis;
XMFLOAT4X4 viewMatrix;
// Keep camera's axes orthogonal to each other and of unit length.
m_cameraZAxis = XMVector3Normalize(m_cameraZAxis);
m_cameraYAxis = XMVector3Normalize(XMVector3Cross(m_cameraZAxis, m_cameraXAxis));
// m_cameraYAxis and m_cameraZAxis are already normalized, so there is no need
// to normalize the below cross product of the two.
m_cameraXAxis = XMVector3Cross(m_cameraYAxis, m_cameraZAxis);
// Fill in the view matrix entries.
float x = -XMVectorGetX(XMVector3Dot(m_cameraPosition, m_cameraXAxis));
float y = -XMVectorGetX(XMVector3Dot(m_cameraPosition, m_cameraYAxis));
float z = -XMVectorGetX(XMVector3Dot(m_cameraPosition, m_cameraZAxis));
XMStoreFloat3(&cameraPosition, m_cameraPosition);
XMStoreFloat3(&cameraXAxis , m_cameraXAxis);
XMStoreFloat3(&cameraYAxis , m_cameraYAxis);
XMStoreFloat3(&cameraZAxis , m_cameraZAxis);
viewMatrix(0, 0) = cameraXAxis.x;
viewMatrix(1, 0) = cameraXAxis.y;
viewMatrix(2, 0) = cameraXAxis.z;
viewMatrix(3, 0) = x;
viewMatrix(0, 1) = cameraYAxis.x;
viewMatrix(1, 1) = cameraYAxis.y;
viewMatrix(2, 1) = cameraYAxis.z;
viewMatrix(3, 1) = y;
viewMatrix(0, 2) = cameraZAxis.x;
viewMatrix(1, 2) = cameraZAxis.y;
viewMatrix(2, 2) = cameraZAxis.z;
viewMatrix(3, 2) = z;
viewMatrix(0, 3) = 0.0f;
viewMatrix(1, 3) = 0.0f;
viewMatrix(2, 3) = 0.0f;
viewMatrix(3, 3) = 1.0f;
m_modelViewProjectionConstantBufferData->view = viewMatrix;
}
这受到两种方法的影响,它们分别围绕相机的x轴和y轴旋转相机:
void Camera::ChangeCameraPitch(float angle)
{
XMMATRIX rotationMatrix = XMMatrixRotationAxis(m_cameraXAxis, angle);
m_cameraYAxis = XMVector3TransformNormal(m_cameraYAxis, rotationMatrix);
m_cameraZAxis = XMVector3TransformNormal(m_cameraZAxis, rotationMatrix);
}
void Camera::ChangeCameraYaw(float angle)
{
XMMATRIX rotationMatrix = XMMatrixRotationAxis(m_cameraYAxis, angle);
m_cameraXAxis = XMVector3TransformNormal(m_cameraXAxis, rotationMatrix);
m_cameraZAxis = XMVector3TransformNormal(m_cameraZAxis, rotationMatrix);
}
世界/模型矩阵和投影矩阵的计算如下所示:
void Model::SetProjectionMatrix(float width, float height, float nearZ, float farZ)
{
XMMATRIX orthographicProjectionMatrix = XMMatrixOrthographicRH(width, height, nearZ, farZ);
XMFLOAT4X4 orientation = XMFLOAT4X4
(
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
);
XMMATRIX orientationMatrix = XMLoadFloat4x4(&orientation);
XMStoreFloat4x4(&m_modelViewProjectionConstantBufferData->projection, XMMatrixTranspose(orthographicProjectionMatrix * orientationMatrix));
}
void Model::SetModelMatrix()
{
XMFLOAT4X4 orientation = XMFLOAT4X4
(
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
);
XMMATRIX orientationMatrix = XMLoadFloat4x4(&orientation);
XMStoreFloat4x4(&m_modelViewProjectionConstantBufferData->model, XMMatrixTranspose(orientationMatrix));
}
坦率地说,我还不理解我面临的问题。如果有更深入的了解的人能给我一些提示,告诉我在哪里需要进行更改,使得从未投影的向量起始于光标位置并进入屏幕,我将不胜感激。
编辑1: 我认为这可能与我的相机在世界坐标系中位于(0, 0, 0)有关。相机围绕其本地X和Y轴旋转。据我所知,由相机创建的视图矩阵构建了图像投影的平面。如果是这样,那么就可以解释为什么射线位于某种程度上“意外”的位置。
我的假设是,我需要将相机移出中心,使其位于对象之外。然而,如果我简单地修改相机的成员变量m_cameraPosition,我的模型就会完全失真。
是否有人能够提供帮助?