我是DirectX编程新手...... 我写了一个代码来绘制网格(跟随Frank D. Luna)。代码几乎正确工作 - 绘制网格但不是所有顶点。这是Grid的图像:
我正在绘制一个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);
}
我的代码出了什么问题?
答案 0 :(得分:0)
解决了它: 改变了这个:
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;