Beispiel #1
0
        public void DrawHollowCircle(Vector2 center, float radius, Color color, float scale, Vector2 origin, int segments = 0)
        {
            radius *= scale;
            center -= origin;

            if (segments <= 0)
            {
                segments = (int)(radius + radius);
            }

            VertexPositionColorTexture[] vertexData = new VertexPositionColorTexture[segments];
            int[] indexData = new int[segments * 2];

            // update vertices
            // implemented using http://slabode.exofire.net/circle_draw.shtml (Just slightly reorganized and I decided to keep the comments)
            float theta = (float)(2.0 * Math.PI / segments);
            float t, c = (float)System.Math.Cos(theta), s = (float)System.Math.Sin(theta);   // precalculate the sine and cosine

            float x = radius * Math.Cos(0),
                  y = radius * Math.Sin(0);

            for (int i = 0; i < vertexData.Length; i++)
            {
                vertexData[i] = new VertexPositionColorTexture(
                    new Vector3(center.X + x, center.Y + y, 0f),
                    color,
                    Microsoft.Xna.Framework.Vector2.Zero
                    );

                // apply the rotation matrix
                t = x;
                x = c * x - s * y;
                y = s * t + c * y;

                indexData[2 * i]     = i; // current vertex
                indexData[2 * i + 1] = (i + 1) % vertexData.Length;
            }

            PrimitiveBatchItem batchItem = new PrimitiveBatchItem();

            batchItem.Set(
                vertexData,
                indexData,
                isHollow: true,
                shader: null,
                shaderParameters: null,
                texture: null
                );

            _batchHollowItems.Add(batchItem);
            _hollowVerticesCount += vertexData.Length;
            _hollowIndicesCount  += indexData.Length;
        }
Beispiel #2
0
        public void DrawHollowRectangle(Vector2 position, Size size, Color color, float rotation, Vector2 scale, Vector2 origin)
        {
            Vector2 topLeft     = position + Math.Rotate(-origin * scale, rotation),
                    topRight    = position + Math.Rotate((-origin + new Vector2(size.Width, 0f)) * scale, rotation),
                    bottomRight = position + Math.Rotate((-origin + size) * scale, rotation),
                    bottomLeft  = position + Math.Rotate((-origin + new Vector2(0f, size.Height)) * scale, rotation);

            VertexPositionColorTexture[] vertexData = new VertexPositionColorTexture[] {
                new VertexPositionColorTexture(
                    new Vector3(topLeft, 0f),
                    color,
                    Microsoft.Xna.Framework.Vector2.Zero
                    ),
                new VertexPositionColorTexture(
                    new Vector3(topRight, 0f),
                    color,
                    Microsoft.Xna.Framework.Vector2.Zero
                    ),
                new VertexPositionColorTexture(
                    new Vector3(bottomRight, 0f),
                    color,
                    Microsoft.Xna.Framework.Vector2.Zero
                    ),
                new VertexPositionColorTexture(
                    new Vector3(bottomLeft, 0f),
                    color,
                    Microsoft.Xna.Framework.Vector2.Zero
                    )
            };

            int[] indexData = new int[] {
                0, 1,
                1, 2,
                2, 3,
                3, 0
            };

            PrimitiveBatchItem batchItem = new PrimitiveBatchItem();

            batchItem.Set(
                vertexData,
                indexData,
                isHollow: true,
                shader: null,
                shaderParameters: null,
                texture: null
                );

            _batchHollowItems.Add(batchItem);
            _hollowVerticesCount += vertexData.Length;
            _hollowIndicesCount  += indexData.Length;
        }
Beispiel #3
0
        public void DrawLines(IList <Vector2> points, Vector2 position, Color color, float rotation, Vector2 scale, Vector2 origin, bool cyclic = true)
        {
            VertexPositionColorTexture[] vertexData = new VertexPositionColorTexture[points.Count];

            int[] indexData;
            if (cyclic)
            {
                indexData = new int[points.Count * 2];
            }
            else
            {
                indexData = new int[(points.Count * 2) - 2];
            }

            int i = 0;

            if (rotation % 360 != 0)
            {
                for (int j = 0; j < points.Count; j++)
                {
                    Vector2 point = points[j];
                    vertexData[i] = new VertexPositionColorTexture(
                        new Vector3(Math.Rotate(((point + position) - origin) * scale, rotation), 0f),
                        color,
                        Microsoft.Xna.Framework.Vector2.Zero
                        );

                    // only add index if isn't last point or is cyclic
                    if (j < points.Count - 1 || cyclic)
                    {
                        indexData[2 * i]     = i;
                        indexData[2 * i + 1] = (i + 1) % vertexData.Length;
                    }

                    i++;
                }
            }
            else
            {
                for (int j = 0; j < points.Count; j++)
                {
                    Vector2 point = points[j];
                    vertexData[i] = new VertexPositionColorTexture(
                        new Vector3((point + position - origin) * scale, 0f),
                        color,
                        Microsoft.Xna.Framework.Vector2.Zero
                        );

                    if (j < points.Count - 1 || cyclic)
                    {
                        indexData[2 * i]     = i;
                        indexData[2 * i + 1] = (i + 1) % vertexData.Length;
                    }

                    i++;
                }
            }

            PrimitiveBatchItem batchItem = new PrimitiveBatchItem();

            batchItem.Set(
                vertexData,
                indexData,
                isHollow: true,
                shader: null,
                shaderParameters: null,
                texture: null
                );

            _batchHollowItems.Add(batchItem);
            _hollowVerticesCount += vertexData.Length;
            _hollowIndicesCount  += indexData.Length;
        }
Beispiel #4
0
        public void DrawFilledPolygon(Polygon polygon, Color color, float rotation, Vector2 scale, Vector2 origin)
        {
            VertexPositionColorTexture[] vertexData = new VertexPositionColorTexture[polygon.VertexCount + 1];
            int centerVertexId = vertexData.Length - 1;

            int[] indexData = new int[polygon.VertexCount * 3];

            int i = 0;

            if (rotation % 360 != 0)
            {
                vertexData[centerVertexId] = new VertexPositionColorTexture(
                    new Vector3(Math.Rotate((polygon.Center - origin) * scale, rotation), 0f),
                    color,
                    Microsoft.Xna.Framework.Vector2.Zero
                    );

                foreach (Vector2 point in polygon)
                {
                    vertexData[i] = new VertexPositionColorTexture(
                        new Vector3(Math.Rotate((point - origin) * scale, rotation), 0f),
                        color,
                        Microsoft.Xna.Framework.Vector2.Zero
                        );

                    indexData[3 * i]     = i;
                    indexData[3 * i + 1] = (i + 1) % polygon.VertexCount;
                    indexData[3 * i + 2] = centerVertexId;
                    i++;
                }
            }
            else
            {
                vertexData[centerVertexId] = new VertexPositionColorTexture(
                    new Vector3((polygon.Center - origin) * scale, 0f),
                    color,
                    Microsoft.Xna.Framework.Vector2.Zero
                    );

                foreach (Vector2 point in polygon)
                {
                    vertexData[i] = new VertexPositionColorTexture(
                        new Vector3((point - origin) * scale, 0f),
                        color,
                        Microsoft.Xna.Framework.Vector2.Zero
                        );

                    indexData[3 * i]     = i;
                    indexData[3 * i + 1] = (i + 1) % polygon.VertexCount;
                    indexData[3 * i + 2] = centerVertexId;
                    i++;
                }
            }

            PrimitiveBatchItem batchItem = new PrimitiveBatchItem();

            batchItem.Set(
                vertexData,
                indexData,
                isHollow: false,
                shader: null,
                shaderParameters: null,
                texture: null
                );

            _batchFilledItems.Add(batchItem);
            _filledVerticesCount += vertexData.Length;
            _filledIndicesCount  += indexData.Length;
        }