Пример #1
0
        public ParticleSystem(GraphicsDevice device, ContentManager content)
        {
            this.device = device;

            // Create vertex buffer used to spawn new particles
            this.particleStart = Buffer.Vertex.New <ParticleVertex>(device, MAX_NEW);

            // Create vertex buffers to use for updating and drawing the particles alternatively
            var vbFlags = BufferFlags.VertexBuffer | BufferFlags.StreamOutput;

            this.particleDrawFrom = Buffer.New <ParticleVertex>(device, MAX_PARTICLES, vbFlags);
            this.particleStreamTo = Buffer.New <ParticleVertex>(device, MAX_PARTICLES, vbFlags);

            this.layout  = VertexInputLayout.FromBuffer(0, this.particleStreamTo);
            this.effect  = content.Load <Effect>("ParticleEffect");
            this.texture = content.Load <Texture2D>("Dot");

            this.viewParameter           = effect.Parameters["_view"];
            this.projParameter           = effect.Parameters["_proj"];
            this.lookAtMatrixParameter   = effect.Parameters["_lookAtMatrix"];
            this.elapsedSecondsParameter = effect.Parameters["_elapsedSeconds"];
            this.camDirParameter         = effect.Parameters["_camDir"];
            this.gravityParameter        = effect.Parameters["_gravity"];
            this.textureParameter        = effect.Parameters["_texture"];
            this.samplerParameter        = effect.Parameters["_sampler"];
            this.updatePass = effect.Techniques["UpdateTeq"].Passes[0];
            this.renderPass = effect.Techniques["RenderTeq"].Passes[0];
        }
Пример #2
0
        private Buffer <SolidVertex> GetVertexBufferFromBoundingBox(BoundingBox box)
        {
            var p0 = new SolidVertex(new Vector3(box.Minimum.X, box.Minimum.Y, box.Minimum.Z));
            var p1 = new SolidVertex(new Vector3(box.Maximum.X, box.Minimum.Y, box.Minimum.Z));
            var p2 = new SolidVertex(new Vector3(box.Maximum.X, box.Minimum.Y, box.Maximum.Z));
            var p3 = new SolidVertex(new Vector3(box.Minimum.X, box.Minimum.Y, box.Maximum.Z));
            var p4 = new SolidVertex(new Vector3(box.Minimum.X, box.Maximum.Y, box.Minimum.Z));
            var p5 = new SolidVertex(new Vector3(box.Maximum.X, box.Maximum.Y, box.Minimum.Z));
            var p6 = new SolidVertex(new Vector3(box.Maximum.X, box.Maximum.Y, box.Maximum.Z));
            var p7 = new SolidVertex(new Vector3(box.Minimum.X, box.Maximum.Y, box.Maximum.Z));

            var vertices = new[]
            {
                p4, p5, p5, p1, p1, p0, p0, p4,
                p5, p6, p6, p2, p2, p1, p1, p5,
                p2, p6, p6, p7, p7, p3, p3, p2,
                p7, p4, p4, p0, p0, p3, p3, p7,
                p7, p6, p6, p5, p5, p4, p4, p7,
                p0, p1, p1, p2, p2, p3, p3, p0
            };

            return(Buffer.New(_device, vertices, BufferFlags.VertexBuffer, SharpDX.Direct3D11.ResourceUsage.Default));
        }
Пример #3
0
        protected override void OnDraw()
        {
            // To make sure things are in a defined state for legacy rendering...
            ((TombLib.Rendering.DirectX11.Dx11RenderingSwapChain)SwapChain).BindForce();
            ((TombLib.Rendering.DirectX11.Dx11RenderingDevice)Device).ResetState();

            _device.SetDepthStencilState(_device.DepthStencilStates.Default);
            _device.SetRasterizerState(_device.RasterizerStates.CullBack);
            _device.SetBlendState(_device.BlendStates.Opaque);

            Matrix4x4 viewProjection = Camera.GetViewProjectionMatrix(ClientSize.Width, ClientSize.Height);

            Effect solidEffect = _deviceManager.___LegacyEffects["Solid"];

            if (Static != null)
            {
                var model = _wadRenderer.GetStatic(Static);

                var effect = _deviceManager.___LegacyEffects["StaticModel"];

                var world = GizmoTransform;

                effect.Parameters["ModelViewProjection"].SetValue((world * viewProjection).ToSharpDX());
                effect.Parameters["Color"].SetValue(Vector4.One);
                effect.Parameters["Texture"].SetResource(_wadRenderer.Texture);
                effect.Parameters["TextureSampler"].SetResource(_device.SamplerStates.Default);

                for (int i = 0; i < model.Meshes.Count; i++)
                {
                    var mesh = model.Meshes[i];

                    _device.SetVertexBuffer(0, mesh.VertexBuffer);
                    _device.SetIndexBuffer(mesh.IndexBuffer, true);
                    _device.SetVertexInputLayout(VertexInputLayout.FromBuffer(0, mesh.VertexBuffer));

                    effect.Parameters["ModelViewProjection"].SetValue((world * viewProjection).ToSharpDX());
                    effect.Techniques[0].Passes[0].Apply();

                    foreach (var submesh in mesh.Submeshes)
                    {
                        _device.Draw(PrimitiveType.TriangleList, submesh.Value.NumIndices, submesh.Value.BaseIndex);
                    }

                    //foreach (var submesh in mesh.Submeshes)
                    //    _device.DrawIndexed(PrimitiveType.TriangleList, submesh.Value.NumIndices, submesh.Value.MeshBaseIndex);
                }

                _device.SetRasterizerState(_rasterizerWireframe);

                // Draw boxes
                if (DrawVisibilityBox || DrawCollisionBox)
                {
                    if (DrawVisibilityBox)
                    {
                        _vertexBufferVisibility?.Dispose();
                        _vertexBufferVisibility = GetVertexBufferFromBoundingBox(Static.VisibilityBox);

                        _device.SetVertexBuffer(_vertexBufferVisibility);
                        _device.SetVertexInputLayout(VertexInputLayout.FromBuffer(0, _vertexBufferVisibility));
                        _device.SetIndexBuffer(null, false);

                        solidEffect.Parameters["ModelViewProjection"].SetValue(viewProjection.ToSharpDX());
                        solidEffect.Parameters["Color"].SetValue(new Vector4(0.0f, 1.0f, 0.0f, 1.0f));
                        solidEffect.CurrentTechnique.Passes[0].Apply();

                        _device.Draw(PrimitiveType.LineList, _vertexBufferVisibility.ElementCount);
                    }

                    if (DrawCollisionBox)
                    {
                        _vertexBufferCollision?.Dispose();
                        _vertexBufferCollision = GetVertexBufferFromBoundingBox(Static.CollisionBox);

                        _device.SetVertexBuffer(_vertexBufferCollision);
                        _device.SetVertexInputLayout(VertexInputLayout.FromBuffer(0, _vertexBufferCollision));
                        _device.SetIndexBuffer(null, false);

                        solidEffect.Parameters["ModelViewProjection"].SetValue(viewProjection.ToSharpDX());
                        solidEffect.Parameters["Color"].SetValue(new Vector4(1.0f, 0.0f, 0.0f, 1.0f));
                        solidEffect.CurrentTechnique.Passes[0].Apply();

                        _device.Draw(PrimitiveType.LineList, _vertexBufferCollision.ElementCount);
                    }
                }

                // Draw normals
                if (DrawNormals)
                {
                    var lines = new List <SolidVertex>();
                    for (int i = 0; i < Static.Mesh.VerticesNormals.Count; i++)
                    {
                        var p = Vector3.Transform(Static.Mesh.VerticesPositions[i], world);
                        var n = Vector3.TransformNormal(Static.Mesh.VerticesNormals[i] /
                                                        Static.Mesh.VerticesNormals[i].Length(), world);

                        var v = new SolidVertex();
                        v.Position = p;
                        v.Color    = new Vector4(1.0f, 1.0f, 1.0f, 1.0f);
                        lines.Add(v);

                        v          = new SolidVertex();
                        v.Position = p + n * 256.0f;
                        v.Color    = new Vector4(1.0f, 1.0f, 1.0f, 1.0f);
                        lines.Add(v);
                    }

                    Buffer <SolidVertex> bufferLines = Buffer.New(_device, lines.ToArray(), BufferFlags.VertexBuffer, SharpDX.Direct3D11.ResourceUsage.Default);
                    _device.SetVertexBuffer(bufferLines);
                    _device.SetVertexInputLayout(VertexInputLayout.FromBuffer(0, bufferLines));
                    _device.SetIndexBuffer(null, false);

                    solidEffect.Parameters["ModelViewProjection"].SetValue(viewProjection.ToSharpDX());
                    solidEffect.Parameters["Color"].SetValue(new Vector4(1.0f, 1.0f, 1.0f, 1.0f));
                    solidEffect.CurrentTechnique.Passes[0].Apply();

                    _device.Draw(PrimitiveType.LineList, bufferLines.ElementCount);
                }
            }

            if (DrawGrid)
            {
                _device.SetRasterizerState(_rasterizerWireframe);

                // Draw the grid
                _device.SetVertexBuffer(0, _plane.VertexBuffer);
                _device.SetVertexInputLayout(VertexInputLayout.FromBuffer(0, _plane.VertexBuffer));
                _device.SetIndexBuffer(_plane.IndexBuffer, true);

                solidEffect.Parameters["ModelViewProjection"].SetValue(viewProjection.ToSharpDX());
                solidEffect.Parameters["Color"].SetValue(Vector4.One);
                solidEffect.Techniques[0].Passes[0].Apply();

                _device.Draw(PrimitiveType.LineList, _plane.VertexBuffer.ElementCount);
            }

            if (DrawLights)
            {
                _device.SetRasterizerState(_rasterizerWireframe);

                foreach (var light in Static.Lights)
                {
                    // Draw the little sphere
                    _device.SetVertexBuffer(0, _littleSphere.VertexBuffer);
                    _device.SetVertexInputLayout(VertexInputLayout.FromBuffer(0, _littleSphere.VertexBuffer));
                    _device.SetIndexBuffer(_littleSphere.IndexBuffer, false);

                    var world = Matrix4x4.CreateTranslation(light.Position);
                    solidEffect.Parameters["ModelViewProjection"].SetValue((world * viewProjection).ToSharpDX());
                    solidEffect.Parameters["Color"].SetValue(new Vector4(1.0f, 1.0f, 0.0f, 1.0f));
                    solidEffect.Techniques[0].Passes[0].Apply();

                    _device.DrawIndexed(PrimitiveType.TriangleList, _littleSphere.IndexBuffer.ElementCount);

                    if (SelectedLight == light)
                    {
                        _device.SetVertexBuffer(0, _sphere.VertexBuffer);
                        _device.SetVertexInputLayout(VertexInputLayout.FromBuffer(0, _sphere.VertexBuffer));
                        _device.SetIndexBuffer(_sphere.IndexBuffer, false);

                        world = Matrix4x4.CreateScale(light.Radius * 2.0f) * Matrix4x4.CreateTranslation(light.Position);
                        solidEffect.Parameters["ModelViewProjection"].SetValue((world * viewProjection).ToSharpDX());
                        solidEffect.Parameters["Color"].SetValue(new Vector4(0.0f, 1.0f, 0.0f, 1.0f));
                        solidEffect.Techniques[0].Passes[0].Apply();

                        _device.DrawIndexed(PrimitiveType.TriangleList, _sphere.IndexBuffer.ElementCount);
                    }
                }

                _device.SetRasterizerState(_device.RasterizerStates.CullBack);
            }

            if (DrawGizmo)
            {
                // Draw the gizmo
                SwapChain.ClearDepth();
                _gizmo.Draw(viewProjection);
            }

            if (SelectedLight != null)
            {
                // Draw the gizmo of selected light
                SwapChain.ClearDepth();
                _gizmoLight.Draw(viewProjection);
            }

            // Draw debug strings
            ((TombLib.Rendering.DirectX11.Dx11RenderingDevice)Device).ResetState(); // To make sure SharpDx.Toolkit didn't change settings.
            SwapChain.RenderText(new Text
            {
                Font      = _fontDefault,
                PixelPos  = new Vector2(5, 5),
                Alignment = new Vector2(0, 0),
                String    =
                    "Position: " + StaticPosition +
                    "\nRotation: " + StaticRotation.X * (180 / Math.PI) +
                    "\nScale: " + StaticScale
            });
        }