Example #1
0
        public override void Setup()
        {
            GL.ClearColor(0.2f, 0.3f, 0.3f, 1.0f);
            var vertices = new ColoredVertex[]
            {
                new ColoredVertex(new Vector3(-0.5f, -0.5f, 0.0f), Color4.AliceBlue),     //Bottom-left vertex
                new ColoredVertex(new Vector3(0.5f, -0.5f, 0.0f), Color4.BlanchedAlmond), //Bottom-right vertex
                new ColoredVertex(new Vector3(0.0f, 0.5f, 0.0f), Color4.Fuchsia)          //Top vertex
            };


            var vbo = VertexBuffer.CreateVertexBuffer();

            vbo.Bind();
            vbo.LoadData(vertices);
            var attributes = new [] {
                new VertexAttribute("aPosition", 3, VertexAttribPointerType.Float, sizeof(float) * (3 + 4), 0),
                new VertexAttribute("aColor", 4, VertexAttribPointerType.Float, sizeof(float) * (3 + 4), sizeof(float) * 3)
            };
            var shader = ShaderProgram.CreateShaderProgram("Assets/vertex.shader", "Assets/frag.shader", attributes);

            shader.Use();
            var vao = VertexArray.CreateVertexArray();

            vao.Bind();
            shader.Use();
            shader.SetVertexAttributes();
            vbo.Bind();
            VertexArrayObject  = vao;
            VertexBufferObject = vbo;
            Shader             = shader;
            base.Setup();
        }
Example #2
0
        public void DrawSector(Vector3 center, Color color, float radius, float angleGrad, float heading = 0.0f)
        {
            int slices = 30;
            if (angleGrad < 120)
            {
                slices = 10;
            }

            int colorInt = color.ToArgb();

            double h = ((Math.PI * 2) - heading) + (Math.PI / 2);
            if (h > (Math.PI * 2))
                h = h - (Math.PI * 2);

            float angle = (float)((angleGrad / 180f) * Math.PI);
            float angleStep = angle / slices;
            _vertexBuffer[0] = new ColoredVertex(center, colorInt);

            float currentAngle = -(angle / 2.0f) + (float)h;
            int vertexIndex = 1;
            for (int i = 0; i < slices; i++, currentAngle += angleStep, vertexIndex++)
            {
                var sine = (float)Math.Sin(currentAngle);
                var cose = (float)Math.Cos(currentAngle);
                _vertexBuffer[vertexIndex] = 
                    new ColoredVertex(center.X + cose * radius, center.Y, center.Z + sine * radius, colorInt);
            }

            SetDeclaration();
            Device.DrawUserPrimitives(PrimitiveType.TriangleFan, slices - 1, _vertexBuffer);
        }
Example #3
0
        public void DrawDonut(Vector3 center, float inRadius, float outRadius, Color color)
        {
            int slices = 30;
            var radsPerSlice = (float)(Math.PI * 2 / slices);
            int colorInt = color.ToArgb();
            // x -> cos
            // z -> sin

            _vertexBuffer[0] = new ColoredVertex(center, colorInt);
            _vertexBuffer[1] = new ColoredVertex(center, colorInt);

            _vertexBuffer[0].Position.X += inRadius;
            _vertexBuffer[1].Position.X += outRadius;

            float angle = 0.0f;
            int vertexIndex = 2;
            for (int i = 1; i < slices; i++, vertexIndex+=2)
            {
                angle += radsPerSlice;
                var sine = (float)Math.Sin(angle);
                var cose = (float)Math.Cos(angle);

                _vertexBuffer[vertexIndex] = new ColoredVertex(center.X + cose * inRadius, center.Y, center.Z + sine * inRadius, colorInt);
                _vertexBuffer[vertexIndex+1] = new ColoredVertex(center.X + cose * outRadius, center.Y, center.Z + sine * outRadius, colorInt);
            }

            _vertexBuffer[vertexIndex] = _vertexBuffer[0];
            _vertexBuffer[vertexIndex + 1] = _vertexBuffer[1];

            SetDeclaration();
            Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, slices*2, _vertexBuffer);
        }
Example #4
0
        public void DrawAgroLine(Clio.Utilities.Vector3 center, float heading, float width, float height, Color color, Color pointColor)
        {
            var newCenter = new Vector3(center.X, center.Y, center.Z);

            float heightBack = width;

            float diag     = (float)Math.Sqrt(height * height + width * width) / 2;
            float diagBack = (float)Math.Sqrt(heightBack * heightBack + width * width) / 2;

            float subangle = (float)Math.Atan2(width / 2, height / 2);


            float h = (float)(((Math.PI * 2) - heading) + (Math.PI / 2));

            float r1 = h - subangle;
            float r2 = h + ((float)Math.PI / 4) + (float)Math.PI;
            float r3 = h - ((float)Math.PI / 4) + (float)Math.PI;
            float r4 = h + subangle;

            _vertexBuffer[0] = new ColoredVertex(newCenter, pointColor);
            _vertexBuffer[1] = new ColoredVertex(newCenter + new Vector3((float)Math.Cos(r1) * diag, 0, (float)Math.Sin(r1) * diag), pointColor);
            _vertexBuffer[2] = new ColoredVertex(newCenter + new Vector3((float)Math.Cos(r2) * diagBack, 0, (float)Math.Sin(r2) * diagBack), color);
            _vertexBuffer[3] = new ColoredVertex(newCenter + new Vector3((float)Math.Cos(r3) * diagBack, 0, (float)Math.Sin(r3) * diagBack), color);
            _vertexBuffer[4] = new ColoredVertex(newCenter + new Vector3((float)Math.Cos(r4) * diag, 0, (float)Math.Sin(r4) * diag), pointColor);
            _vertexBuffer[5] = _vertexBuffer[1];

            SetDeclaration();
            Device.DrawUserPrimitives(PrimitiveType.TriangleFan, 4, _vertexBuffer);
        }
Example #5
0
        public void DrawCircleWithPoint(Clio.Utilities.Vector3 center, float heading, float radius, Color color, Color pointColor)
        {
            int slices       = 30;
            var radsPerSlice = (float)(Math.PI * 2 / slices);

            var newCenter = new Vector3(center.X, center.Y, center.Z);

            _vertexBuffer[0] = new ColoredVertex(newCenter, color);
            _vertexBuffer[1] = new ColoredVertex(newCenter + new Vector3(radius, 0, 0), color);

            for (int i = 0; i < slices; i++)
            {
                double h = ((Math.PI * 2) - heading) + (Math.PI / 2);
                if (h > (Math.PI * 2))
                {
                    h = h - (Math.PI * 2);
                }

                bool watchAt = (i * radsPerSlice) < h && h < ((i + 1) * radsPerSlice);

                var sine   = (float)Math.Sin((i + 1) * radsPerSlice);
                var cosine = (float)Math.Cos((i + 1) * radsPerSlice);

                _vertexBuffer[2 + i] =
                    new ColoredVertex(newCenter + new Vector3(cosine * radius, 0, sine * radius),
                                      watchAt ? pointColor.ToArgb() : color.ToArgb());
            }

            SetDeclaration();
            Device.DrawUserPrimitives(PrimitiveType.TriangleFan, slices, _vertexBuffer);
        }
Example #6
0
        public Water(SharpDevice device)
        {
            vertices = new ColoredVertex[N * M];
            for (int i = 0; i < M; i++)
            {
                for (int j = 0; j < N; j++)
                {
                    vertices[i * N + j] = new ColoredVertex(
                        new Vector3(i * size / N + botLeftCorner.X, 0 + botLeftCorner.Y, j * size / M + botLeftCorner.Z),
                        new Vector4((i + 0.0f) / M, (j + 0.0f) / N, 0, 1)
                        );
                }
            }

            indices = new int[6 * (N - 1) * (M - 1)];
            for (int i = 0; i < M - 1; i++)
            {
                for (int j = 0; j < N - 1; j++)
                {
                    int act       = i * (N - 1) + j;
                    int fullIndex = i * N + j;

                    indices[6 * act + 0] = fullIndex;
                    indices[6 * act + 1] = fullIndex + 1;
                    indices[6 * act + 2] = fullIndex + N;
                    indices[6 * act + 3] = fullIndex + 1;
                    indices[6 * act + 4] = fullIndex + N + 1;
                    indices[6 * act + 5] = fullIndex + N;
                }
            }

            this.createMesh(device);
        }
Example #7
0
        private void SetDeclaration()
        {
            if (_coloredVertexDecl == null)
                _coloredVertexDecl = ColoredVertex.GetDecl(Device);

            Device.VertexDeclaration = _coloredVertexDecl;
            Device.VertexFormat = ColoredVertex.Format;
        }
Example #8
0
        public static ColoredVertex ReadFrom(System.IO.BinaryReader reader)
        {
            var result = new ColoredVertex();

            result.Position = Model.Vec2Float.ReadFrom(reader);
            result.Color    = Model.ColorFloat.ReadFrom(reader);
            return(result);
        }
Example #9
0
        public void DrawTriangleFan(Clio.Utilities.Vector3[] poly, int index, int count, Color color)
        {
            for (int i = 0; i < count; i++)
                _vertexBuffer[i] = new ColoredVertex(poly[index + i].Convert(), color);

            SetDeclaration();
            Device.DrawUserPrimitives(PrimitiveType.TriangleFan, count - 2, _vertexBuffer);
        }
Example #10
0
        public void DrawTriangle(Vector3 a, Vector3 b, Vector3 c, Color color)
        {
            _vertexBuffer[0] = new ColoredVertex(a, color);
            _vertexBuffer[1] = new ColoredVertex(b, color);
            _vertexBuffer[2] = new ColoredVertex(c, color);

            SetDeclaration();
            Device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, _vertexBuffer);
        }
Example #11
0
        public static void DrawLine(Vec2Int p1, Vec2Int p2, Color color, DebugInterface debugInterface)
        {
            var vertex1 = new ColoredVertex(new Vec2Float(p1.X + 0.5f, p1.Y + 0.5f), new Vec2Float(0, 0), color);
            var vertex2 = new ColoredVertex(new Vec2Float(p2.X + 0.5f, p2.Y + 0.5f), new Vec2Float(0, 0), color);

            var debugData    = new DebugData.Primitives(new[] { vertex1, vertex2 }, PrimitiveType.Lines);
            var debugCommand = new DebugCommand.Add(debugData);

            debugInterface.Send(debugCommand);
        }
Example #12
0
            public static new PlacedText ReadFrom(System.IO.BinaryReader reader)
            {
                ColoredVertex vertex    = Model.ColoredVertex.ReadFrom(reader);
                string        text      = System.Text.Encoding.UTF8.GetString(reader.ReadBytes(reader.ReadInt32()));
                float         alignment = reader.ReadSingle();
                float         size      = reader.ReadSingle();
                var           result    = new PlacedText(vertex, text, alignment, size);

                return(result);
            }
Example #13
0
        public void DrawLine(Vector3 start, Vector3 end, Color color, float width = 0.025f)
        {
            Vector3 dir = end - start;

            dir.Z = 0;

            Vector3 extDir1;
            Vector3 extDir2;

            if (dir.LengthSquared() > 0.0001f)
            {
                dir.Normalize();

                extDir1.X = -dir.Y;
                extDir1.Y = dir.X;
                extDir1.Z = 0;

                extDir2 = Vector3.Cross(dir, extDir1);
            }
            else
            {
                extDir1 = Vector3.UnitX;
                extDir2 = Vector3.UnitY;
            }

            _vertexBuffer[0] = new ColoredVertex(start + extDir1 * (width / 2), color);
            _vertexBuffer[1] = new ColoredVertex(start - extDir1 * (width / 2), color);
            _vertexBuffer[2] = new ColoredVertex(end + extDir1 * (width / 2), color);
            _vertexBuffer[3] = new ColoredVertex(end - extDir1 * (width / 2), color);

            _vertexBuffer[4] = new ColoredVertex(start + extDir2 * (width / 2), color);
            _vertexBuffer[5] = new ColoredVertex(start - extDir2 * (width / 2), color);
            _vertexBuffer[6] = new ColoredVertex(end + extDir2 * (width / 2), color);
            _vertexBuffer[7] = new ColoredVertex(end - extDir2 * (width / 2), color);

            _indexBuffer[0] = 0;
            _indexBuffer[1] = 1;
            _indexBuffer[2] = 2;

            _indexBuffer[3] = 1;
            _indexBuffer[4] = 2;
            _indexBuffer[5] = 3;

            _indexBuffer[6] = 4;
            _indexBuffer[7] = 5;
            _indexBuffer[8] = 6;

            _indexBuffer[9]  = 5;
            _indexBuffer[10] = 6;
            _indexBuffer[11] = 7;

            SetDeclaration();
            Device.DrawIndexedUserPrimitives(PrimitiveType.TriangleList, 0, 8, 4, _indexBuffer,
                                             Format.Index32, _vertexBuffer, ColoredVertex.Stride);
        }
Example #14
0
            public static new Polygon ReadFrom(System.IO.BinaryReader reader)
            {
                var result = new Polygon();

                result.Vertices = new ColoredVertex[reader.ReadInt32()];
                for (int i = 0; i < result.Vertices.Length; i++)
                {
                    result.Vertices[i] = ColoredVertex.ReadFrom(reader);
                }
                return(result);
            }
Example #15
0
 public static (ColoredVertex[], PrimitiveType) Curve(
     IEnumerable <System.Numerics.Vector2> curve,
     Color4 color)
 {
     System.Numerics.Vector2[] array  = curve.ToArray();
     ColoredVertex[]           result = new ColoredVertex[array.Length];
     for (int i = 0; i < result.Length; ++i)
     {
         result[i] = new ColoredVertex(new Vector4(array[i].X, array[i].Y, 0, 1), color);
     }
     return(result, PrimitiveType.LineStrip);
 }
Example #16
0
        public static void DrawRegion(int x, int y, Color color, DebugInterface debugInterface)
        {
            var vertex1 = new ColoredVertex(new Vec2Float(x + 0.25f, y + 0.25f), new Vec2Float(0, 0), color);
            var vertex2 = new ColoredVertex(new Vec2Float(x + 0.75f, y + 0.25f), new Vec2Float(0, 0), color);
            var vertex3 = new ColoredVertex(new Vec2Float(x + 0.75f, y + 0.75f), new Vec2Float(0, 0), color);
            var vertex4 = new ColoredVertex(new Vec2Float(x + 0.25f, y + 0.75f), new Vec2Float(0, 0), color);

            var debugData    = new DebugData.Primitives(new[] { vertex1, vertex2, vertex3, vertex4 }, PrimitiveType.Lines);
            var debugCommand = new DebugCommand.Add(debugData);

            debugInterface.Send(debugCommand);
        }
Example #17
0
        public static ColoredVertex ReadFrom(System.IO.BinaryReader reader)
        {
            Model.Vec2Float?worldPos = null;
            if (reader.ReadBoolean())
            {
                worldPos = Model.Vec2Float.ReadFrom(reader);
            }

            Vec2Float screenOffset = Model.Vec2Float.ReadFrom(reader);
            Color     color        = Model.Color.ReadFrom(reader);
            var       result       = new ColoredVertex(worldPos, screenOffset, color);

            return(result);
        }
        /// <summary>
        /// Creates a circular GameObject. The origin (0,0) is at the center of the GameWindow, and x and y
        /// use an origin at the center of the circle as well.
        /// </summary>
        /// <returns>The created GameObject, stored in the ObjectManager</returns>
        public ref GameObject CreateCircle(int layer, int x, int y, int radius, Color4 color)
        {
            int triangleCount = 100;

            ColoredVertex[] vertices = new ColoredVertex[triangleCount + 1];
            uint[]          indices  = new uint[triangleCount * 3];

            vertices[0] = new ColoredVertex(new Vector4((float)x, (float)y, 1.0f, 1.0f), color);
            for (int i = 1; i <= triangleCount; i++)
            {
                float fx = (float)x + ((float)radius * (float)Math.Cos((double)i * 2d * Math.PI / (double)triangleCount));
                float fy = (float)y + ((float)radius * (float)Math.Sin((double)i * 2d * Math.PI / (double)triangleCount));
                vertices[i] = new ColoredVertex(new Vector4(fx, fy, 1.0f, 1.0f), color);
            }
            for (uint i = 0; i < triangleCount; i++)
            {
                indices[i * 3]     = 0;
                indices[i * 3 + 1] = i + 1;
                if (i == triangleCount - 1)
                {
                    indices[i * 3 + 2] = 1;
                }
                else
                {
                    indices[i * 3 + 2] = i + 2;
                }
            }
            Vector3 position = new Vector3((float)x, (float)y, 0f);
            float   rotX = 0f, rotY = 0f, rotZ = 0f;
            Vector3 scale = new Vector3(1.0f, 1.0f, 0.0f);

            int idx = GetNewGameObjectID();

            _gameObjects[idx] = new GameObject(
                idx,
                1,          // renderID
                layer,
                vertices,
                indices,
                position,
                rotX,
                rotY,
                rotZ,
                scale);

            _gameObjectLayers[idx] = layer;

            return(ref _gameObjects[idx]);
        }
        public override void Setup()
        {
            base.Setup();
            var vertices = new ColoredVertex[]
            {
                new ColoredVertex(new Vector3(0.5f, 0.5f, 0.0f), Color4.AliceBlue),       //Bottom-left vertex
                new ColoredVertex(new Vector3(0.5f, -0.5f, 0.0f), Color4.BlanchedAlmond), //Bottom-right vertex
                new ColoredVertex(new Vector3(-0.5f, -0.5f, 0.0f), Color4.Fuchsia),       //Top vertex
                new ColoredVertex(new Vector3(-0.5f, -0.5f, 0.0f), Color4.Red),
                new ColoredVertex(new Vector3(-0.5f, 0.5f, 0.0f), Color4.Blue),
                new ColoredVertex(new Vector3(0.5f, 0.5f, 0.0f), Color4.Yellow)
            };

            _model = Model.CreateModel(vertices);
        }
Example #20
0
        public void DrawTriangles(Clio.Utilities.Vector3[] verts, Color color)
        {
            if (verts.Length > _vertexBuffer.Length)
            {
                Array.Resize(ref _vertexBuffer, verts.Length);
            }

            for (int i = 0; i < verts.Length; i++)
            {
                _vertexBuffer[i] = new ColoredVertex(verts[i].Convert(), color);
            }

            SetDeclaration();
            Device.DrawUserPrimitives(PrimitiveType.TriangleList, verts.Length / 3, _vertexBuffer);
        }
Example #21
0
        public static ColoredVertex ReadFrom(System.IO.BinaryReader reader)
        {
            var result = new ColoredVertex();

            if (reader.ReadBoolean())
            {
                result.WorldPos = Model.Vec2Float.ReadFrom(reader);
            }
            else
            {
                result.WorldPos = null;
            }
            result.ScreenOffset = Model.Vec2Float.ReadFrom(reader);
            result.Color        = Model.Color.ReadFrom(reader);
            return(result);
        }
Example #22
0
        public void DrawHollowBox(Rect rect, int frameWidth, Color color)
        {
            var p1 = new ColoredVertex(rect.X, rect.Y, color);
            var p2 = new ColoredVertex(rect.X + rect.W, rect.Y, color);
            var p3 = new ColoredVertex(rect.X + rect.W, rect.Y + rect.H, color);
            var p4 = new ColoredVertex(rect.X, rect.Y + rect.H, color);

            var p5 = new ColoredVertex((float)rect.X + frameWidth, (float)rect.Y + frameWidth, color);
            var p6 = new ColoredVertex((float)(rect.X + rect.W) - frameWidth, (float)rect.Y + frameWidth, color);
            var p7 = new ColoredVertex((float)(rect.X + rect.W) - frameWidth, (float)(rect.Y + rect.H) - frameWidth, color);
            var p8 = new ColoredVertex((float)rect.X + frameWidth, (float)(rect.Y + rect.H) - frameWidth, color);

            ColoredVertex[] data = new ColoredVertex[] { p1, p5, p2, p6, p3, p7, p4, p8, p1, p5 };
            dx.SetTexture(0, null);
            SendVerticesToDevice(data, 8, PrimitiveType.TriangleStrip, ColoredVertex.VertexElements);
        }
Example #23
0
 public static (ColoredVertex[], PrimitiveType) FilledCircle(
     float radius,
     Color4 color,
     int precision = 30)
 {
     ColoredVertex[] result = new ColoredVertex[precision + 2];
     result[0] = new ColoredVertex(new Vector4(0, 0, 0, 1), color);
     for (int i = 0; i <= precision; ++i)
     {
         result[i + 1] = new ColoredVertex(
             new Vector4(
                 radius * (float)Math.Cos(2 * i * Math.PI / precision),
                 radius * (float)Math.Sin(2 * i * Math.PI / precision), 0, 1), color);
     }
     return(result, PrimitiveType.TriangleFan);
 }
Example #24
0
        public static new Polygon ReadFrom(System.IO.BinaryReader reader)
        {
            if (reader == null)
            {
                throw new System.ArgumentNullException(nameof(reader));
            }

            var vertices = new ColoredVertex[reader.ReadInt32()];

            for (var i = 0; i < vertices.Length; i++)
            {
                vertices[i] = ColoredVertex.ReadFrom(reader);
            }

            return(new Polygon(vertices));
        }
Example #25
0
 public static (ColoredVertex[], PrimitiveType) CheckMark(
     float width,
     float height,
     Color4 color)
 {
     ColoredVertex[] result = new ColoredVertex[]
     {
         new ColoredVertex(new Vector4(-0.0852f * width, -0.3510f * height, 0f, 1f), color),
         new ColoredVertex(new Vector4(+0.3520f * width, +0.2360f * height, 0f, 1f), color),
         new ColoredVertex(new Vector4(+0.2554f * width, +0.3080f * height, 0f, 1f), color),
         new ColoredVertex(new Vector4(-0.0820f * width, -0.1450f * height, 0f, 1f), color),
         new ColoredVertex(new Vector4(-0.0820f * width, -0.1450f * height, 0f, 1f), color),
         new ColoredVertex(new Vector4(-0.2464f * width, +0.0193f * height, 0f, 1f), color),
         new ColoredVertex(new Vector4(-0.3510f * width, -0.0854f * height, 0f, 1f), color),
     };
     return(result, PrimitiveType.TriangleFan);
 }
Example #26
0
        public static (ColoredVertex[], PrimitiveType) LineCircle(
            float radius,
            Color4 color,
            int precision = 30)
        {
            var res = new ColoredVertex[precision];

            for (int i = 0; i < precision; ++i)
            {
                float angle = MathHelper.TwoPi * i / precision;
                res[i] =
                    new ColoredVertex(
                        new Vector4(radius * (float)Math.Cos(angle), radius * (float)Math.Sin(angle), 0f, 1f),
                        color);
            }
            return(res, PrimitiveType.LineLoop);
        }
Example #27
0
 public static (ColoredVertex[], PrimitiveType) Rectangle(
     float width,
     float height,
     Color4 color)
 {
     width /= 2; height /= 2;
     ColoredVertex[] result = new ColoredVertex[]
     {
         new ColoredVertex(new Vector4(+width, +height, 0, 1), color),
         new ColoredVertex(new Vector4(+width, -height, 0, 1), color),
         new ColoredVertex(new Vector4(-width, +height, 0, 1), color),
         new ColoredVertex(new Vector4(+width, -height, 0, 1), color),
         new ColoredVertex(new Vector4(-width, +height, 0, 1), color),
         new ColoredVertex(new Vector4(-width, -height, 0, 1), color)
     };
     return(result, PrimitiveType.Triangles);
 }
        public override void Setup()
        {
            var data = new ColoredVertex[]
            {
                new ColoredVertex(new Vector3(-0.5f, -0.5f, 0.0f), Color4.AliceBlue),     //Bottom-left vertex
                new ColoredVertex(new Vector3(0.5f, -0.5f, 0.0f), Color4.BlanchedAlmond), //Bottom-right vertex
                new ColoredVertex(new Vector3(0.0f, 0.5f, 0.0f), Color4.Fuchsia)          //Top vertex
            };
            var vertexAttributes = new VertexAttribute[] {
                new VertexAttribute("aPosition", 3, VertexAttribPointerType.Float, sizeof(float) * (3 + 4), 0),
                new VertexAttribute("aColor", 4, VertexAttribPointerType.Float, sizeof(float) * (3 + 4), sizeof(float) * 3)
            };
            var shaderProgram = ShaderProgram.CreateShaderProgram("Assets/vertex.shader", "Assets/frag.shader", vertexAttributes);

            _model = Model.CreateModel(data, shaderProgram);
            base.Setup();
        }
Example #29
0
        public void DrawOutlinedBox(Vector3 center, Vector3 extents, Color color)
        {
            Vector3 min = center - extents;
            Vector3 max = center + extents;

            _vertexBuffer[0] = new ColoredVertex(new Vector3(min.X, max.Y, max.Z), color);
            _vertexBuffer[1] = new ColoredVertex(new Vector3(max.X, max.Y, max.Z), color);
            _vertexBuffer[2] = new ColoredVertex(new Vector3(min.X, min.Y, max.Z), color);
            _vertexBuffer[3] = new ColoredVertex(new Vector3(max.X, min.Y, max.Z), color);
            _vertexBuffer[4] = new ColoredVertex(new Vector3(min.X, min.Y, min.Z), color);
            _vertexBuffer[5] = new ColoredVertex(new Vector3(max.X, min.Y, min.Z), color);
            _vertexBuffer[6] = new ColoredVertex(new Vector3(min.X, max.Y, min.Z), color);
            _vertexBuffer[7] = new ColoredVertex(new Vector3(max.X, max.Y, min.Z), color);

            SetDeclaration();
            Device.DrawIndexedUserPrimitives(PrimitiveType.LineList, 0, 8, 12, s_boxOutlineIndices,
                                             Format.Index16, _vertexBuffer, 16);
        }
Example #30
0
        public static (ColoredVertex[], PrimitiveType) HollowCircle(
            float radius,
            float thickness,
            Color4 color,
            BorderType borderType = BorderType.Inner,
            int precision         = 30)
        {
            float outrad = 0f, inrad = 0f;

            switch (borderType)
            {
            case BorderType.Inner:
                outrad = radius;
                inrad  = radius - thickness;
                break;

            case BorderType.Middle:
                outrad = radius + thickness / 2;
                inrad  = radius - thickness / 2;
                break;

            case BorderType.Outter:
                outrad = radius + thickness;
                inrad  = radius;
                break;

            default:
                throw new ArgumentException("Invalid argument", "borderType");
            }
            ColoredVertex[] result = new ColoredVertex[2 * precision + 2];
            for (int i = 0; i <= precision; ++i)
            {
                result[2 * i + 0] = new ColoredVertex(
                    new Vector4(
                        outrad * (float)Math.Cos(2 * i * Math.PI / precision),
                        outrad * (float)Math.Sin(2 * i * Math.PI / precision), 0, 1), color);
                result[2 * i + 1] = new ColoredVertex(
                    new Vector4(
                        inrad * (float)Math.Cos(2 * i * Math.PI / precision),
                        inrad * (float)Math.Sin(2 * i * Math.PI / precision), 0, 1), color);
            }
            return(result, PrimitiveType.TriangleStrip);
        }
Example #31
0
        public void DrawPolygon(ushort[] indices)
        {
            Color4[] colors = Shader.Method(Shader, Points.Length, Shape.Rectangle);
            ColoredVertex[] vertices = new ColoredVertex[Points.Length];

            //colors[colors.Length - 1] = new Color4(0, 1, 0);

            for (int i = 0; i < Points.Length; i++)
            {
                Vector4 vertex = Points[i];
                vertices[i] = new ColoredVertex(vertex, colors[i]);
            }

            ShapeDescription polygonShape = new ShapeDescription
            {
                Vertices = vertices,
                Indices = indices,
                Primitives = indices.Length / 3,
                Shape = Shape.RectangleMesh
            };

            shapes.Add(polygonShape);
        }
Example #32
0
 public ColoredVertex[] CreateColoredVertexArray(Color4[] colors, float zDepth)
 {
     ColoredVertex[] coloredVertices = new ColoredVertex[Count];
     for (int i = 0; i < Vertices.Count; i++)
     {
         Vector2D vector = Vertices[i];
         coloredVertices[i] = new ColoredVertex(new Vector4(vector, zDepth, 1.0f), colors[i]);
     }
     return coloredVertices;
 }
Example #33
0
        public static ColoredVertex[] CreateEllipseMesh(Vector4 center, float radiusX, float radiusY, int slices, int segments, Color4[] colors,
            out ushort[] indices, float[] ringOffsets=null)
        {
            const float radTo = MathHelper.TwoPi;
            float delta = radTo/slices;
            if (ringOffsets == null)
                ringOffsets = new[] {0.0f, 1.0f};
            int rings = ringOffsets.Length;
            ColoredVertex[] vertices = new ColoredVertex[((rings-1)*slices) + 1];

            vertices[0] = new ColoredVertex(center, colors[0]);

            // First ring vertices
            // ringOffsets[0] is assumed to be the center
            // ringOffsets[1] the first ring
            for (int i=0; i<slices; i++)
            {
                float ringOffset = ringOffsets[1];
                float theta = i*delta;
                Vector4 vertexPos = CreateEllipseVertex(center, theta, radiusX, radiusY, ringOffset);

                vertices[i+1] = new ColoredVertex(vertexPos, colors[i+1]);
            }

            indices = new ushort[3*slices*((2*(rings-2))+1)];

            PolyEllipse.TriangulateEllipseFirstRing(slices, ref indices);

            int indexCount = 0;
            int baseIndex = 3*slices;
            for (int r = 1; r < rings-1; r++)
            {
                // Other rings vertices
                for (int i = 0; i < slices; i++)
                {
                    float ringOffset = ringOffsets[r+1];
                    float theta = i * delta;
                    Vector4 vertexPos = CreateEllipseVertex(center, theta, radiusX, radiusY, ringOffset);

                    vertices[(r*slices) + i+1] = new ColoredVertex(vertexPos, colors[(r*slices) + i + 1]);
                }

                // Other rings indices
                int j = r * slices;
                int k = (r - 1) * slices;

                for (int i = 0; i < slices; i++)
                {
                    // current ring

                    // first face
                    indices[baseIndex + indexCount] = (ushort) (j + i+2);
                    indices[baseIndex + indexCount + 1] = (ushort)(k + i + 1);
                    indices[baseIndex + indexCount + 2] = (ushort)(j + i + 1);
                    // second face
                    indices[baseIndex + indexCount + 5] = (ushort)(k + i + 2);
                    indices[baseIndex + indexCount + 4] = (ushort)(j + i + 2);
                    indices[baseIndex + indexCount + 3] = (ushort)(k + i + 1);
                    indexCount += 6;
                }
                // Wrap faces
                indices[baseIndex + indexCount - 2] = (ushort)((r+1)*slices);
                indices[baseIndex + indexCount - 4] = (ushort)(r*slices+1);
                indices[baseIndex + indexCount - 6] = (ushort)(1+(r-1)*slices);
            }
            return vertices;
        }
Example #34
0
        public static ColoredVertex[] CreateEllipseOutline(Vector4 center, float radiusX, float radiusY, int slices, Color4[] colors)
        {
            const float radTo = MathHelper.TwoPi;
            float delta = radTo / slices;

            ColoredVertex[] vertices = new ColoredVertex[slices];
            for (int i = 0; i < slices; i++)
            {
                float theta = i * delta;
                Vector4 vertexPos = CreateEllipseVertex(center, theta, radiusX, radiusY);
                vertices[i] = new ColoredVertex(vertexPos, colors[i + 1]);
            }

            return vertices;
        }
Example #35
0
        public static ColoredVertex[] CreateEquilateralTriangle(Vector4 leftVertex, float sideLength, Color4[] colors,
            bool isTriangleUpside, out ushort[] indices)
        {
            ColoredVertex[] vertices = new ColoredVertex[3];
            // Left vertex
            vertices[0] = new ColoredVertex
                              {
                                  Position = new Vector4(leftVertex.X, leftVertex.Y, leftVertex.Z, 1.0f),
                                  Color = colors[0]
                              };
            // Right vertex
            vertices[1] = new ColoredVertex
                              {
                                  Position = new Vector4(leftVertex.X + sideLength, leftVertex.Y, leftVertex.Z, 1.0f),
                                  Color = colors[1]
                              };

            float height = (float) (sideLength/2*Math.Sqrt(3));
            height *= isTriangleUpside ? +1 : -1;

            // Top/Bottom vertex
            vertices[2] = new ColoredVertex
                              {
                                  Position = new Vector4(leftVertex.X+sideLength/2, leftVertex.Y + height, leftVertex.Z, 1.0f),
                                  Color = colors[2]
                              };

            indices = isTriangleUpside
                          ? new ushort[] {0, 1, 2}
                          : new ushort[] {1, 0, 2};
            return vertices;
        }
Example #36
0
        public static ColoredVertex[] CreateLineMesh(float width, Color4 color, Vector4 v1, Vector4 v2, out ushort[] indices, ushort baseIndex = (ushort) 0)
        {
            ColoredVertex[] vertices = new ColoredVertex[4];
            float z = v1.Z;
            const float w = 1.0f;
            Vector2 v1i = new Vector2(v1.X, v1.Y);
            Vector2 v2i = new Vector2(v2.X, v2.Y);

            Vector2 vDir = (v1i - v2i);
            vDir = new Vector2(-vDir.Y, vDir.X);//vDir.Perp();
            vDir.Normalize();
            float vLength = (float)Math.Sqrt(vDir.X * vDir.X + vDir.Y * vDir.Y);
            vDir = new Vector2(vDir.X / vLength, vDir.Y / vLength);
            width /= 2;

            Vector2 vTopRight = v1i + (-width * vDir);
            Vector2 vTopLeft = v1i + (width * vDir);
            Vector2 vBottomRight = v2i + (-width * vDir);
            Vector2 vBottomLeft = v2i + (width * vDir);

            vertices[0] = new ColoredVertex(new Vector4(vTopLeft,z, w), color );
            vertices[1] = new ColoredVertex(new Vector4(vTopRight, z, w), color);
            vertices[2] = new ColoredVertex(new Vector4(vBottomRight, z, w), color);
            vertices[3] = new ColoredVertex(new Vector4(vBottomLeft, z, w), color);

            // Top left 0
            // Top right 1
            // Bottom right 2
            // Bottom left 3
            indices = new ushort[]
                      {
                          1, 0, 3,
                          2, 1, 3
                      };

            if (baseIndex>0)
                for (int i = 0; i < indices.Length; i++)
                {
                    indices[i] += baseIndex;
                }

            return vertices;
        }
Example #37
0
        public static ColoredVertex[] CreateQuad(Vector4 topLeftVertex, float width, float height, Color4[] colors,
            out ushort[] indices)
        {
            ColoredVertex[] vertices = new ColoredVertex[4];
            // Top left 0
            vertices[0] = new ColoredVertex
            {
                Position =
                    new Vector4(topLeftVertex.X, topLeftVertex.Y,
                    topLeftVertex.Z, 1.0f),
                Color = colors[0]
            };

            // Top right 1
            vertices[1] = new ColoredVertex
            {
                Position =
                    new Vector4(topLeftVertex.X + width, topLeftVertex.Y,
                    topLeftVertex.Z, 1.0f),
                Color = colors[1]
            };
            // Bottom right 2
            vertices[2] = new ColoredVertex
            {
                Position =
                    new Vector4(topLeftVertex.X + width, topLeftVertex.Y - height,
                    topLeftVertex.Z, 1.0f),
                Color = colors[2]
            };

            // Bottom left 3
            vertices[3] = new ColoredVertex
            {
                Position =
                    new Vector4(topLeftVertex.X, topLeftVertex.Y - height,
                    topLeftVertex.Z, 1.0f),
                Color = colors[3]
            };

            // Top left 0
            // Top right 1
            // Bottom right 2
            // Bottom left 3
            indices = new ushort[]
                          {
                              1, 0, 3,
                              2, 1, 3
                          };

            return vertices;
        }
Example #38
0
        public static ColoredVertex[] DrawPolyLine(int width, Color4[] colors, bool closed, IEnumerable<Vector4> points, out ushort[] indices)
        {
            Vector4[] pointsArray = points.ToArray();
            int length = closed ? pointsArray.Length : pointsArray.Length - 1;

            ColoredVertex[] vertices = new ColoredVertex[4*length];
            indices = new ushort[6*length];
            ushort vertexCount = 0;
            ushort indexCount = 0;
            Vector4 v1 = pointsArray[0];
            for (int i = 1; i <= length; i++)
            {
                int index = i < length ? i : 0;
                Vector4 v2 = pointsArray[index];
                Color4 color = colors[index];
                ushort[] tempIndices;
                Array.Copy(CreateLineMesh(width, color, v1, v2, out tempIndices, vertexCount), 0, vertices, vertexCount, 4);
                Array.Copy(tempIndices, 0, indices, indexCount, 6);
                vertexCount += 4;
                indexCount += 6;
                v1 = v2;
            }

            return vertices;
        }
Example #39
0
        public static ColoredVertex[] CreateRectangleMesh(Vector4 topLeftVertex, float width, float height, int widthSegments, int heightSegments,
            Color4[] colors,
            out ushort[] indices,
            float[] widthOffsets=null, float[] heightOffsets=null)
        {
            if (widthOffsets == null)
                widthOffsets = CreateOffsetsArray(widthSegments);
            if (heightOffsets == null)
                heightOffsets = CreateOffsetsArray(heightSegments);

            Vector4[] vertices = CreateRectangleMesh(topLeftVertex, width, height, out indices,
                                                     widthOffsets, heightOffsets);

            ColoredVertex[] coloredVertices = new ColoredVertex[vertices.Length];

            for (int i = 0; i < vertices.Length; i++)
            {
                coloredVertices[i] = new ColoredVertex(vertices[i], colors[i]);
            }

            return coloredVertices;
        }
Example #40
0
        public void BuildInterfaceMesh()
        {
            int hiddenVertices = 0;
            int hiddenIndices = 0;
            foreach (ShapeDescription sDesc in
                TreeTraversal.PreOrderHiddenControlsVisit(this).Where(control => control.Description.Shape != Shape.None)
                    .SelectMany(control => control.Shapes))
            {
                hiddenVertices += sDesc.Vertices.Length;
                hiddenIndices += sDesc.Indices.Length;
            }

            if (hudInterface.Vertices.Length == 0)
                throw Error.InvalidOperation(Properties.Resources.ERR_HudNoControls);

            Shapes[0] = hudInterface;
            ColoredVertex[] vertices = new ColoredVertex[Shapes[0].Vertices.Length + hiddenVertices];
            ushort[] indices = new ushort[Shapes[0].Indices.Length + hiddenIndices];
            Array.Copy(Shapes[0].Vertices, vertices, Shapes[0].Vertices.Length);
            Array.Copy(Shapes[0].Indices, indices, Shapes[0].Indices.Length);

            if (InterfaceMesh != null)
                InterfaceMesh.Rebuild(vertices, indices);
            else
                InterfaceMesh = new InterfaceMesh(vertices, indices) {OwnerHud = this};

            OnLoad(EventArgs.Empty);
        }