对象在应用投 影矩阵后消失

问题描述 投票:0回答:1

我正在尝试使用DirectX显示3D立方体。我创建了一个x64 c ++ Windows应用程序,以下顶点和索引描述了多维数据集(请注意,该多维数据集没有故意的底部)

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

我没有深度/模具视图,因为我认为我不需要它,因为它只是一个简单的立方体。我创建了一个ConstantBuffer

struct ConstantBuffer {
    DirectX::XMMATRIX model;
    DirectX::XMMATRIX view;
    DirectX::XMMATRIX projection;
};

根据我从SDK的DirectXMath库使用的方法填充此缓冲区。

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

BackBuffer和窗口的大小为1920x1080像素。

我的顶点着色器如下:

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

我不知道怎么了。尽管我的Pixel Shader输出红色,并且我将所有矩阵都设置为Identity Matrix,但我的屏幕上什么也没画,但是看到的是立方体的红色正面。我认为它与透视矩阵有关(正交也不起作用)?

任何想法如何进一步调试它或有什么问题吗?

更新:另外,这是nvidia nsight向我展示的,如果有帮助,常量缓冲区中的矩阵是什么:

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)
c++ math matrix directx direct3d
1个回答
0
投票

我只是在查看Microsoft github页面上的一些示例后才弄清楚的。由于某些原因,他们使用XMMatrixTranspose。应用后可以使用。

但是,如果它是相同的技术,意味着纯DirectX和HLSL着色器,我不知道为什么要使用它。我以为它们都是列/行专业,没有什么不同。但是矩阵函数似乎返回了另一种格式。

© www.soinside.com 2019 - 2024. All rights reserved.