Ejemplo n.º 1
0
        public void Render()
        {
            var context = this.deviceResources.D3DContext;

            D3D11RenderTargetView[]   pRTV = new D3D11RenderTargetView[2];
            D3D11ShaderResourceView[] pSRV = new D3D11ShaderResourceView[8];

            // Array of our samplers
            D3D11SamplerState[] ppSamplerStates = new D3D11SamplerState[3] {
                this.g_pSamplePoint, this.g_pSampleLinear, this.g_pSamplePointCmp
            };
            context.PixelShaderSetSamplers(0, ppSamplerStates);

            // Store off original render target, this is the back buffer of the swap chain
            D3D11RenderTargetView pOrigRTV = this.deviceResources.D3DRenderTargetView;
            D3D11DepthStencilView pOrigDSV = this.deviceResources.D3DDepthStencilView;

            // Clear the render target
            float[] ClearColor = { 0.0f, 0.25f, 0.25f, 1.0f };
            context.ClearRenderTargetView(this.deviceResources.D3DRenderTargetView, ClearColor);
            context.ClearDepthStencilView(this.deviceResources.D3DDepthStencilView, D3D11ClearOptions.Depth | D3D11ClearOptions.Stencil, 1.0f, 0);

            // disable color writes
            context.OutputMergerSetBlendState(this.g_pBlendStateColorWritesOff, null, 0xffffffff);

            this.RenderShadowMap(out XMFloat4X4 mViewProjLight, out XMFloat3 vLightDir);

            // enable color writes
            context.OutputMergerSetBlendState(this.g_pBlendStateNoBlend, null, 0xffffffff);

            // Get the projection & view matrix from the camera class
            XMMatrix mView = this.ViewMatrix;
            XMMatrix mProj = this.ProjectionMatrix;
            XMMatrix mWorldViewProjection = mView * mProj;

            // Setup the constant buffer for the scene vertex shader
            ConstantBufferConstants pConstants = new ConstantBufferConstants
            {
                WorldViewProjection = mWorldViewProjection.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_pSceneVS, null);
            context.PixelShaderSetShader(this.g_pScenePS, null);

            // Set the vertex buffer format
            context.InputAssemblerSetInputLayout(this.g_pSceneVertexLayout);

            // Rebind to original back buffer and depth buffer
            pRTV[0] = pOrigRTV;
            context.OutputMergerSetRenderTargets(pRTV, pOrigDSV);

            // set the shadow map
            context.PixelShaderSetShaderResources(1, new[] { this.g_pDepthTextureSRV });

            // Render the scene
            this.g_SceneMesh.Render(0, -1, -1);
            this.g_Poles.Render(0, -1, -1);

            // restore resources
            context.PixelShaderSetShaderResources(0, pSRV);
        }
Ejemplo n.º 2
0
        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);
        }