0
votes

I'm trying to make a grid of triangles for a terrain generation project in DirectX 11, but when it gets drawn to the screen it draws in all three axis, instead of just the x and z.

enter image description here enter image description here

I do get the correct amount of vertices and indices, but in the vector of indices it has a size of 972, the first 486 of them are set to 0 instead of the actual values.

enter image description here

I was wondering if I could get some clarification on whether I was setting the vertex/index buffers correctly.

Below is an example for a 10 by 10 grid.

GenerateTerrain method

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
The declaration of indices isn't shown... might be important.MuertoExcobito

1 Answers

0
votes
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));
    }
}

You write resize, so the vector is resized, and then you add more indices with pushback. I think you want to reserve memory "reserve" and add them with pushback.

Also i would recommend to use a pointer to the first value, instead of a pointer to the vector

you:

InitData.pSysMem = &indices;

better:

InitData.pSysMem = &indices[0];

Buffer initialization seems to be ok.

Good luck