简体   繁体   English

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

[英]DirectX11: Translating along z axis causes vertex deformation

I am rendering a rectangular prism and translating it. 我正在渲染一个矩形棱镜并对其进行平移。 However, when I translate it away from the camera, sometimes the model does some unexpected things; 但是,当我将其平移到远离相机的位置时,有时模型会执行一些意外的操作。 it will stretch or not translate at all. 它会拉伸或完全不翻译。 It seems to all depend on what the z coordinate of the vertex is. 它似乎完全取决于顶点的z坐标。 If the front of the model starts at 2.0f, the model translates just fine. 如果模型的前端从2.0f开始,则模型转换就很好。 However, if the front is at the minimum Z distance (1.0f) the model will stretch and that same face will not be translated. 但是,如果正面在最小Z距离(1.0f)处,则模型将拉伸并且不会平移同一张脸。 If the front is behind 1.0f, the model will not be displayed on screen at all. 如果前端在1.0f后面,则该模型将完全不显示在屏幕上。

Here is my model data: 这是我的模型数据:

The first three floats are position,next three are normals, and the last two are a uv pair 前三个浮点是位置,下三个浮点是法线,后两个浮点是uv对

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}
};

I am using a for loop to change the z coordinates of each vertex 我正在使用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]);
}

Here is the output of the program 这是程序的输出

distance = 0.0f 距离 = 0.0f

distance = 2.0f 距离 = 2.0f

distance = 1.0f 距离 = 1.0f

distance = -1.0f 距离 = -1.0f

In each of these pictures the vertices have been translated 5 units forward, but like I said above this doesn't seem to change the output if the distance is negative, and it distorts the model if distance is 0.0f. 在每张图片中,顶点都已向前平移了5个单位,但是正如我在上面说的那样,如果距离为负,这似乎并不会改变输出,如果距离为0.0f,则会扭曲模型。

Here is the code I'm using to set up my model, view, and projection matrices. 这是我用来设置模型,视图和投影矩阵的代码。

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));

And then in my main loop I translate the model matrix based on user input, but for now I just translate it by 5.0f. 然后在我的主循环中,根据用户输入转换模型矩阵,但现在我仅将其转换为5.0f。

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

Your screen-shots make absolute sense: 您的屏幕截图绝对有道理:

  1. Distance=0: You are inside the cube and see parts of the inside. 距离= 0:您在多维数据集中,并且看到其中的一部分。 Two planes have been most likely not drawn because of the near clipping plane. 由于剪切平面很近,所以很可能没有绘制两个平面。

  2. The cube is 2 units apart and 1 unit in size. 立方体相距2个单位,大小为1个单位。 Therefor you see one side of it. 因此,您会看到它的一侧。 If you would rotate it a bit you would also see two other sides as well (Try it!). 如果稍微旋转一下,也会看到另外两个侧面(尝试一下!)。

  3. Distance is 1 unit. 距离为1个单位。 Cube size is 1 unit as well: The cube is right in front of (mathematically touching the) camera. 多维数据集的大小也为1个单位:多维数据集就在相机(在数学上接触)的前面。 Therefore it covers the entire screen. 因此,它覆盖了整个屏幕。

  4. The cube is behind the camera. 立方体在相机后面。 Nothing to see. 没什么可看的。 And that is what gets rendered: The background. 这就是渲染的内容:背景。

Imho all your screen-shots show what I would expect them to show. 恕我直言,您所有的屏幕截图都显示了我希望它们显示的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM