0
我是新的DirectX編程... 我寫了一個代碼繪製網格(遵循Frank D. Luna)。代碼幾乎正確地工作 - 網格被繪製,但不是所有的頂點。這裏是網格的圖像: DirectX 11 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);
}
什麼我的代碼有問題嗎?
問題就迎刃而解了。 – Tesla
請注意,您應該查看[此帖子](https://blogs.msdn.microsoft.com/chuckw/2014/04/07/book-recommendations/)以及[DirectX Tool Kit](https: //github.com/Microsoft/DirectXTK/wiki/Getting-Started)。弗蘭克的書很有用,但在談論現在的[遺留DirectX SDK](https://msdn.microsoft.com/en-us/library/windows/desktop/ee663275.aspx)時有點過時。 –
是的,這是我學習DirectX時遇到的主要問題,本書中使用的大多數技術現在已過時。 – Tesla