コード例 #1
0
ファイル: Minimap.cs プロジェクト: jackinf/dx11
        public Minimap(Device device, DeviceContext dc, int minimapWidth, int minimapHeight, Terrain terrain, CameraBase viewCam)
        {
            _dc = dc;

            _minimapViewport = new Viewport(0, 0, minimapWidth, minimapHeight);

            CreateMinimapTextureViews(device, minimapWidth, minimapHeight);

            _terrain = terrain;

            SetupOrthoCamera();
            _viewCam = viewCam;

            // frustum vb will contain four corners of view frustum, with first vertex repeated as the last
            var vbd = new BufferDescription(
                VertexPC.Stride * 5,
                ResourceUsage.Dynamic,
                BindFlags.VertexBuffer,
                CpuAccessFlags.Write,
                ResourceOptionFlags.None,
                0
            );
            _frustumVB = new Buffer(device, vbd);

            _edgePlanes = new[] {
            new Plane(1, 0, 0, -_terrain.Width / 2),
            new Plane(-1, 0, 0, _terrain.Width / 2),
            new Plane(0, 1, 0, -_terrain.Depth / 2),
            new Plane(0, -1, 0, _terrain.Depth / 2)
            };

            ScreenPosition = new Vector2(0.25f, 0.75f);
            Size = new Vector2(0.25f, 0.25f);
        }
コード例 #2
0
ファイル: ParticleSystem.cs プロジェクト: amitprakash07/dx11
        public void Draw(DeviceContext dc, CameraBase camera) {
            var vp = camera.ViewProj;

            // set shader variables
            _fx.SetViewProj(vp);
            _fx.SetGameTime(_gameTime);
            _fx.SetTimeStep(_timeStep);
            _fx.SetEyePosW(EyePosW);
            _fx.SetEmitPosW(EmitPosW);
            _fx.SetEmitDirW(EmitDirW);
            _fx.SetTexArray(_texArraySRV);
            _fx.SetRandomTex(_randomTexSRV);

            dc.InputAssembler.InputLayout = InputLayouts.Particle;
            dc.InputAssembler.PrimitiveTopology = PrimitiveTopology.PointList;

            var stride = Particle.Stride;
            const int offset = 0;
            
            // bind the input vertex buffer for the stream-out technique
            // use the _initVB when _firstRun = true
            dc.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(_firstRun ? _initVB : _drawVB, stride, offset));
            // bind the stream-out vertex buffer
            dc.StreamOutput.SetTargets(new StreamOutputBufferBinding(_streamOutVB, offset));

            // draw the particles using the stream-out technique, which will update the particles positions
            // and output the resulting particles to the stream-out buffer
            var techDesc = _fx.StreamOutTech.Description;
            for (int p = 0; p < techDesc.PassCount; p++) {
                _fx.StreamOutTech.GetPassByIndex(p).Apply(dc);
                if (_firstRun) {
                    dc.Draw(1, 0);
                    _firstRun = false;
                } else {
                    // the _drawVB buffer was populated by the Stream-out technique, so we don't
                    // know how many vertices are contained within it.  Direct3D keeps track of this
                    // internally, however, and we can use DrawAuto to draw everything in the buffer.
                    dc.DrawAuto();
                }
            }
            // Disable stream-out
            dc.StreamOutput.SetTargets(null);

            // ping-pong the stream-out and draw buffers, since we will now want to draw the vertices
            // populated into the buffer that was bound to stream-out
            var temp = _drawVB;
            _drawVB = _streamOutVB;
            _streamOutVB = temp;

            // draw the particles using the draw technique that will transform the points to lines/quads
            dc.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(_drawVB, stride, offset));
            techDesc = _fx.DrawTech.Description;
            for (var p = 0; p < techDesc.PassCount; p++) {
                _fx.DrawTech.GetPassByIndex(p).Apply(dc);
                dc.DrawAuto();
            }
        }
コード例 #3
0
ファイル: Sky.cs プロジェクト: amitprakash07/dx11
        public void Draw(DeviceContext dc, CameraBase camera) {
            var eyePos = camera.Position;
            var t = Matrix.Translation(eyePos);
            var wvp = t * camera.ViewProj;

            Effects.SkyFX.SetWorldViewProj(wvp);
            Effects.SkyFX.SetCubeMap(_cubeMapSRV);

            var stride = Marshal.SizeOf(typeof(Vector3));
            const int Offset = 0;
            dc.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(_vb, stride, Offset));
            dc.InputAssembler.SetIndexBuffer(_ib, Format.R32_UInt, 0);
            dc.InputAssembler.InputLayout = InputLayouts.Pos;
            dc.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
            var tech = Effects.SkyFX.SkyTech;
            for (var p = 0; p < tech.Description.PassCount; p++) {
                var pass = tech.GetPassByIndex(p);
                pass.Apply(dc);
                dc.DrawIndexed(_indexCount, 0, 0);
            }
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: remy22/dx11
        private void DrawScene(CameraBase camera, bool drawCenterSphere)
        {
            ImmediateContext.InputAssembler.InputLayout = InputLayouts.Basic32;
            ImmediateContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;

            Matrix view = camera.View;
            Matrix proj = camera.Proj;
            Matrix viewProj = camera.ViewProj;
            Effects.BasicFX.SetEyePosW(camera.Position);
            Effects.BasicFX.SetDirLights(_dirLights);

            var activeTexTech = Effects.BasicFX.Light1TexTech;
            var activeSkullTech = Effects.BasicFX.Light1ReflectTech;
            var activeReflectTech = Effects.BasicFX.Light1TexReflectTech;
            switch (_lightCount) {
                case 1:
                    activeTexTech = Effects.BasicFX.Light1TexTech;
                    activeSkullTech = Effects.BasicFX.Light1ReflectTech;
                    activeReflectTech = Effects.BasicFX.Light1TexReflectTech;
                    break;
                case 2:
                    activeTexTech = Effects.BasicFX.Light2TexTech;
                    activeSkullTech = Effects.BasicFX.Light2ReflectTech;
                    activeReflectTech = Effects.BasicFX.Light2TexReflectTech;
                    break;
                case 3:
                    activeTexTech = Effects.BasicFX.Light3TexTech;
                    activeSkullTech = Effects.BasicFX.Light3ReflectTech;
                    activeReflectTech = Effects.BasicFX.Light3TexReflectTech;
                    break;
            }
            for (int p = 0; p < activeSkullTech.Description.PassCount; p++) {
                var pass = activeSkullTech.GetPassByIndex(p);
                ImmediateContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(_skullVB, Basic32.Stride, 0));
                ImmediateContext.InputAssembler.SetIndexBuffer(_skullIB, Format.R32_UInt, 0);

                var world = _skullWorld;
                var worldInvTranspose = MathF.InverseTranspose(world);
                var wvp = world * viewProj;
                Effects.BasicFX.SetWorld(world);
                Effects.BasicFX.SetWorldInvTranspose(worldInvTranspose);
                Effects.BasicFX.SetTexTransform(Matrix.Identity);
                Effects.BasicFX.SetWorldViewProj(wvp);
                Effects.BasicFX.SetMaterial(_skullMat);
                pass.Apply(ImmediateContext);
                ImmediateContext.DrawIndexed(_skullIndexCount, 0, 0);
            }
            // draw grid, box, cylinders without reflection
            for (var p = 0; p < activeTexTech.Description.PassCount; p++) {
                var pass = activeTexTech.GetPassByIndex(p);
                ImmediateContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(_shapesVB, Basic32.Stride, 0));
                ImmediateContext.InputAssembler.SetIndexBuffer(_shapesIB, Format.R32_UInt, 0);

                var world = _gridWorld;
                var worldInvTranspose = MathF.InverseTranspose(world);
                var wvp = world * view * proj;
                Effects.BasicFX.SetWorld(world);
                Effects.BasicFX.SetWorldInvTranspose(worldInvTranspose);
                Effects.BasicFX.SetWorldViewProj(wvp);
                Effects.BasicFX.SetTexTransform(Matrix.Scaling(6, 8, 1));
                Effects.BasicFX.SetMaterial(_gridMat);
                Effects.BasicFX.SetDiffuseMap(_floorTexSRV);
                pass.Apply(ImmediateContext);
                ImmediateContext.DrawIndexed(_gridIndexCount, _gridIndexOffset, _gridVertexOffset);

                world = _boxWorld;
                worldInvTranspose = MathF.InverseTranspose(world);
                wvp = world * viewProj;
                Effects.BasicFX.SetWorld(world);
                Effects.BasicFX.SetWorldInvTranspose(worldInvTranspose);
                Effects.BasicFX.SetWorldViewProj(wvp);
                Effects.BasicFX.SetTexTransform(Matrix.Identity);
                Effects.BasicFX.SetMaterial(_boxMat);
                Effects.BasicFX.SetDiffuseMap(_stoneTexSRV);
                pass.Apply(ImmediateContext);
                ImmediateContext.DrawIndexed(_boxIndexCount, _boxIndexOffset, _boxVertexOffset);

                foreach (var matrix in _cylWorld) {
                    world = matrix;
                    worldInvTranspose = MathF.InverseTranspose(world);
                    wvp = world * viewProj;
                    Effects.BasicFX.SetWorld(world);
                    Effects.BasicFX.SetWorldInvTranspose(worldInvTranspose);
                    Effects.BasicFX.SetWorldViewProj(wvp);
                    Effects.BasicFX.SetTexTransform(Matrix.Identity);
                    Effects.BasicFX.SetMaterial(_cylinderMat);
                    Effects.BasicFX.SetDiffuseMap(_brickTexSRV);
                    pass.Apply(ImmediateContext);
                    ImmediateContext.DrawIndexed(_cylinderIndexCount, _cylinderIndexOffset, _cylinderVertexOffset);
                }
                foreach (var matrix in _sphereWorld) {
                    world = matrix;
                    worldInvTranspose = MathF.InverseTranspose(world);
                    wvp = world * viewProj;
                    Effects.BasicFX.SetWorld(world);
                    Effects.BasicFX.SetWorldInvTranspose(worldInvTranspose);
                    Effects.BasicFX.SetWorldViewProj(wvp);
                    Effects.BasicFX.SetTexTransform(Matrix.Identity);
                    Effects.BasicFX.SetMaterial(_sphereMat);
                    Effects.BasicFX.SetDiffuseMap(_stoneTexSRV);
                    pass.Apply(ImmediateContext);
                    ImmediateContext.DrawIndexed(_sphereIndexCount, _sphereIndexOffset, _sphereVertexOffset);
                }
            }
            if (drawCenterSphere) {
                for (int p = 0; p < activeReflectTech.Description.PassCount; p++) {
                    var pass = activeReflectTech.GetPassByIndex(p);
                    var world = _centerSphereWorld;
                    var wit = MathF.InverseTranspose(world);
                    var wvp = world * viewProj;
                    Effects.BasicFX.SetWorld(world);
                    Effects.BasicFX.SetWorldInvTranspose(wit);
                    Effects.BasicFX.SetWorldViewProj(wvp);
                    Effects.BasicFX.SetTexTransform(Matrix.Identity);
                    Effects.BasicFX.SetMaterial(_centerSphereMat);
                    Effects.BasicFX.SetDiffuseMap(_stoneTexSRV);
                    Effects.BasicFX.SetCubeMap(_dynamicCubeMapSRV);

                    pass.Apply(ImmediateContext);
                    ImmediateContext.DrawIndexed(_sphereIndexCount, _sphereIndexOffset, _sphereVertexOffset);

                }
            }
            _sky.Draw(ImmediateContext, camera);

            ImmediateContext.Rasterizer.State = null;
            ImmediateContext.OutputMerger.DepthStencilState = null;
            ImmediateContext.OutputMerger.DepthStencilReference = 0;
        }
コード例 #5
0
ファイル: SSAO.cs プロジェクト: amitprakash07/dx11
        public void ComputeSsao(CameraBase camera) {
            _dc.OutputMerger.SetTargets((DepthStencilView)null, _ambientRTV0);
            _dc.ClearRenderTargetView(_ambientRTV0, Color.Black);
            _dc.Rasterizer.SetViewports(_ambientMapViewport);

            var T = Matrix.Identity;
            T.M11 = 0.5f;
            T.M22 = -0.5f;
            T.M41 = 0.5f;
            T.M42 = 0.5f;

            var P = camera.Proj;
            var pt = P * T;

            Effects.SsaoFX.SetViewToTexSpace(pt);
            Effects.SsaoFX.SetOffsetVectors(_offsets);
            Effects.SsaoFX.SetFrustumCorners(_frustumFarCorners);
            Effects.SsaoFX.SetNormalDepthMap(_normalDepthSRV);
            Effects.SsaoFX.SetRandomVecMap(_randomVectorSRV);

            var stride = Basic32.Stride;
            const int Offset = 0;

            _dc.InputAssembler.InputLayout = InputLayouts.Basic32;
            _dc.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
            _dc.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(_screenQuadVB, stride, Offset));
            _dc.InputAssembler.SetIndexBuffer(_screenQuadIB, Format.R16_UInt, 0);

            var tech = Effects.SsaoFX.SsaoTech;
            for (var p = 0; p < tech.Description.PassCount; p++) {
                tech.GetPassByIndex(p).Apply(_dc);
                _dc.DrawIndexed(6, 0, 0);
            }
        }