Depth Stencil和Shadow资源为空

时间:2013-02-21 01:57:11

标签: directx-10 stencil-buffer shadow-mapping

在花费大约3天试图让阴影贴图工作之后,我在将阴影贴图传递到着色器时遇到了一些麻烦。

首先我初始化深度模板和资源视图,然后我绘制(我相信)深度模板。我发现的问题是深度模板永远不会有任何与之相关的数据。

Pix在资源视图或深度模板中没有显示数据,我无法将深度模板的图像保存到文件中(我来自非阴影深度模板)。我可以调用DrawSceneShadow()并将其渲染到rendertarget并得到我想要的东西。

感谢您的任何帮助。

初​​始化

// Create depth stencil texture
D3D10_TEXTURE2D_DESC descDepth;
descDepth.Width = width;
descDepth.Height = height;
descDepth.MipLevels = 1;
descDepth.ArraySize = 1;
descDepth.Format = DXGI_FORMAT_R32_TYPELESS;
descDepth.SampleDesc.Count = 1;
descDepth.SampleDesc.Quality = 0;
descDepth.Usage = D3D10_USAGE_DEFAULT;
descDepth.BindFlags = D3D10_BIND_DEPTH_STENCIL;
descDepth.CPUAccessFlags = 0;
descDepth.MiscFlags = 0;
hr = g_pd3dDevice->CreateTexture2D( &descDepth, NULL, &g_pDepthStencil );

// Create the depth stencil view
D3D10_DEPTH_STENCIL_VIEW_DESC descDSV;
descDSV.Format = DXGI_FORMAT_D32_FLOAT;
descDSV.ViewDimension = D3D10_DSV_DIMENSION_TEXTURE2D;
descDSV.Texture2D.MipSlice = 0;
hr = g_pd3dDevice->CreateDepthStencilView( g_pDepthStencil, &descDSV, &g_pDepthStencilView );

/////////////////////////////

//Shadow Mapping
//create shadow map texture desc
descDepth.Width = width;
descDepth.Height = height;
descDepth.Format = DXGI_FORMAT_R32_TYPELESS;
descDepth.BindFlags = D3D10_BIND_DEPTH_STENCIL | D3D10_BIND_SHADER_RESOURCE;

//create shader resource view desc
D3D10_SHADER_RESOURCE_VIEW_DESC srvDesc;
srvDesc.Format = DXGI_FORMAT_R32_FLOAT;
srvDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MipLevels = descDepth.MipLevels;
srvDesc.Texture2D.MostDetailedMip = 0;

渲染场景

//Create shadow map
//***************************************************************************
//set render targets
//set render targets and viewport
g_pd3dDevice->OMSetRenderTargets(0, 0, pShadowMapDepthView);
g_pd3dDevice->ClearDepthStencilView( pShadowMapDepthView, D3D10_CLEAR_DEPTH, 1.0f, 0 );

moveCam( lightPos.x, lightPos.y, lightPos.z );
DrawSceneShadow();


//Render final scene
//***************************************************************************

//set render targets
g_pd3dDevice->OMSetRenderTargets(1, &g_pRenderTargetView, g_pDepthStencilView);    
g_pd3dDevice->RSSetViewports(1, &vp);
g_pd3dDevice->ClearRenderTargetView( g_pRenderTargetView, D3DXCOLOR(0.6f,0.6f,0.6f,0) );
g_pd3dDevice->ClearDepthStencilView( g_pDepthStencilView, D3D10_CLEAR_DEPTH, 1.0f, 0 );

//bind shadow map texture
g_pEffect->GetVariableByName("shadowMap")->AsShaderResource()->SetResource( pShadowMapSRView );
ScreenGrab();
resetCam();
lightMatrix();
DrawScene();

//unbind shadow map as SRV and call apply on scene rendering technique
g_pEffect->GetVariableByName("shadowMap")->AsShaderResource()->SetResource( 0 );
g_pRenderShadowMapTechnique->GetPassByIndex(0)->Apply( 0 );

0 个答案:

没有答案