Exemplo n.º 1
0
        public virtual void DrawLine(Point2f start, Point2f end, Colorf color, float padding = 2.0f)
        {
            //draw line with start and end position
            //padding means the width of line
            //color.Alpha means the opacity of line

            //first, we compute the matrix of world transform
            var transform = new GuiTransform();
            var vector    = new System.Numerics.Vector2(end.X - start.X, end.Y - start.Y);

            //1.compute the scale component, x-component is euqal the length of line, y-component is equal the padding
            transform.World = Matrix4x4.CreateScale(vector.Length(), padding, 1.0f);
            //2.compute the angle of rotate, we only rotate it at the z-axis
            transform.World *= Matrix4x4.CreateRotationZ((float)Math.Atan2(vector.Y, vector.X), new System.Numerics.Vector3(0, padding * 0.5f, 0));
            //3.compute the translation, the position of start, but the y is start.y - padding * 0.5f
            transform.World *= Matrix4x4.CreateTranslation(new System.Numerics.Vector3(start.X, start.Y - padding * 0.5f, 0));
            //4.keep transform matrix data
            transform.World *= Transform;

            //set project matrix
            transform.Project = mProject;

            //second, we set the render config
            var renderConfig = new GuiRenderConfig()
            {
                Color = color, Config = new Vector4(0)
            };

            //update buffer
            mTransformBuffer.Update(transform);
            mRenderConfigBuffer.Update(renderConfig);

            //set buffer and shader
            mDevice.SetVertexBuffer(mSquareVertexBuffer);
            mDevice.SetIndexBuffer(mSquareIndexBuffer);

            mDevice.SetBuffer(mTransformBuffer, mTransformBufferSlot, GpuShaderType.All);
            mDevice.SetBuffer(mRenderConfigBuffer, mRenderConfigBufferSlot, GpuShaderType.All);

            //draw
            mDevice.DrawIndexed(6, 0, 0);
        }
Exemplo n.º 2
0
        private void DrawImage(Rectanglef rectangle, Image image, Colorf color, GuiRenderMode mode)
        {
            //fill rectangle with texture
            //the result color's alpha is equal texture.alpha * opacity

            //do not render image
            if (image.GpuTexture == null)
            {
                return;
            }

            var transform    = new GuiTransform();
            var renderConfig = new GuiRenderConfig()
            {
                Color = color, Config = new Vector4((int)mode)
            };

            //1.scale the rectangle
            transform.World = Matrix4x4.CreateScale(rectangle.Right - rectangle.Left, rectangle.Bottom - rectangle.Top, 1.0f);
            //2.translate it
            transform.World *= Matrix4x4.CreateTranslation(rectangle.Left, rectangle.Top, 0.0f);
            //3.keep transform matrix data
            transform.World *= Transform;

            //set projection matrix
            transform.Project = mProject;

            mTransformBuffer.Update(transform);
            mRenderConfigBuffer.Update(renderConfig);

            mDevice.SetVertexBuffer(mSquareVertexBuffer);
            mDevice.SetIndexBuffer(mSquareIndexBuffer);

            mDevice.SetBuffer(mTransformBuffer, mTransformBufferSlot, GpuShaderType.All);
            mDevice.SetBuffer(mRenderConfigBuffer, mRenderConfigBufferSlot, GpuShaderType.All);
            mDevice.SetResourceUsage(image.GpuResourceUsage, mTextureSlot, GpuShaderType.All);

            mDevice.DrawIndexed(6, 0, 0);
        }
Exemplo n.º 3
0
 public virtual void DrawText(Rectanglef rectangle, Text text, Colorf color)
 {
     DrawImage(rectangle, text.Image, color, GuiRenderMode.Text);
 }
Exemplo n.º 4
0
 public virtual void DrawImage(Rectanglef rectangle, Image image, Colorf color)
 {
     DrawImage(rectangle, image, color, GuiRenderMode.Image);
 }
Exemplo n.º 5
0
        public virtual void DrawRectangle(Rectanglef rectangle, Colorf color, float padding = 2.0f)
        {
            //draw rectangle with color
            //padding means the width of edge
            //color.Alpha means the opacity of edge

            //read rectangle data
            var outSide = rectangle;

            //inside rectangle will smaller than outside
            var inSide = new Rectanglef(
                outSide.Left + padding,
                outSide.Top + padding,
                outSide.Right - padding,
                outSide.Bottom - padding);

            //first, we need compute the vertex buffer for our rectangle
            //we do not need tex coord of vertex
            //we can compute the index buffer at init the render
            GuiVertex[] vertics = new GuiVertex[]
            {
                new GuiVertex()
                {
                    Position = new Vector3f(outSide.Left, outSide.Top, 0)
                },
                new GuiVertex()
                {
                    Position = new Vector3f(outSide.Right, outSide.Top, 0)
                },
                new GuiVertex()
                {
                    Position = new Vector3f(outSide.Right, outSide.Bottom, 0)
                },
                new GuiVertex()
                {
                    Position = new Vector3f(outSide.Left, outSide.Bottom, 0)
                },
                new GuiVertex()
                {
                    Position = new Vector3f(inSide.Left, inSide.Top, 0)
                },
                new GuiVertex()
                {
                    Position = new Vector3f(inSide.Right, inSide.Top, 0)
                },
                new GuiVertex()
                {
                    Position = new Vector3f(inSide.Right, inSide.Bottom, 0)
                },
                new GuiVertex()
                {
                    Position = new Vector3f(inSide.Left, inSide.Bottom, 0)
                }
            };

            //update vertex buffer
            mRectangleVertexBuffer.Update(vertics);

            //second, update constant buffer
            var transform = new GuiTransform()
            {
                World = Transform, Project = mProject
            };
            var renderConfig = new GuiRenderConfig()
            {
                Color = color, Config = new Vector4(0)
            };

            mTransformBuffer.Update(transform);
            mRenderConfigBuffer.Update(renderConfig);

            //set buffer and shader
            mDevice.SetVertexBuffer(mRectangleVertexBuffer);
            mDevice.SetIndexBuffer(mRectangleIndexBuffer);

            mDevice.SetBuffer(mTransformBuffer, mTransformBufferSlot, GpuShaderType.All);
            mDevice.SetBuffer(mRenderConfigBuffer, mRenderConfigBufferSlot, GpuShaderType.All);

            //draw
            mDevice.DrawIndexed(24, 0, 0);
        }
Exemplo n.º 6
0
 public virtual void Clear(Image image, Colorf clear)
 {
     mDevice.ClearRenderTarget(
         renderTarget: image.GpuRenderTarget,
         color: clear);
 }