1

我在使用 DirectX 中的顶点和索引缓冲区进行绘制时遇到了一些问题。

这是一个定义立方体的函数,但我已经注释掉了除正面之外的所有内容:

void GeometryGenerator::CreateCube(MeshData& meshData)
    {
meshData.Vertices.resize(8);
meshData.Indices.resize(6);

float width;
float height;
float depth;

width = 45;
height = 45;
depth = 45;

XMFLOAT3 pos[8] = 
{
    XMFLOAT3(-0.5f * width, -0.5f * height, -0.5f * depth),
    XMFLOAT3(-0.5f * width, -0.5f * height,  0.5f * depth),
    XMFLOAT3(-0.5f * width,  0.5f * height, -0.5f * depth),
    XMFLOAT3(-0.5f * width,  0.5f * height,  0.5f * depth),
    XMFLOAT3( 0.5f * width, -0.5f * height, -0.5f * depth),
    XMFLOAT3( 0.5f * width, -0.5f * height,  0.5f * depth),
    XMFLOAT3( 0.5f * width,  0.5f * height, -0.5f * depth),
    XMFLOAT3( 0.5f * width,  0.5f * height,  0.5f * depth)
};

unsigned short k[6] = 
{
    ////1,0,2, // -x
    ////2,1,0,

    ////6,5,1, // +x
    ////6,7,5,
    ////
    ////0,1,5, // -y
    ////0,5,4,
    ////
    ////2,6,7, // +y
    ////2,7,3,

    ////4,6,0, // -z
    ////6,2,0,

    7,3,1, //+z
    5,7,1,
};

for(size_t i = 0; i < 8; ++i)
    meshData.Vertices[i].Position = pos[i];

for(size_t i = 0; i < 6; ++i)
    meshData.Indices[i] = k[i];

}

它绘制了一个三角形和一条线,它们都没有到达预期的顶点。一个三角形看起来覆盖了 7,3,0,并且有一条线覆盖了 1,0。我在下面附上了 90 度旋转的图像:

0度 90度 180度 360度

我很高兴发布任何必要的代码,但我怀疑您是否希望我发布我的整个项目。我认为可能有用的一些事情是......

我的顶点结构:

struct Vertex
{   
DirectX::XMFLOAT3 pos;
DirectX::XMFLOAT3 color;
};

我的输入布局:

    const D3D11_INPUT_ELEMENT_DESC vertexDesc[] = 
    {
        { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0 },
        { "COLOR",    0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    };

我的顶点着色器:

cbuffer ModelViewProjectionConstantBuffer : register(b0)
{
matrix model;
matrix view;
matrix projection;
};

struct VertexShaderInput
{
float3 pos : POSITION;
float3 color : COLOR0;
};

struct VertexShaderOutput
{
float4 pos : SV_POSITION;
float3 color : COLOR0;
};

VertexShaderOutput main(VertexShaderInput input)
{
VertexShaderOutput output;
float4 pos = float4(input.pos, 1.0f);

// Transform the vertex position into projected space.
pos = mul(pos, model);
pos = mul(pos, view);
pos = mul(pos, projection);
output.pos = pos;

// Pass through the color without modification.
output.color = input.color;

return output;
}

原始拓扑:

m_d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

索引缓冲区格式:

m_d3dContext->IASetIndexBuffer(
    m_indexBuffer.Get(),
    DXGI_FORMAT_R16_UINT,
    0);

顶点步幅和偏移量:

UINT stride = sizeof(Vertex);
UINT offset = 0;
m_d3dContext->IASetVertexBuffers(
    0,
    1,
    m_vertexBuffer.GetAddressOf(),
    &stride,
    &offset);

这是 DrawIndexed 调用:

m_d3dContext->DrawIndexed(
    m_Indices.size(),
    0,
    0);

获取数据并创建索引缓冲区的代码:

    GeometryGenerator generator;
    GeometryGenerator::MeshData cubeData;
    generator.CreateCube(cubeData);

            //m_Indices is a std::Vector<Vertex>
    m_Indices.resize(cubeData.Indices.size());

    for(size_t i = 0; i < cubeData.Indices.size(); i++)
        m_Indices[i] = cubeData.Indices[i];

    D3D11_SUBRESOURCE_DATA indexBufferData = {0};
    indexBufferData.pSysMem = &m_Indices[0];
    indexBufferData.SysMemPitch = 0;
    indexBufferData.SysMemSlicePitch = 0;

    const UINT indexBufferWidth = m_Indices.size() * sizeof(UINT);
    CD3D11_BUFFER_DESC indexBufferDesc(indexBufferWidth, D3D11_BIND_INDEX_BUFFER);
    DX::ThrowIfFailed(
        m_d3dDevice->CreateBuffer(
            &indexBufferDesc,
            &indexBufferData,
            &m_indexBuffer));

感谢您的帮助!我一直在研究 Frank Luna 的“DirectX 11 3D 游戏编程简介”,但我找不到任何关于此类问题的讨论。

4

1 回答 1

4

您很可能在以下其中一项中犯了错误。任何一个错误都可能很容易导致您正在观察的问题。

// Input layout defined correctly:
D3D11_INPUT_ELEMENT_DESC vertexDesc[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 } };

// Vertex shader input matches input layout:
VS_OUT main(float3 pos : POSITION)

// Primitive topology set correctly:
context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

// Index buffer format set correctly:
context->IASetIndexBuffer(indexBuffer, DXGI_FORMAT_R16_UINT, 0);

// Vertex stride and offset set correctly:
UINT stride = sizeof(XMFLOAT3);
UINT offset = 0;
context->IASetVertexBuffers(0, 1, vertexBuffer, &stride, &offset);

// Draw arguments are correct:
context->DrawIndexed(6, 0, 0);
于 2013-10-23T00:06:15.630 回答