1

我在使用 directx 11 渲染时遇到问题 - 如果我尝试渲染多个模型,我只会看到具有奇数索引的模型。所有使用偶数索引渲染的模型都是不可见的。

我的代码基于 rastertek 教程:

m_dx->BeginScene(0.0f, 0.0f, 0.0f, 1.0f);
{
    m_camera->Render();

    XMMATRIX view;
    m_camera->GetViewMatrix(view);

    XMMATRIX world;
    m_dx->GetWorldMatrix(world);

    XMMATRIX projection;
    m_dx->GetProjectionMatrix(projection);

    XMMATRIX ortho;
    m_dx->GetOrthoMatrix(ortho);

    world = XMMatrixTranslation(-2, 0, -4);
    m_model->Render(m_dx->GetDeviceContext());
    m_texture_shader->Render(m_dx->GetDeviceContext(), m_model->GetIndicesCount(), world, view, projection,
        m_model->GetTexture());

    world = XMMatrixTranslation(2, 0, -2);
    m_model->Render(m_dx->GetDeviceContext());
    m_texture_shader->Render(m_dx->GetDeviceContext(), m_model->GetIndicesCount(), world, view, projection,
        m_model->GetTexture());

    world = XMMatrixTranslation(0, 0, -3);
    m_model->Render(m_dx->GetDeviceContext());
    m_texture_shader->Render(m_dx->GetDeviceContext(), m_model->GetIndicesCount(), world, view, projection,
        m_model->GetTexture());

}
m_dx->EndScene();

模型渲染方法

UINT stride, offset;

stride = sizeof(VertexPosTextureNormal);
offset = 0;

device_context->IASetVertexBuffers(0, 1, &m_vertex_buffer, &stride, &offset);
device_context->IASetIndexBuffer(m_index_buffer, DXGI_FORMAT_R32_UINT, 0);
device_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

着色器渲染方法:

world = XMMatrixTranspose(world);
view = XMMatrixTranspose(view);
projection = XMMatrixTranspose(projection);

D3D11_MAPPED_SUBRESOURCE mapped_subres;
RETURN_FALSE_IF_FAILED(context->Map(m_matrix_buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped_subres));
MatrixBuffer* data = (MatrixBuffer*)mapped_subres.pData;
data->world = world;
data->view = view;
data->projection = projection;
context->Unmap(m_matrix_buffer, 0);
context->VSSetConstantBuffers(0, 1, &m_matrix_buffer);
context->PSSetShaderResources(0, 1, &texture);

    // render
    context->IASetInputLayout(m_layout);

context->VSSetShader(m_vertex_shader, NULL, 0);
context->PSSetShader(m_pixel_shader, NULL, 0);
context->PSSetSamplers(0, 1, &m_sampler_state);
context->DrawIndexed(indices, 0, 0);

这可能是什么原因?

谢谢你。

4

1 回答 1

0

这段代码 -

world = XMMatrixTranspose(world);
view = XMMatrixTranspose(view);
projection = XMMatrixTranspose(projection); 

每次调用它时都会转置相同的矩阵,因此它们仅具有正确的值交替时间。每次在调用代码中都会重置世界矩阵,但视图和项目矩阵交替出现错误。

于 2012-11-06T09:23:47.770 回答