DirectX 11 Grid Not getting Drawn Properly

552 views Asked by At

I'm new to DirectX programming... I wrote a code to draw grid (following Frank D. Luna). The code works almost correctly - grid is drawn but not all the vertices. Here is the image of the Grid: image of the grid

I'm drawing a 4x4 grid. As seen in the image, the bottom-most vertices are not drawn, and in each row an extra triangle is drawn, stretching from 1st quad to last quad in each row

Code:

void Model::CreateGrid(const Vector3& centerPos,float width,float depth,int verticesX,int verticesZ 
{
    assert(verticesX > 1 || verticesZ >1);
    gridVertexCount = verticesX*verticesZ;
    gridIndexCount = (verticesX - 1)*(verticesZ - 1)*6;
    v.resize(gridVertexCount);
    vIndex.resize(gridIndexCount);
    float dx = width / (verticesX - 1);
    float dz = depth / (verticesZ - 1);
    float halfWidth = width / 2.0f;
    float halfDepth = depth / 2.0f;
    //Compute The Grid Vertex
    for (size_t rows = 0; rows < verticesZ; rows++)
    {
        float z = halfDepth - rows*dz;
        for (size_t col = 0; col < verticesX; col++)
        {
            float x = -halfWidth + col*dx;
            v[col + rows*verticesX].pos = XMFLOAT3(x+centerPos.x, 0.0f, z + centerPos.z);
            v[col + rows*verticesX].color = Colors::White;
        }
    }

    //Compute the Grid Indices
    UINT k = 0;
    for (size_t row = 0; row < verticesZ - 1; row++)
    {
        for (size_t col = 0; col < verticesX - 1; col++)
        {
            //Index of One Quad
            vIndex[k] = row*verticesX + col;
            vIndex[k + 1] = row*verticesX+col+1;
            vIndex[k + 2] = (row + 1)*verticesX + col;
            vIndex[k + 3] = (row + 1)*verticesX + col;
            vIndex[k + 4] = row*verticesX + col + 1;
            vIndex[k + 5] = (row + 1)*verticesX + (col+1);
            //Move to Next Quad
            k +=6;
        }
    }
    gridVertexSize = v.size();
    gridIndexSize = vIndex.size();
    //Add Vertex Data to Global Mesh Data
    globalMesh.VertexData.insert(globalMesh.VertexData.end(), v.begin(),v.end());
    v.clear();
    //Add Index Data to Global Mesh Data
    globalMesh.IndexData.insert(globalMesh.IndexData.end(), vIndex.begin(), vIndex.end());
    vIndex.clear();
    this->BuildGeometryBuffers();
    this->BuildFX();
    this->BuildVertexLayout(nullptr);
}

//Then I Set the Buffers
void Model::BuildGeometryBuffers()

{       

    //Set Vertex Buffer Description
    D3D11_BUFFER_DESC vbd;
    vbd.Usage = D3D11_USAGE_IMMUTABLE;
    vbd.ByteWidth = (UINT)sizeof(Vertex)*globalMesh.VertexData.size();       
    vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    vbd.CPUAccessFlags = 0;
    vbd.MiscFlags = 0;
    vbd.StructureByteStride = 0;
    D3D11_SUBRESOURCE_DATA vintData;
    vintData.pSysMem = &globalMesh.VertexData[0];
    device->CreateBuffer(&vbd, &vintData, &m_vB);

    //Set Index Buffer Description
    D3D11_BUFFER_DESC ibd;

    ibd.Usage = D3D11_USAGE_IMMUTABLE;
    ibd.ByteWidth = (UINT)sizeof(UINT)*globalMesh.IndexData.size();        
    ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;
    ibd.CPUAccessFlags = 0;
    ibd.MiscFlags = 0;
    ibd.StructureByteStride = 0;
    D3D11_SUBRESOURCE_DATA iinitData;
    iinitData.pSysMem = &globalMesh.IndexData[0];
    device->CreateBuffer(&ibd, &iinitData, &m_iB);
}

What is wrong with my code?

1

There are 1 answers

1
Tesla On

Solved it : Changed this :

        vIndex[k] = row*verticesX + col;
        vIndex[k + 1] = row*verticesX+col+1;
        vIndex[k + 2] = (row + 1)*verticesX + col;
        vIndex[k + 3] = (row + 1)*verticesX + col;
        vIndex[k + 4] = row*verticesX + col + 1;
        vIndex[k + 5] = (row + 1)*verticesX + (col+1);

To:

            vIndex[k] = row*verticesX + col;
            vIndex[k + 1] = row*verticesX+col+1;
            vIndex[k + 2] = (row + 1)*verticesX + col;
            vIndex[k + 3] = row*verticesX + col + 1;
            vIndex[k + 4] = (row + 1)*verticesX + (col + 1);
            vIndex[k + 5] = (row + 1)*verticesX + col;