Example #1
0
        public PrimitiveDemo(Layer layer) : base(layer)
        {
            // Primitives can only be drawn from instances. There are no static methods.
            _trianglefan          = new TriangleFanPrimitive();
            _trianglefan.Vertices = new List <Vertex>
            {
                new Vertex(new Vector2(0, 0)),
                new Vertex(new Vector2(16, 0)),
                new Vertex(new Vector2(40, 10)),
                new Vertex(new Vector2(64, 64)),
                new Vertex(new Vector2(0, 32)),
            };


            _trianglestrip          = new TriangleStripPrimitive();
            _trianglestrip.Vertices = new List <Vertex>
            {
                new Vertex(new Vector2(0, 0), _mainColor),
                new Vertex(new Vector2(32, 32), _secondaryColor),
                new Vertex(new Vector2(64, 0), _secondaryColor2),
                new Vertex(new Vector2(96, 32), _secondaryColor),
                new Vertex(new Vector2(64 + 32, 0), _secondaryColor2),
                new Vertex(new Vector2(96 + 32, 32), _secondaryColor),
                new Vertex(new Vector2(64 + 64, 0), _secondaryColor2),
                new Vertex(new Vector2(96 + 64, 32), _mainColor),
            };

            _mesh          = new MeshPrimitive(16, 16);
            _mesh.Position = new Vector2(0, 100);

            // You can set the texture for a primitive. Preferrably it shouldn't be in texture atlas.
            // If in atlas, textures wouldn't be able to repeat.
            _mesh.SetTextureFromFrame(Default.AutismCat[0]);
            _mesh.Vertices = new List <Vertex>(8 * 8);

            for (var k = 0; k < _mesh.Height; k += 1)
            {
                for (var i = 0; i < _mesh.Width; i += 1)
                {
                    _mesh.Vertices.Add(
                        new Vertex(
                            Vector2.Zero,                             // Positions will be set later.
                            Color.White,
                            new Vector2(i / (float)(_mesh.Width - 1), k / (float)(_mesh.Height - 1)) * _meshRepeat
                            )
                        );
                }
            }


            _linestrip = new LineStripPrimitive();

            _linestrip.Vertices = new List <Vertex>();

            for (var i = 0; i < 16; i += 1)
            {
                _linestrip.Vertices.Add(new Vertex(Vector2.Zero, new Color(16 * i, 8 * i, 4 * i)));
            }


            // You can make your own custom primitives.
            // CustomTrianglePrimitive and CustomLinePrimitive give you
            // access to the index array. Index array tells inwhat order vertices should be drawn.
            // One vertex can be used multiple times in an index array.
            _custom          = new CustomTrianglePrimitive();
            _custom.Vertices = new List <Vertex>()
            {
                new Vertex(new Vector2(0, 0), Color.Green),
                new Vertex(new Vector2(32, 0)),
                new Vertex(new Vector2(32, 32)),
                new Vertex(new Vector2(64, 32), Color.Blue),
            };
            _custom.Indices = new short[] { 0, 1, 2, 1, 3, 2 };
        }