setRenderTarget()用法总结

网上查了很多,很多是rtt,,setRenderTarget()的不多,借鉴shadowmap例子,

 

初始化

 

    //创建shadowmap纹理
    hr = pd3dDevice->CreateTexture( m_TextureSize, m_TextureSize, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pRTTTexture, NULL);
    //创建深度模板
    hr = pd3dDevice->CreateDepthStencilSurface(m_TextureSize, m_TextureSize, D3DFMT_D24X8, D3DMULTISAMPLE_NONE, 0, TRUE, &m_pRTTSurface, NULL);

每帧(这里写在了一个beginscene()和endScene()之间,以提高灵活性,很多都是写了两个beginScene()


    V(pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 45, 50, 170), 1.0f, 0));
    pd3dDevice->BeginScene();

    LPDIRECT3DSURFACE9 backgroundSurface = NULL;
    pd3dDevice->GetRenderTarget(0, &backgroundSurface);
    LPDIRECT3DSURFACE9 pOldDS = NULL;
    pd3dDevice->GetDepthStencilSurface(&pOldDS);
    LPDIRECT3DSURFACE9 RTTSurface = NULL;
    g_OccluderObject.ReturnTexture()->GetSurfaceLevel(0, &RTTSurface);

    pd3dDevice->SetRenderTarget(0, RTTSurface);
    pd3dDevice->SetDepthStencilSurface(g_OccluderObject.getRTTSurface());
    V(pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 45, 50, 170), 1.0f, 0));
    pd3dDevice->BeginScene();
    g_OccluderObject.RenderToTexture(pd3dDevice, &g_matView, &g_matProject,&g_matViewRTT, &g_matProjectRTT);    
    pd3dDevice->EndScene();
    //恢复以前的背景页面
    pd3dDevice->SetRenderTarget(0, backgroundSurface);    
    pd3dDevice->SetDepthStencilSurface(pOldDS);
    //渲染物体
    g_OccluderObject.renderObjectVector(pd3dDevice);
    g_TextureDebug.OnFrameRender(pd3dDevice, g_OccluderObject.ReturnTexture(), 0);
    pd3dDevice->EndScene();    
    
    backgroundSurface->Release();
    backgroundSurface = NULL;
    RTTSurface->Release();
    RTTSurface = NULL;
    pOldDS->Release();
    pOldDS = NULL;

你可能感兴趣的:(遇到的技术问题)