I am trying to display an 3D cube with DirectX. I created a x64 c++ windows application and the following vertices and indices describe the cube (Note the cube has no bottom part on purpose)
Vertex cube[]
{
{-0.25f, -0.5f, 0.25f},
{-0.25f, 0.5f, 0.25f},
{0.25f, 0.5f, 0.25f},
{0.25f, -0.5f, 0.25f},
{-0.25f, -0.5f, 0.5f},
{-0.25f, 0.5f, 0.5f},
{0.25f, 0.5f, 0.5f},
{0.25f, -0.5f, 0.5f}
};
unsigned short cubeIndices[]{
0, 1, 2, // front
0, 2, 3,
4, 5, 6, // back
4, 6, 7,
1, 5, 6, // top
1, 6, 2,
0, 4, 5, // left
0, 5, 1,
3, 2, 6, // right
3, 6, 7
};
I have not Depth/Stencil view as I assume I do not need it as it is just one simple cube. I created a ConstantBuffer
struct ConstantBuffer {
DirectX::XMMATRIX model;
DirectX::XMMATRIX view;
DirectX::XMMATRIX projection;
};
This buffer is filled based on methods I use from the DirectXMath Library of the SDK.
// Based on MSDN, direct x uses left handed und looks in positive Z
m_constantBufferData.model = m_constantBufferData.model * DirectX::XMMatrixTranslation(0.0f, 0.0f, 8.0f); // move it by 8 units in positive z)
m_constantBufferData.view = DirectX::XMMatrixIdentity(); // I assume we do not need to put anything in view, because the object is in positive z and center
m_constantBufferData.projection = DirectX::XMMatrixPerspectiveFovLH(DirectX::XMConvertToRadians(90.0f), 1920.0f/1080.0f, 0.1f, 100.0f);
//m_constantBufferData.projection = DirectX::XMMatrixOrthographicLH(1920.0f, 1080.0f, 0.1f, 100.0f);
The BackBuffer and Window is of size 1920x1080 Pixels.
My Vertex Shader looks as follows:
cbuffer simpleConstantBuffer : register(b0)
{
matrix model;
matrix view;
matrix projection;
}
float4 main(float4 pos : POSITION) : SV_POSITION
{
float4 output = pos;
output = mul(output, model);
output = mul(output, view);
output = mul(output, projection);
return output;
}
I do not know what could be wrong. Nothing is drawn on screen, although my Pixel Shader outputs red color and if I set all matrices to the Identity Matrix I see the red front face of the cube rendered. I assume it is something with the perspective matrix (orthogonal also does not work)?
Any idea how to debug this further or what could be wrong?
UPDATE: As an addition this is what nvidia nsight shows me what the matrices in the constant buffer are in case that helps:
Model:
(1.00, 0.00, 0.00, 0.00)
(0.00, 1.00, 0.00, 0.00)
(0.00, 0.00, 1.00, 8.00)
(0.00, 0.00, 0.00, 1.00)
View:
(1.00, 0.00, 0.00, 0.00)
(0.00, 1.00, 0.00, 0.00)
(0.00, 0.00, 1.00, 1.00)
(0.00, 0.00, 0.00, 1.00)
Projection:
(0.56, 0.00, 0.00, 0.00)
(0.00, 1.00, 0.00, 0.00)
(0.00, 0.00, 1.00, -0.10)
(0.00, 0.00, 1.00, 0.00)