Example #1
0
        public virtual void DrawLine(Position <float> start, Position <float> end, Color <float> 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
            MatrixData matrixData = new MatrixData();
            Vector2    vector     = new 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
            matrixData.World = Matrix4x4.CreateScale(vector.Length(), padding, 1.0f);
            //2.compute the angle of rotate, we only rotate it at the z-axis
            matrixData.World *= Matrix4x4.CreateRotationZ((float)Math.Atan2(vector.Y, vector.X), new Vector3(0, padding * 0.5f, 0));
            //3.compute the translation, the position of start, but the y is start.y - padding * 0.5f
            matrixData.World *= Matrix4x4.CreateTranslation(new Vector3(start.X, start.Y - padding * 0.5f, 0));
            //4.keep transform matrix data
            matrixData.World *= Transform;

            //set project matrix
            matrixData.Project = mProject;

            //second, we set the render config
            RenderConfig renderConfig = new RenderConfig()
            {
                Color = color
            };

            //update buffer
            mMatrixDataBuffer.Update(matrixData);
            mRenderConfigBuffer.Update(renderConfig);

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

            mDevice.SetBuffer(mMatrixDataBuffer, mMatrixDataBufferSlot, GpuShaderType.VertexShader);
            mDevice.SetBuffer(mRenderConfigBuffer, mRenderConfigBufferSlot, GpuShaderType.PixelShader);

            //draw
            mDevice.DrawIndexed(6, 0, 0);
        }
Example #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);
        }
Example #3
0
        public void FillBezier(Position <float>[] controls, Color <float>[] colors)
        {
            Utility.Assert(controls.Length >= 3 && colors.Length >= 3);
            Utility.Assert(Mode == RenderMode.Fill);

            Vertex[] vertices = new Vertex[3];

            for (int i = 0; i < 3; i++)
            {
                vertices[i] = new Vertex()
                {
                    Position = new Vector3(controls[i].X, controls[i].Y, 0),
                    Color    = new Vector4(colors[i].Red, colors[i].Green, colors[i].Blue, colors[i].Alpha)
                };
            }

            vertices[0].TexCoord = new Vector2(0, 0);
            vertices[1].TexCoord = new Vector2(0.5f, 0);
            vertices[2].TexCoord = new Vector2(1, 1);

            TransformMatrix transform = new TransformMatrix()
            {
                World      = Transform,
                Projection = mProjection
            };

            var transformMatrix = transform.World * transform.Projection;

            TrianglePoints trianglePoints = new TrianglePoints()
            {
                Position0 = Vector4.Transform(vertices[0].Position, transformMatrix),
                Position1 = Vector4.Transform(vertices[1].Position, transformMatrix),
                Position2 = Vector4.Transform(vertices[2].Position, transformMatrix),
            };

            TriangleColors triangleColors = new TriangleColors()
            {
                Color0 = vertices[0].Color,
                Color1 = vertices[1].Color,
                Color2 = vertices[2].Color
            };

            var size = new Vector4(mCanvasSize.Width * 0.5f, mCanvasSize.Height * 0.5f, 0, 0);

            trianglePoints.Position0 = (trianglePoints.Position0 + new Vector4(1)) * size;
            trianglePoints.Position1 = (trianglePoints.Position1 + new Vector4(1)) * size;
            trianglePoints.Position2 = (trianglePoints.Position2 + new Vector4(1)) * size;

            trianglePoints.Position0.Y = mCanvasSize.Height - trianglePoints.Position0.Y;
            trianglePoints.Position1.Y = mCanvasSize.Height - trianglePoints.Position1.Y;
            trianglePoints.Position2.Y = mCanvasSize.Height - trianglePoints.Position2.Y;

            //update data to gpu
            mFillVertexBuffer.Update(vertices);
            mTransformBuffer.Update(transform);
            mTrianglePointsBuffer.Update(trianglePoints);
            mTriangleColorsBuffer.Update(triangleColors);

            mDevice.SetVertexBuffer(mFillVertexBuffer);
            mDevice.SetIndexBuffer(mFillIndexBuffer);
            mDevice.SetBuffer(mTransformBuffer, 0);
            mDevice.SetBuffer(mTrianglePointsBuffer, 1);
            mDevice.SetBuffer(mTriangleColorsBuffer, 2);

            mDevice.DrawIndexed(3);
        }