在花了大约 3 天的时间试图让阴影贴图工作后,我在将阴影贴图传递给着色器时遇到了一些麻烦。
首先我初始化深度模板和资源视图,然后我绘制(我相信)到深度模板。我发现的问题是深度模板从来没有与之关联的任何数据。
Pix 在资源视图或深度模板中没有显示任何数据,我无法将深度模板的图像保存到文件中(我来自非阴影深度模板)。我可以调用 DrawSceneShadow() 并将其渲染到渲染目标并得到我想要的。
感谢您提供任何帮助。
初始化
// 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 );