0
\$\begingroup\$

I'm attempting to render the scene from the current view matrix onto a texture (currently just for testing purposes) but I can't for the life of me get it to work.

Currently all the texture displays is the clear colour that I set after setting the render target, instead of all the other meshes in the scene like it should display.

Graphics::GetGraphics()->GetImmediateContext()->OMSetRenderTargets(1, &targetView, Graphics::GetGraphics()->GetDepthStencilView());
    float ClearColor[4] ={0.204f, 0.255f, 0.204f, 1.0f};
    Graphics::GetGraphics()->GetImmediateContext()->ClearRenderTargetView(targetView, ClearColor);

enter image description here

I'm certain the issue I'm having is to do with the order of my draw calls but I as this is my first time EVER implementing this, I can't really see what I've done wrong since in my head it should work. This is current render function for the model that I'm applying this texture too:

Graphics::GetGraphics()->GetImmediateContext()->OMSetRenderTargets(1, &targetView, Graphics::GetGraphics()->GetDepthStencilView());
    float ClearColor[4] ={0.204f, 0.255f, 0.204f, 1.0f};
    Graphics::GetGraphics()->GetImmediateContext()->ClearRenderTargetView(targetView, ClearColor);

    ID3D11RenderTargetView* originalTargetView = Graphics::GetGraphics()->GetRenderTarget();
    Graphics::GetGraphics()->GetImmediateContext()->OMSetRenderTargets(1, &originalTargetView, Graphics::GetGraphics()->GetDepthStencilView());

    Graphics::GetGraphics()->GetImmediateContext()->IASetVertexBuffers(0, 1, &mesh->GetMeshData()->VertexBuffer, &mesh->GetMeshData()->VBStride, &mesh->GetMeshData()->VBOffset);
    Graphics::GetGraphics()->GetImmediateContext()->IASetIndexBuffer(mesh->GetMeshData()->IndexBuffer, DXGI_FORMAT_R16_UINT, 0);

    Graphics::GetGraphics()->GetImmediateContext()->VSSetShader(shader->vertexShader, nullptr, 0);
    Graphics::GetGraphics()->GetImmediateContext()->PSSetShader(shader->pixelShader, nullptr, 0);

    Graphics::GetGraphics()->GetImmediateContext()->VSSetConstantBuffers(0, 1, &shader->constantBuffer);
    Graphics::GetGraphics()->GetImmediateContext()->PSSetConstantBuffers(0, 1, &shader->constantBuffer);

    Graphics::GetGraphics()->GetImmediateContext()->PSSetShaderResources(0, 1, &texture);
    Graphics::GetGraphics()->GetImmediateContext()->PSSetSamplers(0, 1, &mesh->samplerLinear);

    shader->Update(_world);

    Graphics::GetGraphics()->GetImmediateContext()->DrawIndexed(mesh->GetMeshData()->IndexCount, 0, 0);

The targetView render target is what I use to render the scene to the texture. The original target view that I set after is the render target that outputs to the back buffer. I then do a bunch of code to render the actual mesh, apply the texture to the mesh as well as update the shader which just looks like this:

    XMMATRIX world = XMLoadFloat4x4(&_world);
    XMMATRIX view = XMLoadFloat4x4(&Graphics::GetGraphics()->GetViewMatrix());
    XMMATRIX projection = XMLoadFloat4x4(&Graphics::GetGraphics()->GetProjectionMatrix());

    ConstantBufferTV cb;
    cb.mWorld = XMMatrixTranspose(world);
    cb.mView = XMMatrixTranspose(view);
    cb.mProjection = XMMatrixTranspose(projection);

    Graphics::GetGraphics()->GetImmediateContext()->UpdateSubresource(constantBuffer, 0, nullptr, &cb, 0, 0);

Currently I've got it setup so the TV Screen that I've called it is rendered last:

void Scene::RenderScene()
{
    if (sceneLoaded)
    {
        for (int i = 0; i < gameObjects.size(); i++)
        {
            gameObjects[i]->Render();
        }
    }

    skybox->Render();
    tvScreen->Render();
}

The only other time I ever call SetRenderTargets again is before I call RenderScene but I also do this every frame (This is basically the main render function):

void Application::Draw()
{
    _pImmediateContext->OMSetRenderTargets(1, &_pRenderTargetView, depthStencilView);

    float ClearColor[4] ={0.204f, 0.255f, 0.204f, 1.0f};
    _pImmediateContext->ClearRenderTargetView(_pRenderTargetView, ClearColor);
    _pImmediateContext->ClearDepthStencilView(depthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0.0f); //Clears the Depth Buffer Each Frame

    scene->RenderScene();

    _pSwapChain->Present(0, 0);
}

From what I can tell, it seems to actually be rendering to the texture fine but it's just not picking up any models in the scene, only the clear colour so I'm almost certain it's the order I've done things.

I'd love some help on what exactly I've done wrong

\$\endgroup\$
  • \$\begingroup\$ Ok, from what I can see you might have your ordering wrong, just a little hard to understand from the code. So, what you really want to do, is render your TV Texture first into it's on texture, which has a SRV attached (and is also a rendertarget itself). It is important post rendering to your TV Texture you unbind it from the rendertarget view, then you can bind it as a shader resource for rendering your actual TV surface. You then should just render your scene as per normal (from the screenshot that's fine). If you can, turn on Debug also, you may find it will warn of bound textures. \$\endgroup\$ – ErnieDingo Jan 14 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.