Example #1
0
        public void DrawSquare(
            HSBColor idColor,
            Matrix world,
            Vector3 pt0,
            Vector3 pt1,
            Vector3 pt2,
            Vector3 pt3)
        {
            var rgba = idColor.ToColor();
            var temp = (VertexPositionColorTexture[])unitSquare.Clone();

            temp[0].Position = pt0;
            temp[0].Color    = rgba;
            temp[1].Position = pt1;
            temp[1].Color    = rgba;
            temp[2].Position = pt2;
            temp[2].Color    = rgba;

            temp[3].Position = pt0;
            temp[3].Color    = rgba;
            temp[4].Position = pt2;
            temp[4].Color    = rgba;
            temp[5].Position = pt3;
            temp[5].Color    = rgba;

            this.worldMatrix.SetValue(world);
            this.shapesEffect.CurrentTechnique = this.squareTechnique;
            foreach (
                EffectPass pass in
                this.shapesEffect.CurrentTechnique.Passes
                )
            {
                pass.Apply();
                this.device.DrawUserPrimitives(
                    PrimitiveType.TriangleList,
                    temp,
                    0,
                    2
                    );
            }
        }
Example #2
0
        public TrunkNode(
            NodeDNA dna,
            Vector3 origin,
            int depth,
            float angle,
            float width)
        {
            this.dna    = dna;
            this.origin = origin;
            this.color  = HSBColors.Brown; //makeIDColor(dna, 0);
            this.angle  = angle;
            this.depth  = depth;

            this.width  = width;
            this.length =
                this.width *
                MathHelper.Lerp(
                    MinTrunkLengthScale,
                    MaxTrunkLengthScale,
                    dna.Bushiness
                    );
        }
Example #3
0
        protected override void Draw(GameTime gameTime)
        {
            var background = new HSBColor(0.55f, 0.1f, 1f, 1);

            GraphicsDevice.Clear(background.ToColor());
            GraphicsDevice.RasterizerState = RasterizerState.CullNone;
            GraphicsDevice.BlendState      = BlendState.AlphaBlend;

            Matrix view = Matrix.CreateLookAt(
                new Vector3(0, 0, -10),
                new Vector3(0, 0, 0),
                Vector3.UnitY) * Matrix.CreateScale(0.75f);
            Matrix projection = Matrix.CreateOrthographic(
                GraphicsDevice.Viewport.Width,
                GraphicsDevice.Viewport.Height,
                0.0f,
                100.0f);

            this.drawingContext.View       = view;
            this.drawingContext.Projection = projection;

            this.node.Draw(this.drawingContext);
        }
Example #4
0
        public Node(Node parent, float childPct)
        {
            this.id     = NextID++;
            this.parent = parent;

            if (parent != null)
            {
                this.depth = parent.depth + 1;
                this.dna   = parent.dna;

                // As a child, offset the child index
                var skew   = (float)Math.Pow(this.dna.AngleSkew - .5f, 3);
                var spread = (2f * this.dna.Bushiness);
                this.baseAngle  = parent.angle + spread * (childPct - .5f) + skew;
                this.baseAngle +=
                    this.dna.Wiggle * .1f * MathF.Sin(this.depth) * this.depth;

                // Set the position relative to the parent
                var mult = 15 - 12 * this.dna.Bushiness;
                this.branchLength = .7f * mult * parent.radius;

                this.branchLength *=
                    (1 + 1 * this.dna.Variation * (NodeDNA.NextFloat() - .5f));
                this.radius = parent.radius * (.6f + .3f * this.dna.Shrinkage);

                this.position =
                    MathF.PolarOffset(
                        parent.position,
                        this.branchLength,
                        this.baseAngle
                        );
            }

            this.angle   = this.baseAngle;
            this.idColor = makeIDColor(this.dna, this.depth);
        }
Example #5
0
        public void DrawCircle(HSBColor idColor, Vector3 position, float radius)
        {
            // TODO: Batch/cache.
            var rgba = idColor.ToColor();
            var temp = (VertexPositionColorTexture[])unitSquare.Clone();

            for (int i = 0; i < temp.Length; i++)
            {
                temp[i].Color = rgba;
            }

            Matrix world =
                Matrix.CreateScale(radius * 2) *
                Matrix.CreateTranslation(position);

            this.worldMatrix.SetValue(world);
            this.shapesEffect.CurrentTechnique = this.circleTechnique;
            foreach (EffectPass pass in this.shapesEffect.CurrentTechnique.Passes)
            {
                pass.Apply();
                this.device.DrawUserPrimitives(
                    PrimitiveType.TriangleList, temp, 0, 2);
            }
        }
Example #6
0
        public void Draw(DrawingContext context)
        {
            foreach (var child in this.children)
            {
                Vector3 edge   = child.position - position;
                float   length = child.branchLength;
                float   angle  = (float)Math.Atan2(edge.Y, edge.X);

                Matrix world = Matrix.CreateRotationZ(angle) *
                               Matrix.CreateTranslation(this.position);

                context.DrawSquare(
                    this.idColor,
                    world,
                    new Vector3(0, -this.radius, 0),
                    new Vector3(0, +this.radius, 0),
                    new Vector3(length, +child.radius, 0),
                    new Vector3(length, -child.radius, 0)
                    );

                var leafCount = (float)Math.Floor(this.dna.LeafCount * 5);
                for (var j = 0; j < (int)leafCount; j++)
                {
                    HSBColor leafColor = this.idColor.Alter(
                        shade: 0.3f * MathF.Sin(j + this.depth),
                        fade: -0.3f + 0.2f * MathF.Sin(j + this.depth)
                        );

                    world =
                        Matrix.CreateTranslation(length / leafCount, 0, 0) *
                        world;

                    var r0     = 15 * this.radius * (.3f + this.dna.LeafAspect);
                    var r1     = r0 * (.7f * this.dna.LeafShape + .12f);
                    var theta  = MathF.Sin(j * 3 + this.depth);
                    var dTheta = 1 / (.8f + 2 * this.dna.LeafAspect);
                    var theta0 = theta - dTheta;
                    var theta1 = theta + dTheta;

                    context.DrawSquare(
                        leafColor,
                        world,
                        new Vector3(0, 0, 0),
                        new Vector3(
                            r1 * MathF.Cos(theta0),
                            r1 * MathF.Sin(theta),
                            0
                            ),
                        new Vector3(
                            r0 * MathF.Cos(theta),
                            r0 * MathF.Sin(theta),
                            0
                            ),
                        new Vector3(
                            r1 * MathF.Cos(theta1),
                            r1 * MathF.Sin(theta1),
                            0
                            )
                        );
                }

                child.Draw(context);
            }

            context.DrawCircle(this.idColor, this.position, this.radius);
            if (this.children.Count == 0)
            {
                Matrix world = Matrix.CreateRotationZ(this.angle) *
                               Matrix.CreateTranslation(this.position);

                var flowerCount = (float)Math.Round(8 * this.dna.FlowerCount);
                var petalSize   = 5 * this.radius;

                var aspect = .1f + .9f * this.dna.PetalAspect;
                var petalH = petalSize * aspect;
                var petalW = petalSize * (1 - aspect);
                for (var i = 0; i < flowerCount; i++)
                {
                    var flowerColor = new HSBColor(
                        (this.dna.FlowerHue * 1.2f + .9f) % 1f,
                        this.dna.FlowerSaturation,
                        MathF.Unit(.9f + .3f * MathF.Sin(i * 3)),
                        .7f
                        );

                    world =
                        Matrix.CreateRotationZ(MathHelper.TwoPi / flowerCount) *
                        world;

                    var flowerCenter = Vector3.Transform(
                        new Vector3(petalH * 1.5f, 0, 0),
                        world
                        );
                    context.DrawElipse(
                        flowerColor,
                        flowerCenter,
                        petalH,
                        petalW
                        );
                }
            }
        }