/// <summary>
 /// Initializes a new instance of the <see cref="TreeMaterial"/> class.
 /// </summary>
 /// <param name="shader">The shader.</param>
 public TreeMaterial(ForwardTreeShader shader)
 {
     wsource = new WindStrengthSin();
     this.animator = new TreeWindAnimator(wsource);
     this.shader = shader;
     CanAppearOfReflectionRefraction = true;
     CanCreateShadow = true;
     IsVisible = true;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="TreeMaterial"/> class.
 /// </summary>
 /// <param name="shader">The shader.</param>
 /// <param name="wsource">The wsource.</param>
 public TreeMaterial(ForwardTreeShader shader, WindSource wsource)
 {
     this.wsource = wsource;
     this.shader = shader;
     this.animator = new TreeWindAnimator(wsource);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="TreeMaterial"/> class.
 /// </summary>
 /// <param name="shader">The shader.</param>
 public TreeMaterial(ForwardTreeShader shader)
 {
     wsource = new WindStrengthSin();
     this.animator = new TreeWindAnimator(wsource);
     this.shader = shader;
 }
        public override void Initialize()
        {
            _treeProfile = Content.Load<TreeProfile>("Trees/Graywood");
            _tree = new SunlitSimpleTree(_treeProfile.GenerateSimpleTree());
            _tree.Lighting = _sky;

            // Scale tree down to a desired height
            const int desiredHeight = 6;
            float treeModelHeight = _tree.TrunkMesh.BoundingSphere.Radius*2;
            _scale = Matrix.CreateScale(desiredHeight/treeModelHeight);
            Position = _position;

            _wind = new WindStrengthSin();
            _animator = new TreeWindAnimator(_wind);

            base.Initialize();
        }
        /// <summary>
        /// Load your graphics content.
        /// </summary>
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // Calculate the projection matrix.
            Viewport viewport = GraphicsDevice.Viewport;

            float aspectRatio = (float)viewport.Width / (float)viewport.Height;

            projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
                                                                    aspectRatio,
                                                                    1, 10000);

            terrain = Content.Load<Model>("terrain");
            BasicDirectionalLight terrainDirectionalLight = null;
            directionToSun = Vector3.Normalize(new Vector3(2f, 0.2f, -1.0f));

            foreach (ModelMesh mesh in terrain.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.Projection = projection;

                    effect.PreferPerPixelLighting = true;

                    // Set the specular lighting to match the sky color.
                    effect.SpecularColor = new Vector3(0f);

                    // Set the fog to match the distant mountains
                    // that are drawn into the sky texture.
                    effect.FogEnabled = false;
                    effect.FogColor = new Vector3(0.15f);
                    effect.FogStart = 100;
                    effect.FogEnd = 320;

                    effect.EnableDefaultLighting();

                    effect.AmbientLightColor = new Vector3(0.5f, 0.5f, 0.5f);
                    effect.DirectionalLight1.Enabled = false;
                    effect.DirectionalLight2.Enabled = false;
                    effect.DirectionalLight0.Direction = -directionToSun;
                    effect.DiffuseColor = new Vector3(1f, 1f, 1f);
                    terrainDirectionalLight = effect.DirectionalLight0;
                }
            }

            heightmap = Content.Load<Texture2D>("Textures/terrain");
            CalculateHeightValues();

            sky = Content.Load<Sky>("skybox");
            sunTexture = Content.Load<Texture2D>("Textures/Sun");

            backbufferWidth = graphics.PreferredBackBufferWidth;
            backbufferHeight = graphics.PreferredBackBufferHeight;

            lightScatterPostProcess = Content.Load<Effect>("Effects/LightScatterPostProcess");

            //Setup post-process parameters
            lightScatterPostProcess.Parameters["Density"].SetValue(0.95f);
            lightScatterPostProcess.Parameters["Weight"].SetValue(1f / 120f * 1.2f);
            lightScatterPostProcess.Parameters["Decay"].SetValue(0.991f);
            lightScatterPostProcess.Parameters["Exposure"].SetValue(0.5f);

            //Trees
            Random r = new Random(Environment.TickCount);
            treeProfile = Content.Load<TreeProfile>("Trees/Willow");

            wind = new WindStrengthSin(0.1f);
            windAnimator = new TreeWindAnimator(wind);

            trees = new LinkedList<Tree>();

            for (int i = 0; i < 300; i++)
            {
                SimpleTree simpleTree = treeProfile.GenerateSimpleTree();
                simpleTree.LeafEffect.CurrentTechnique = simpleTree.LeafEffect.Techniques["SetNoRenderStates"];

                simpleTree.TrunkEffect.Parameters["DirLight0DiffuseColor"].SetValue(new Vector4(terrainDirectionalLight.DiffuseColor, 1f));
                simpleTree.TrunkEffect.Parameters["DirLight0Direction"].SetValue(terrainDirectionalLight.Direction);
                simpleTree.TrunkEffect.Parameters["DirLight1Enabled"].SetValue(false);

                simpleTree.LeafEffect.Parameters["DirLight0DiffuseColor"].SetValue(new Vector4(terrainDirectionalLight.DiffuseColor, 1f));
                simpleTree.LeafEffect.Parameters["DirLight0Direction"].SetValue(terrainDirectionalLight.Direction);
                simpleTree.LeafEffect.Parameters["DirLight1Enabled"].SetValue(false);

                int x = r.Next((int)(terrainWidth / terrainScale));
                int z = r.Next((int)(terrainHeight / terrainScale));
                float y = heightValues[x, z];

                x *= (int)terrainScale;
                z *= (int)terrainScale;

                Vector3 position = new Vector3(x, y, z) - new Vector3(terrainWidth * 0.5f, 0f, terrainHeight * 0.5f);
                Matrix scale = Matrix.CreateScale(Vector3.Lerp(new Vector3(0.005f), new Vector3(0.015f), (float)r.NextDouble()));

                trees.AddLast(new Tree(simpleTree, scale * Matrix.CreateTranslation(position)));
            }

            //Render targets
            sceneRenderTarget = new RenderTarget2D(GraphicsDevice, backbufferWidth, backbufferHeight,
                1, SurfaceFormat.Color, GraphicsDevice.PresentationParameters.MultiSampleType,
                GraphicsDevice.PresentationParameters.MultiSampleQuality);
            lightScatterRenderTarget = new RenderTarget2D(GraphicsDevice, backbufferWidth, backbufferHeight,
                1, SurfaceFormat.Color, GraphicsDevice.PresentationParameters.MultiSampleType,
                GraphicsDevice.PresentationParameters.MultiSampleQuality);

            //FPS
            fpsFont = Content.Load<SpriteFont>("Fonts/Verdana");
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="TreeMaterial"/> class.
 /// </summary>
 /// <param name="shader">The shader.</param>
 /// <param name="wsource">The wsource.</param>
 public TreeMaterial(ForwardTreeShader shader, WindSource wsource)
 {
     this.wsource = wsource;
     this.shader = shader;
     this.animator = new TreeWindAnimator(wsource);
     CanAppearOfReflectionRefraction = true;
     CanCreateShadow = true;
     IsVisible = true;
 }
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            camerafps = new FPSCameraController(camera, GraphicsDevice);
            grid = new Grid(GraphicsDevice);

            LoadTreeGenerators();
            NewTree();

            font = Content.Load<SpriteFont>("Fonts/Font");

            // Create a wind animation. The WindStrengthSin class is a rather crude but simple wind generator
            wind = new WindStrengthSin();
            animator = new TreeWindAnimator(wind);
        }
        protected override void Initialize()
        {
            content = new ContentManager(Services, "Content");

            // Load all trees in the Content/Trees folder
            string[] files = Directory.GetFiles("Content/Trees", "*.xnb", SearchOption.TopDirectoryOnly);
            foreach (string filename in files)
            {
                string assetName = filename.Substring("Content/".Length, filename.Length - "Content/.xnb".Length);
                Profiles.Add(content.Load<TreeProfile>(assetName));
                ProfileNames.Add(Path.GetFileName(assetName));
            }
            profileIndex = 0;

            // Create the wind animator
            wind = new WindStrengthSin();
            animator = new TreeWindAnimator(wind);

            // Create the ground plane and an effect for it
            groundPlane = new Quad(GraphicsDevice, 10000, 10000);
            groundEffect = new BasicEffect(GraphicsDevice, new EffectPool());
            groundEffect.Texture = content.Load<Texture2D>("Textures/Grass");
            groundEffect.TextureEnabled = true;

            // Create a camera
            Camera = new Camera();
            Camera.Position = new Vector3(4000, 4000, 4000);
            Camera.Target = new Vector3(0, 2000, 0);
            Camera.AspectRatio = GraphicsDevice.Viewport.Width / (float)GraphicsDevice.Viewport.Height;

            CameraOrbitAngle = 0.0f;
            CameraPitchAngle = -10.0f;
            CameraDistance = 5000.0f;

            // Enable mipmaps
            GraphicsDevice.SamplerStates[0].MipFilter = TextureFilter.Linear;

            // Store the initial renderstate
            block = new StateBlock(GraphicsDevice);
            block.Capture();

            Initialized = true;

            UpdateTree();

            Application.Idle += new EventHandler(Application_Idle);
        }