I am working on my first 3D game engine and I'm just getting past the "draw a rainbow cube" stage. My next step is being able to draw a ground mesh. I'd like to use multiple different vertex buffers for storing position data. I basically copied the code I used for generating the vertex and index buffers for the cube (just changed the names of the buffers/the input data). Then I changed my render function to take in a source value to tell it which buffer to set. When I first tried it, my rotating cube turned into a gigantic horribly disfigured shape. A couple little changes later, and now I get my cube to show properly, but the ground is nowhere to be seen. If someone could give me some insight as to what I'm missing in this puzzle, I'd appreciate it. I couldn't find anything that quite described my issue.
void DXDEngine::Render(UINT numIndices, XMMATRIX VPMat, XMMATRIX worldMat, UINT source) //source: 1 = squareVertBuffer, 2 = ground buffer
{
//Called each frame to present the buffer
bool validSource = false;
UINT stride = sizeof(Vertex);
UINT offset = 0;
UINT stride2 = sizeof(Vertex);
UINT offset2 = 0;
switch (source)
{
case(1):
//Set the cube vertex buffer
devcon->IASetVertexBuffers(0, 1, &squareVertBuffer, &stride, &offset);
devcon->IASetIndexBuffer(squareIndexBuffer, DXGI_FORMAT_R32_UINT, 0);
validSource = true;
break;
case(2):
//Set the vertex buffer
devcon->IASetVertexBuffers(1, 0, &groundVertBuffer, &stride2, &offset2);
devcon->IASetIndexBuffer(groundIndexBuffer, DXGI_FORMAT_R32_UINT, 0);
validSource = true;
break;
default:
validSource = false;
break;
}
if (validSource == true)
{
XMMATRIX WVPMat = worldMat * VPMat;
//Insert the transpose of the world view projection matrix into the per object structure
cbPerObj.WVP = XMMatrixTranspose(WVPMat);
//pass the per object structure to the buffer
devcon->UpdateSubresource(cbPerObjectBuffer, 0, NULL, &cbPerObj, 0, 0);
//Update the vertex shader to use the constant buffer
devcon->VSSetConstantBuffers(0, 1, &cbPerObjectBuffer);
// do 3D rendering on the back buffer here
devcon->DrawIndexed(numIndices, 0, 0);
}
}
initialization of square buffer: D3D11_BUFFER_DESC vertexBufferDesc; ZeroMemory(&vertexBufferDesc, sizeof(vertexBufferDesc));
vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
vertexBufferDesc.ByteWidth = sizeof(Vertex) * vLen;
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexBufferDesc.CPUAccessFlags = 0;
vertexBufferDesc.MiscFlags = 0;
D3D11_SUBRESOURCE_DATA vertexBufferData;
ZeroMemory(&vertexBufferData, sizeof(vertexBufferData));
vertexBufferData.pSysMem = v;
hr = dev->CreateBuffer(&vertexBufferDesc, &vertexBufferData, &squareVertBuffer);
//Set the vertex buffer
UINT stride = sizeof(Vertex);
UINT offset = 0;
devcon->IASetVertexBuffers(0, 1, &squareVertBuffer, &stride, &offset);
D3D11_BUFFER_DESC indexBufferDesc;
ZeroMemory(&indexBufferDesc, sizeof(indexBufferDesc));
indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
indexBufferDesc.ByteWidth = sizeof(DWORD) * indicesLen;
indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
indexBufferDesc.CPUAccessFlags = 0;
indexBufferDesc.MiscFlags = 0;
D3D11_SUBRESOURCE_DATA iinitData;
iinitData.pSysMem = indices;
dev->CreateBuffer(&indexBufferDesc, &iinitData, &squareIndexBuffer);
devcon->IASetIndexBuffer(squareIndexBuffer, DXGI_FORMAT_R32_UINT, 0);
and the ground vertex buffer
D3D11_BUFFER_DESC gVertexBufferDesc;
ZeroMemory(&gVertexBufferDesc, sizeof(gVertexBufferDesc));
gVertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
gVertexBufferDesc.ByteWidth = sizeof(Vertex) * grndVertLen;
gVertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
gVertexBufferDesc.CPUAccessFlags = 0;
gVertexBufferDesc.MiscFlags = 0;
D3D11_SUBRESOURCE_DATA gVertexBufferData;
ZeroMemory(&gVertexBufferData, sizeof(gVertexBufferData));
gVertexBufferData.pSysMem = grndVert;
hr = dev->CreateBuffer(&gVertexBufferDesc, &gVertexBufferData, &groundVertBuffer);
//Set the vertex buffer
UINT stride = sizeof(Vertex);
UINT offset = 0;
devcon->IASetVertexBuffers(1, 1, &groundVertBuffer, &stride, &offset);
D3D11_BUFFER_DESC gIndexBufferDesc;
ZeroMemory(&gIndexBufferDesc, sizeof(gIndexBufferDesc));
gIndexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
gIndexBufferDesc.ByteWidth = sizeof(DWORD) * indicesLen;
gIndexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
gIndexBufferDesc.CPUAccessFlags = 0;
gIndexBufferDesc.MiscFlags = 0;
D3D11_SUBRESOURCE_DATA iinitData;
iinitData.pSysMem = grndIndex;
dev->CreateBuffer(&gIndexBufferDesc, &iinitData, &groundIndexBuffer);
devcon->IASetIndexBuffer(groundIndexBuffer, DXGI_FORMAT_R32_UINT, 0);