0
votes

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)
1
It looks like your triangles use inconsistent handedness. When looked at straight on, some go clockwise (0, 1, 2), others counterclockwise (4, 5, 6). With back face culling, this can cause you to not have anything rendered. You should change the index ordering of your triangles so that they all go the same way (or turn off back face culling, but that can introduce performance penalties). - 1201ProgramAlarm
I also thought the same so I turned off culling by setting the Rasterizer State. However what I also found is that when I debug with nvidia nsight it should highlight the vertices it tries to draw in the RenderTargetView when I click on the DrawIndexed call. But nothing is there... If i set back to the Identity matrix I can see the object drawn. - user7145038

1 Answers

0
votes

I just figured it out after looking at some examples on Microsofts github page. For some reason they use XMMatrixTranspose. After applying that it works.

However I do not know why I have to use this if it is the same technology, meaning pure DirectX and HLSL shaders. I thought they both are column/row major and not different. But it seems the matrix functions return another format.