【问题标题】:DirectX 11 Grid Not getting Drawn ProperlyDirectX 11 网格未正确绘制
【发布时间】:2016-12-26 16:40:42
【问题描述】:

我是 DirectX 编程的新手... 我写了一个代码来绘制网格(跟随 Frank D. Luna)。该代码几乎可以正常工作 - 绘制了网格但不是所有的顶点。这是网格的图像:

我正在绘制一个 4x4 网格。 如图所示,最底部的顶点未绘制,并且在每一行中绘制了一个额外的三角形,从每行中的第一个四边形延伸到最后一个四边形

代码:

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

我的代码有什么问题?

【问题讨论】:

  • 问题已解决。
  • 请注意,您应该查看this postDirectX Tool Kit。弗兰克的书很有用,但有点过时,尤其是在谈到现在的legacy DirectX SDK
  • 是的,这是我在学习 DirectX 时遇到的主要问题,本书中使用的大多数技术现在已经过时了。

标签: c++ directx direct3d direct3d11


【解决方案1】:

解决了: 改变了这个:

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

收件人:

            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;

【讨论】:

  • 问题在于缠绕顺序 xD
猜你喜欢
  • 1970-01-01
  • 2017-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-23
  • 2020-04-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多