DirectX11:沿z轴平移会导致顶点变形

时间:2014-06-14 01:03:32

标签: c++ directx

我正在渲染一个矩形棱镜并翻译它。然而,当我将它从相机中移开时,有时模型会做一些意想不到的事情;它会延伸或根本不翻译。它似乎都取决于顶点的z坐标。如果模型的正面从2.0f开始,则模型转换得很好。但是,如果前部处于最小Z距离(1.0f),则模型将伸展,并且不会翻译相同的面部。如果前面在1.0f后面,模型将根本不显示在屏幕上。

这是我的模型数据:

前三个浮点数是位置,接下来三个是正常值,最后两个是紫外线对

VertexData cubeData[] = 
{

    //back
    {-0.5f,-0.5f,1.0f,0.0f,0.0f,1.0f,0.0f,0.0f},
    {-0.5f, 0.5f,1.0f,0.0f,0.0f,1.0f,0.0f,0.0f},
    { 0.5f, 0.5f,1.0f,0.0f,0.0f,1.0f,0.0f,0.0f},
    {-0.5f,-0.5f,1.0f,0.0f,0.0f,1.0f,0.0f,0.0f},
    { 0.5f,-0.5f,1.0f,0.0f,0.0f,1.0f,0.0f,0.0f},
    { 0.5f, 0.5f,1.0f,0.0f,0.0f,1.0f,0.0f,0.0f},

    //front
    {-0.5f,-0.5f,-1.0f,0.0f,0.0f,-1.0f,0.0f,0.0f},
    {-0.5f, 0.5f,-1.0f,0.0f,0.0f,-1.0f,0.0f,0.0f},
    { 0.5f, 0.5f,-1.0f,0.0f,0.0f,-1.0f,0.0f,0.0f},
    {-0.5f,-0.5f,-1.0f,0.0f,0.0f,-1.0f,0.0f,0.0f},
    { 0.5f,-0.5f,-1.0f,0.0f,0.0f,-1.0f,0.0f,0.0f},
    { 0.5f, 0.5f,-1.0f,0.0f,0.0f,-1.0f,0.0f,0.0f},

    //left
    {-0.5f,-0.5f, 1.0f,-1.0f,0.0f,0.0f,0.0f,0.0f},
    {-0.5f, 0.5f, 1.0f,-1.0f,0.0f,0.0f,0.0f,0.0f},
    {-0.5f,-0.5f,-1.0f,-1.0f,0.0f,0.0f,0.0f,0.0f},
    {-0.5f, 0.5f,-1.0f,-1.0f,0.0f,0.0f,0.0f,0.0f},
    {-0.5f, 0.5f, 1.0f,-1.0f,0.0f,0.0f,0.0f,0.0f},
    {-0.5f,-0.5f,-1.0f,-1.0f,0.0f,0.0f,0.0f,0.0f},

    //right
    {0.5f,-0.5f, 1.0f,1.0f,0.0f,0.0f,0.0f,0.0f},
    {0.5f, 0.5f, 1.0f,1.0f,0.0f,0.0f,0.0f,0.0f},
    {0.5f,-0.5f,-1.0f,1.0f,0.0f,0.0f,0.0f,0.0f},
    {0.5f, 0.5f,-1.0f,1.0f,0.0f,0.0f,0.0f,0.0f},
    {0.5f, 0.5f, 1.0f,1.0f,0.0f,0.0f,0.0f,0.0f},
    {0.5f,-0.5f,-1.0f,1.0f,0.0f,0.0f,0.0f,0.0f}
};

我使用for循环来改变每个顶点的z坐标

std::vector<VertexData> vertexDataCube;
float distance = 0.0f;
for(int i = 0;i < 24;i++)
{
    cubeData[i].z += distance;
    vertexDataCube.push_back(cubeData[i]);
}

这是程序的输出

距离 = 0.0f

距离 = 2.0f

距离 = 1.0f

距离 = -1.0f

在这些图片的每一个中,顶点已向前翻译了5个单位,但就像我上面说的那样,如果距离为负,这似乎不会改变输出,并且如果距离为0.0f。

以下是我用来设置模型,视图和投影矩阵的代码。

DirectX::XMFLOAT3 look,pos,up;
look = DirectX::XMFLOAT3(0.0f,0.0f,100.0f);
pos = DirectX::XMFLOAT3(0.0f,0.0f,1.0f);
up = DirectX::XMFLOAT3(0.0f,1.0f,0.0f);

XMStoreFloat4x4(&constBufferData.mModel,DirectX::XMMatrixIdentity());
XMStoreFloat4x4(&constBufferData.mView,DirectX::XMMatrixLookToLH(DirectX::XMLoadFloat3(&pos),DirectX::XMLoadFloat3(&look),DirectX::XMLoadFloat3(&up)));
XMStoreFloat4x4(&constBufferData.mPerspective,DirectX::XMMatrixPerspectiveFovLH(3.14159f/4.0f,WINDOW_WIDTH/WINDOW_HEIGHT,1.0f,100.0f));

然后在我的主循环中,我根据用户输入翻译模型矩阵,但是现在我只需将其翻译为5.0f。

XMStoreFloat4x4(&constBufferData.mModel,DirectX::XMMatrixTranslation(0.0f,0.0f,5.0f));

1 个答案:

答案 0 :(得分:1)

您的屏幕截图绝对有意义:

  1. 距离= 0:您在立方体内部并看到内部的部分内容。由于近剪裁平面,很可能没有绘制两个平面。

  2. 立方体相距2个单位,大小为1个单位。因此,你看到它的一面。如果你稍微旋转一下,你也会看到另外两面(试试吧!)。

  3. 距离是1个单位。立方体尺寸也是1个单位:立方体正好在(数学上接触)相机前面。因此它覆盖整个屏幕。

  4. 立方体在相机后面。没什么好看的。这就是呈现的内容:背景。

  5. Imho所有的屏幕截图都显示了我希望他们展示的内容。