三角网格无法正确渲染[DirectX 11]

时间:2016-01-28 10:58:26

标签: c++ directx directx-11 terrain

我正在尝试为DirectX 11中的地形生成项目制作三角形网格,但是当它被绘制到屏幕时,它会绘制所有三个轴,而不仅仅是x和z。

enter image description here enter image description here

我确实获得了正确数量的顶点和索引,但是在索引向量中它的大小为972,其中前486个被设置为0而不是实际值。

enter image description here

我想知道是否可以澄清我是否正确设置了顶点/索引缓冲区。

以下是10 x 10网格的示例。

GenerateTerrain方法

void Application::GenerateTerrain(int vertRows, int vertCols)
{
    HRESULT hr;

    // ------------------------------------- Create Vertex Buffer --------------------------------------

    totalCellRows = vertRows - 1;
    totalCellCols = vertCols - 1;

    // Width and Total Width
    float dx = 1.0f;
    float totalWidth = totalCellCols * dx;

    // Depth and Total Depth
    float dz = 1.0f;
    float totalDepth = totalCellRows * dz;

    // X and Z Offsets
    float xOffset = -totalWidth * 0.5f;
    float zOffset = totalDepth * 0.5f;

    totalVertices = vertRows * vertCols;
    totalTriangles = (totalCellRows * totalCellCols) * 2;
    totalIndices = totalTriangles * 3;

    terrainVertices = new SimpleVertex[totalVertices];

    // Array Version
    int k = 0;
    for (int i = 0; i < vertRows; i++)
    {
        for (int j = 0; j < vertCols; j++)
        {
            SimpleVertex newVertex;
            terrainVertices[k].PosL = XMFLOAT3(j * dx + xOffset, 0.0f, -(i * dz) + zOffset);
            terrainVertices[k].NormL = XMFLOAT3(0.0f, 1.0f, 0.0f);
            terrainVertices[k].Tex = XMFLOAT2(0.0f, 0.0f);
            k++;
        }
    }

    D3D11_BUFFER_DESC bd;
    ZeroMemory(&bd, sizeof(bd));
    bd.Usage = D3D11_USAGE_DEFAULT;
    bd.ByteWidth = sizeof(SimpleVertex) * totalVertices;
    bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    bd.CPUAccessFlags = 0;

    D3D11_SUBRESOURCE_DATA InitData;
    ZeroMemory(&InitData, sizeof(InitData));
    InitData.pSysMem = &terrainVertices;

    hr = _pd3dDevice->CreateBuffer(&bd, &InitData, &_pGridVertexBuffer);

    // ------------------------------------- Create Index Buffer --------------------------------------

    // Vector Version

    indices.resize(totalIndices);
    for (WORD i = 0; i < (WORD)vertRows - 1; i++)
    {
        for (WORD j = 0; j < (WORD)vertCols - 1; j++)
        {
            indices.push_back(i * vertCols + j);
            indices.push_back(i * vertCols + (j + 1));
            indices.push_back((i + 1) * vertCols + j);

            indices.push_back((i + 1) * vertCols + j);
            indices.push_back((i * vertCols + (j + 1)));
            indices.push_back((i + 1) * vertCols + (j + 1));
        }
    }

    ZeroMemory(&bd, sizeof(bd));
    bd.Usage = D3D11_USAGE_DEFAULT;
    bd.ByteWidth = sizeof(WORD) * totalIndices;
    bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
    bd.CPUAccessFlags = 0;

    ZeroMemory(&InitData, sizeof(InitData));
    InitData.pSysMem = &indices;
    hr = _pd3dDevice->CreateBuffer(&bd, &InitData, &_pGridIndexBuffer);
}

1 个答案:

答案 0 :(得分:0)

indices.resize(totalIndices); <----- !!!!!!ERROR!!!! u mean reserve
for (WORD i = 0; i < (WORD)vertRows - 1; i++)
{
    for (WORD j = 0; j < (WORD)vertCols - 1; j++)
    {
        indices.push_back(i * vertCols + j);
        indices.push_back(i * vertCols + (j + 1));
        indices.push_back((i + 1) * vertCols + j);

        indices.push_back((i + 1) * vertCols + j);
        indices.push_back((i * vertCols + (j + 1)));
        indices.push_back((i + 1) * vertCols + (j + 1));
    }
}

您编写resize,因此调整了矢量大小,然后使用pushback添加更多索引。我想你想要保留内存“预留”,然后用后推添加它们。

另外我建议使用指向第一个值的指针,而不是指向矢量

的指针

在:

InitData.pSysMem = &indices;

更好:

InitData.pSysMem = &indices[0];

缓冲区初始化似乎没问题。

祝你好运