Created
August 9, 2011 06:39
-
-
Save zao/1133515 to your computer and use it in GitHub Desktop.
Instanced drawing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void animated_mesh::draw(animated_mesh& model, std::vector<instance_data> const& instances, D3DXMATRIX view, D3DXMATRIX projection) | |
{ | |
auto hr = noisy_hresult(); | |
unsigned stride[] = { sizeof(vertex_type_a), sizeof(vertex_type_b), sizeof(instance_data) }; | |
unsigned offset[] = { 0, 0, 0 }; | |
CComPtr<ID3D11DeviceContext> ctx = model.ctx; | |
CComPtr<ID3D11Device> device; | |
ctx->GetDevice(&device); | |
auto instance_vb = rendering::make_vertex_buffer(device, instances); | |
auto& geom = model.geometry; | |
auto& shaders = model.shaders; | |
for (size_t i = 0; i < geom.vertex_buffers.size(); ++i) | |
{ | |
ctx->IASetVertexBuffers(i, 1, &geom.vertex_buffers[i].buffer.p, stride + i, offset + i); | |
} | |
if (geom.index_buffer) | |
{ | |
ctx->IASetIndexBuffer(geom.index_buffer, DXGI_FORMAT_R32_UINT, 0); | |
} | |
// set third buffer to the per-instance one | |
ctx->IASetVertexBuffers(2, 1, &instance_vb.buffer.p, stride + 2, offset + 2); | |
ctx->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); | |
D3D11_MAPPED_SUBRESOURCE map_result; | |
hr = ctx->Map(shaders.matrix_buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map_result); | |
auto p = static_cast<rendering::matrix_buffer_type*>(map_result.pData); | |
D3DXMatrixIdentity(&p->world); | |
D3DXMatrixTranspose(&p->view, &view); | |
D3DXMatrixTranspose(&p->projection, &projection); | |
ctx->Unmap(shaders.matrix_buffer, 0); | |
unsigned buffer_index = 0; | |
ctx->VSSetConstantBuffers(buffer_index, 1, &shaders.matrix_buffer.p); | |
ctx->IASetInputLayout(shaders.layout); | |
ctx->VSSetShader(shaders.vs, NULL, 0); | |
ctx->PSSetShader(shaders.ps, NULL, 0); | |
if (geom.index_buffer) | |
{ | |
ctx->DrawIndexedInstanced(geom.n_indices, instances.size(), 0, 0, 0); | |
} | |
else | |
{ | |
ctx->DrawInstanced(geom.n_vertices, instances.size(), 0, 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment