Reputation: 97
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. If the front of the model starts at 2.0f, the model translates just fine. However, if the front is at the minimum Z distance (1.0f) the model will stretch and that same face will not be translated. If the front is behind 1.0f, the model will not be displayed on screen at all.
Here is my model data:
The first three floats are position,next three are normals, and the last two are a uv pair
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
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
distance = 2.0f
distance = 1.0f
distance = -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.
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.
XMStoreFloat4x4(&constBufferData.mModel,DirectX::XMMatrixTranslation(0.0f,0.0f,5.0f));
Upvotes: 0
Views: 265
Reputation: 86353
Your screen-shots make absolute sense:
Distance=0: You are inside the cube and see parts of the inside. Two planes have been most likely not drawn because of the near clipping plane.
The cube is 2 units apart and 1 unit in size. 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!).
Distance is 1 unit. Cube size is 1 unit as well: The cube is right in front of (mathematically touching the) camera. Therefore it covers the entire screen.
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.
Upvotes: 1