遇到RenderTarget設置另外一張Texture後,把它拿出來卻位置跑掉的問題

 

我視窗大小設定640*480

然後新增一張空白的Texture 256*256

設置RenderTarget要Render在它上面

然後把Texture拿出來看,測試看有無成功

發現位置卻跑掉了

後來原本以為是要swapChain ResizeBuffer

於是在網路上找到了這段code

    if (swapChain_)
    {
        d3dContext_->OMSetRenderTargets(0, 0, 0);

        // Release all outstanding references to the swap chain's buffers.
        //g_pRenderTargetView->Release();

        HRESULT hr;
        // Preserve the existing buffer count and format.
        // Automatically choose the width and height to match the client rect for HWNDs.
        hr = swapChain_->ResizeBuffers(0, 0, 0, DXGI_FORMAT_UNKNOWN, 0);
        
        // Perform error handling here!

        // Get buffer and create a render-target-view.
        ID3D11Texture2D* pBuffer=0;
        
        // Perform error handling here!
        hr = swapChain_->GetBuffer(0, __uuidof( ID3D11Texture2D),
            (void**) &pBuffer );
        hr = d3dDevice_->CreateRenderTargetView(pBuffer, NULL,
            &renderTargetView);
        pBuffer->Release();
        
        // Perform error handling here!
        

        d3dContext_->OMSetRenderTargets(1, &renderTargetView, NULL );

        // Set up the viewport.
        
        D3D11_VIEWPORT vp;
        vp.Width = w;
        vp.Height = h;
        vp.MinDepth = 0.0f;
        vp.MaxDepth = 1.0f;
        vp.TopLeftX = 0;
        vp.TopLeftY = 0;
        d3dContext_->RSSetViewports( 1, &vp );
        
    }

 

後來發現不用這麼麻煩

只要Set up the viewport就好了

        D3D11_VIEWPORT vp;
        vp.Width = w;
        vp.Height = h;
        vp.MinDepth = 0.0f;
        vp.MaxDepth = 1.0f;
        vp.TopLeftX = 0;
        vp.TopLeftY = 0;
        d3dContext_->RSSetViewports( 1, &vp );

照我的問題,在準備要Render在256*256上面時

先更改viewport width and height 256*256

然後記得要Render回到原本的backBuffer要改回視窗的大小640*480

這樣就可以解決位置跑掉的問題了

arrow
arrow
    全站熱搜

    台灣李宏感 發表在 痞客邦 留言(0) 人氣()