コード例 #1
0
        public void DrawToShadowMap(DeviceContext dc, ShadowMap sMap, Matrix viewProj)
        {
            sMap.BindDsvAndSetNullRenderTarget(dc);

            dc.InputAssembler.PrimitiveTopology = PrimitiveTopology.PatchListWith4ControlPoints;
            dc.InputAssembler.InputLayout       = InputLayouts.TerrainCP;

            var       stride = TerrainCP.Stride;
            const int offset = 0;

            dc.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(_quadPatchVB, stride, offset));
            dc.InputAssembler.SetIndexBuffer(_quadPatchIB, Format.R16_UInt, 0);


            var frustum = Frustum.FromViewProj(viewProj);
            var planes  = frustum.Planes;

            Effects.TerrainFX.SetViewProj(viewProj);
            Effects.TerrainFX.SetEyePosW(new Vector3(viewProj.M41, viewProj.M42, viewProj.M43));
            Effects.TerrainFX.SetMinDist(MinDist);
            Effects.TerrainFX.SetMaxDist(MaxDist);
            Effects.TerrainFX.SetMinTess(MinTess);
            Effects.TerrainFX.SetMaxTess(MaxTess);
            Effects.TerrainFX.SetWorldCellSpace(_terrain.Info.CellSpacing);
            Effects.TerrainFX.SetWorldFrustumPlanes(planes);
            Effects.TerrainFX.SetHeightMap(_heightMapSRV);


            var tech = Effects.TerrainFX.TessBuildShadowMapTech;

            for (var p = 0; p < tech.Description.PassCount; p++)
            {
                var pass = tech.GetPassByIndex(p);
                pass.Apply(dc);
                dc.DrawIndexed(_numPatchQuadFaces * 4, 0, 0);
            }
            dc.HullShader.Set(null);
            dc.DomainShader.Set(null);
        }
コード例 #2
0
ファイル: ShadowsDemo.cs プロジェクト: mrommel/dx11
        public override void DrawScene()
        {
            Effects.BasicFX.SetShadowMap(null);
            Effects.NormalMapFX.SetShadowMap(null);

            _sMap.BindDsvAndSetNullRenderTarget(ImmediateContext);

            DrawSceneToShadowMap();

            ImmediateContext.Rasterizer.State = null;

            ImmediateContext.OutputMerger.SetTargets(DepthStencilView, RenderTargetView);
            ImmediateContext.Rasterizer.SetViewports(Viewport);

            ImmediateContext.ClearRenderTargetView(RenderTargetView, Color.Silver);
            ImmediateContext.ClearDepthStencilView(DepthStencilView, DepthStencilClearFlags.Depth | DepthStencilClearFlags.Stencil, 1.0f, 0);

            var viewProj = _camera.ViewProj;
            var view     = _camera.View;
            var proj     = _camera.Proj;

            Effects.BasicFX.SetDirLights(_dirLights);
            Effects.BasicFX.SetEyePosW(_camera.Position);
            Effects.BasicFX.SetCubeMap(_sky.CubeMapSRV);
            Effects.BasicFX.SetShadowMap(_sMap.DepthMapSRV);

            Effects.NormalMapFX.SetDirLights(_dirLights);
            Effects.NormalMapFX.SetEyePosW(_camera.Position);
            Effects.NormalMapFX.SetCubeMap(_sky.CubeMapSRV);
            Effects.NormalMapFX.SetShadowMap(_sMap.DepthMapSRV);

            var activeTech       = Effects.NormalMapFX.Light3TexTech;
            var activeSphereTech = Effects.BasicFX.Light3ReflectTech;
            var activeSkullTech  = Effects.BasicFX.Light3ReflectTech;

            ImmediateContext.InputAssembler.InputLayout = InputLayouts.PosNormalTexTan;

            if (Util.IsKeyDown(Keys.W))
            {
                ImmediateContext.Rasterizer.State = RenderStates.WireframeRS;
            }
            for (var p = 0; p < activeTech.Description.PassCount; p++)
            {
                // draw grid
                var pass = activeTech.GetPassByIndex(p);
                _grid.ShadowTransform = _shadowTransform;
                _grid.Draw(ImmediateContext, pass, view, proj);
                // draw box
                _box.ShadowTransform = _shadowTransform;
                _box.Draw(ImmediateContext, pass, view, proj);

                // draw columns
                foreach (var cylinder in _cylinders)
                {
                    cylinder.ShadowTransform = _shadowTransform;
                    cylinder.Draw(ImmediateContext, pass, view, proj);
                }
            }
            ImmediateContext.HullShader.Set(null);
            ImmediateContext.DomainShader.Set(null);
            ImmediateContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;

            for (var p = 0; p < activeSphereTech.Description.PassCount; p++)
            {
                var pass = activeSphereTech.GetPassByIndex(p);

                foreach (var sphere in _spheres)
                {
                    sphere.ShadowTransform = _shadowTransform;
                    sphere.Draw(ImmediateContext, pass, view, proj, RenderMode.Basic);
                }
            }
            var       stride = Basic32.Stride;
            const int offset = 0;

            ImmediateContext.Rasterizer.State = null;

            ImmediateContext.InputAssembler.InputLayout = InputLayouts.Basic32;
            ImmediateContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(_skullVB, stride, offset));
            ImmediateContext.InputAssembler.SetIndexBuffer(_skullIB, Format.R32_UInt, 0);

            for (var p = 0; p < activeSkullTech.Description.PassCount; p++)
            {
                var world = _skullWorld;
                var wit   = MathF.InverseTranspose(world);
                var wvp   = world * viewProj;

                Effects.BasicFX.SetWorld(world);
                Effects.BasicFX.SetWorldInvTranspose(wit);
                Effects.BasicFX.SetWorldViewProj(wvp);
                Effects.BasicFX.SetShadowTransform(world * _shadowTransform);
                Effects.BasicFX.SetMaterial(_skullMat);

                activeSkullTech.GetPassByIndex(p).Apply(ImmediateContext);
                ImmediateContext.DrawIndexed(_skullIndexCount, 0, 0);
            }
            DrawScreenQuad();

            _sky.Draw(ImmediateContext, _camera);

            ImmediateContext.Rasterizer.State = null;
            ImmediateContext.OutputMerger.DepthStencilState     = null;
            ImmediateContext.OutputMerger.DepthStencilReference = 0;

            SwapChain.Present(0, PresentFlags.None);
        }
コード例 #3
0
ファイル: Terrain.cs プロジェクト: jackinf/dx11
        public void DrawToShadowMap(DeviceContext dc, ShadowMap sMap, Matrix viewProj)
        {
            sMap.BindDsvAndSetNullRenderTarget(dc);

            dc.InputAssembler.PrimitiveTopology = PrimitiveTopology.PatchListWith4ControlPoints;
            dc.InputAssembler.InputLayout = InputLayouts.TerrainCP;

            var stride = TerrainCP.Stride;
            const int offset = 0;

            dc.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(_quadPatchVB, stride, offset));
            dc.InputAssembler.SetIndexBuffer(_quadPatchIB, Format.R16_UInt, 0);

            var frustum = Frustum.FromViewProj(viewProj);
            var planes = frustum.Planes;

            Effects.TerrainFX.SetViewProj(viewProj);
            Effects.TerrainFX.SetEyePosW(new Vector3(viewProj.M41, viewProj.M42, viewProj.M43));
            Effects.TerrainFX.SetMinDist(MinDist);
            Effects.TerrainFX.SetMaxDist(MaxDist);
            Effects.TerrainFX.SetMinTess(MinTess);
            Effects.TerrainFX.SetMaxTess(MaxTess);
            Effects.TerrainFX.SetWorldCellSpace(Info.CellSpacing);
            Effects.TerrainFX.SetWorldFrustumPlanes(planes);
            Effects.TerrainFX.SetHeightMap(_heightMapSRV);

            var tech = Effects.TerrainFX.TessBuildShadowMapTech;
            for (int p = 0; p < tech.Description.PassCount; p++) {
                var pass = tech.GetPassByIndex(p);
                pass.Apply(dc);
                dc.DrawIndexed(_numPatchQuadFaces * 4, 0, 0);
            }
            dc.HullShader.Set(null);
            dc.DomainShader.Set(null);
        }