private void RenderShadowMap(out XMFloat4X4 mViewProjLight, out XMFloat3 vLightDir) { var context = this.deviceResources.D3DContext; D3D11Rect[] oldRects = context.RasterizerStageGetScissorRects(); D3D11Viewport[] oldVp = context.RasterizerStageGetViewports(); D3D11Rect[] rects = new D3D11Rect[1] { new D3D11Rect(0, (int)g_fShadowMapWidth, 0, (int)g_fShadowMapHeight) }; context.RasterizerStageSetScissorRects(rects); D3D11Viewport[] vp = new D3D11Viewport[1] { new D3D11Viewport(0, 0, g_fShadowMapWidth, g_fShadowMapHeight, 0.0f, 1.0f) }; context.RasterizerStageSetViewports(vp); // Set our scene render target & keep original depth buffer D3D11RenderTargetView[] pRTVs = new D3D11RenderTargetView[2]; context.OutputMergerSetRenderTargets(pRTVs, this.g_pDepthStencilTextureDSV); // Clear the render target context.ClearDepthStencilView(this.g_pDepthStencilTextureDSV, D3D11ClearOptions.Depth | D3D11ClearOptions.Stencil, 1.0f, 0); // Get the projection & view matrix from the camera class XMFloat3 up = new XMFloat3(0, 1, 0); XMFloat4 vLight = new XMFloat4(0.0f, 0.0f, 0.0f, 1.0f); XMFloat4 vLightLookAt = new XMFloat4(0.0f, -0.5f, 1.0f, 0.0f); vLightLookAt = vLight.ToVector() + vLightLookAt.ToVector(); vLight = XMVector4.Transform(vLight, this.LightWorldMatrix); vLightLookAt = XMVector4.Transform(vLightLookAt, this.LightWorldMatrix); vLightDir = XMVector.Subtract(vLightLookAt, vLight); XMMatrix mProj = XMMatrix.OrthographicOffCenterLH(-8.5f, 9, -15, 11, -20, 20); XMMatrix mView = XMMatrix.LookAtLH(vLight, vLightLookAt, up); mViewProjLight = mView * mProj; // Setup the constant buffer for the scene vertex shader ConstantBufferConstants pConstants = new ConstantBufferConstants { WorldViewProjection = mViewProjLight.ToMatrix().Transpose(), WorldViewProjLight = mViewProjLight.ToMatrix().Transpose(), ShadowMapDimensions = new XMFloat4( g_fShadowMapWidth, g_fShadowMapHeight, 1.0f / g_fShadowMapWidth, 1.0f / g_fShadowMapHeight), LightDir = new XMFloat4(vLightDir.X, vLightDir.Y, vLightDir.Z, 0.0f), SunWidth = this.SunWidth }; context.UpdateSubresource(this.g_pcbConstants, 0, null, pConstants, 0, 0); context.VertexShaderSetConstantBuffers(g_iConstantsConstantBufferBind, new[] { this.g_pcbConstants }); context.PixelShaderSetConstantBuffers(g_iConstantsConstantBufferBind, new[] { this.g_pcbConstants }); // Set the shaders context.VertexShaderSetShader(this.g_pShadowMapVS, null); context.PixelShaderSetShader(null, null); // Set the vertex buffer format context.InputAssemblerSetInputLayout(this.g_pSceneVertexLayout); // Render the scene this.g_Poles.Render(0, -1, -1); // reset the old viewport etc. context.RasterizerStageSetScissorRects(oldRects); context.RasterizerStageSetViewports(oldVp); }