我正在尝试在彼此之上绘制一些2D方块。
第一个方块是800 x 600像素,以0,0为中心 第二组正方形每个都是30 x 30像素,在第一个2D正方形的顶部移动
对于第一个方块,我试图使用简单的索引几何来渲染它。
我已经尝试过单独渲染,但它确实有效。但是,当我尝试同时渲染两者时,即使我先渲染第一个正方形,第二个正方形的实例似乎会因为移动800 x 600的正方形而与实例缓冲区中的第一个实例相匹配正方形,即使我从板切换到每个正方形时都传递新的顶点缓冲区。以下是我的渲染代码:
void Render()
{
FLOAT color[] = {0.3f, 0.4f, 0.5f, 1.0f};
m_d3dImmediateContext->ClearRenderTargetView(m_d3dRenderTargetView, color);
m_d3dImmediateContext->ClearDepthStencilView(m_d3dDepthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
m_d3dImmediateContext->IASetInputLayout(m_d3dInputLayout);
m_d3dImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
UINT stride = sizeof(Vertex);
UINT offset = 0;
m_d3dImmediateContext->IASetVertexBuffers(0, 1, &m_d3dBoardVBuffer, &stride, &offset);
m_d3dImmediateContext->IASetIndexBuffer(m_d3dIBuffer, DXGI_FORMAT_R32_UINT, 0);
m_d3dImmediateContext->VSSetShader(m_d3dVShader, NULL, 0);
m_d3dImmediateContext->PSSetShader(m_d3dPShader, NULL, 0);
m_d3dImmediateContext->VSSetConstantBuffers(0, 1, &m_d3dCBPerObject);
m_d3dImmediateContext->PSSetSamplers(0, 1, &m_samLinear);
m_d3dImmediateContext->PSSetShaderResources(0, 1, &m_d3dBoardSRV);
XMMATRIX worldViewProj = m_world * m_view * m_proj;
XMStoreFloat4x4(&m_cbPerObject.WorldViewProj, worldViewProj);
D3D11_MAPPED_SUBRESOURCE mappedResource;
m_d3dImmediateContext->Map(m_d3dCBPerObject, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
CopyMemory(mappedResource.pData, &m_cbPerObject, sizeof(struct CBPerObject));
m_d3dImmediateContext->Unmap(m_d3dCBPerObject, 0);
m_d3dImmediateContext->DrawIndexed(m_indexCount, 0, 0);
UINT blockStride[2] = {sizeof(Vertex), sizeof(Instance)};
UINT blockOffset[2] = {0, 0};
ID3D11Buffer *vBuffers[2] = {m_d3dBlockVBuffer, m_d3dBlockInstVBuffer};
m_d3dImmediateContext->IASetVertexBuffers(0, 2, vBuffers, blockStride, blockOffset);
m_d3dImmediateContext->IASetIndexBuffer(m_d3dIBuffer, DXGI_FORMAT_R32_UINT, 0);
m_d3dImmediateContext->VSSetShader(m_d3dVShader, NULL, 0);
m_d3dImmediateContext->PSSetShader(m_d3dPShader, NULL, 0);
m_d3dImmediateContext->VSSetConstantBuffers(0, 1, &m_d3dCBPerObject);
m_d3dImmediateContext->PSSetSamplers(0, 1, &m_samLinear);
m_d3dImmediateContext->PSSetShaderResources(0, 1, &m_d3dBlockSRV);
worldViewProj = m_world * m_view * m_proj;
XMStoreFloat4x4(&m_cbPerObject.WorldViewProj, worldViewProj);
D3D11_MAPPED_SUBRESOURCE mappedResource2;
m_d3dImmediateContext->Map(m_d3dCBPerObject, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource2);
CopyMemory(mappedResource2.pData, &m_cbPerObject, sizeof(struct CBPerObject));
m_d3dImmediateContext->Unmap(m_d3dCBPerObject, 0);
m_d3dImmediateContext->DrawIndexedInstanced(m_indexCount, 4, 0, 0, 0);
m_d3dSwapChain->Present(0, 0);
}
我做的事情根本不正确吗?该板为800 x 600平方,块为30 x 30平方。在电路板上,我想基本上渲染多个方块。
答案 0 :(得分:0)
您没有为第二个顶点缓冲区设置输入布局。您在渲染m_d3dBoardVBuffer之前设置它,但在渲染vBuffers之前不要设置唯一的。