我是DirectX编程的新手。我写了一个绘制网格的代码(跟随弗兰克·D·露娜)。代码几乎正确地工作-网格被绘制,但不是所有的顶点。以下是网格的图像:
我正在绘制一个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);
}我的代码出了什么问题?
发布于 2016-12-27 02:38:16
解决了这个问题:更改了以下内容:
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;https://stackoverflow.com/questions/41333933
复制相似问题