예제 #1
0
        /// <summary>
        /// Loads graphics resources needed for the game.
        /// </summary>
        public override void LoadGraphicsContent(bool loadAllContent)
        {
            // Initialize the camera

            camera = new FirstPersonCamera(GameOptions.CameraAccelerationMagnitude, GameOptions.CameraRotationSpeed,
                                           GameOptions.CameraVelocityDecayRate);
            camera.AspectRatio = (float)ScreenManager.GraphicsDevice.Viewport.Width /
                                    (float)ScreenManager.GraphicsDevice.Viewport.Height;
            camera.Position = new Vector3(0, GameOptions.TerrainMaxHeight, 0);
            camera.Angles = new Vector3(-MathHelper.PiOver2, 0.0f, 0.0f);

            if (loadAllContent)
            {
                // Load the sky box
                skyBox = new SkyBox(ScreenManager.Game, ScreenManager.Content, camera);

                // Set up the terrain parameters
                TerrainQuadTreeParameters parameters = new TerrainQuadTreeParameters();

                parameters.HeightMapName = "Content\\Textures\\Heightmap";
                parameters.LayerMap0Name = "Content\\Textures\\grass01";
                parameters.LayerMap1Name = "Content\\Textures\\rock01";
                parameters.LayerMap2Name = "Content\\Textures\\snow01";
                parameters.GrassTextureName = "Content\\Textures\\grass";

                parameters.MaxHeight = GameOptions.TerrainMaxHeight;
                parameters.TerrainScale = GameOptions.TerrainScale;

                parameters.MaxScreenSpaceError = 0.075f;
                parameters.ScreenHeight = ScreenManager.Game.GraphicsDevice.Viewport.Height;
                parameters.FieldOfView = camera.FieldOfView;

                parameters.GrassChunkDimension = 30;
                parameters.GrassChunkStarSeparation = 266.6f;
                parameters.GrassStartFadingInDistance = 4000.0f;
                parameters.GrassStopFadingInDistance = 3900.0f;

                parameters.WindStrength = 100.0f;
                parameters.WindDirection = new Vector4(-1.0f, 0.0f, 0.0f, 0.0f);

                parameters.DoPreprocessing = true;

                parameters.ComputePerspectiveScalingFactor();

                terrain = new TerrainQuadTree(ScreenManager.Game, ScreenManager.Content, parameters);

                // Iniialize the water manager
                waterManager = new WaterManager(ScreenManager.Game, GameOptions.WaterHeight);
            }
        }
예제 #2
0
        /// <summary>
        /// Loads graphics resources needed for the game.
        /// </summary>
        public override void LoadGraphicsContent(bool loadAllContent)
        {
            // Initialize the camera

            camera = new FirstPersonCamera(GameOptions.CameraAccelerationMagnitude, GameOptions.CameraRotationSpeed,
                                           GameOptions.CameraVelocityDecayRate);
            camera.AspectRatio = (float)ScreenManager.GraphicsDevice.Viewport.Width /
                                 (float)ScreenManager.GraphicsDevice.Viewport.Height;
            camera.Position = new Vector3(0, GameOptions.TerrainMaxHeight, 0);
            camera.Angles   = new Vector3(-MathHelper.PiOver2, 0.0f, 0.0f);

            if (loadAllContent)
            {
                // Load the sky box
                skyBox = new SkyBox(ScreenManager.Game, ScreenManager.Content, camera);

                // Set up the terrain parameters
                TerrainQuadTreeParameters parameters = new TerrainQuadTreeParameters();

                parameters.HeightMapName    = "Content\\Textures\\Heightmap";
                parameters.LayerMap0Name    = "Content\\Textures\\grass01";
                parameters.LayerMap1Name    = "Content\\Textures\\rock01";
                parameters.LayerMap2Name    = "Content\\Textures\\snow01";
                parameters.GrassTextureName = "Content\\Textures\\grass";

                parameters.MaxHeight    = GameOptions.TerrainMaxHeight;
                parameters.TerrainScale = GameOptions.TerrainScale;

                parameters.MaxScreenSpaceError = 0.075f;
                parameters.ScreenHeight        = ScreenManager.Game.GraphicsDevice.Viewport.Height;
                parameters.FieldOfView         = camera.FieldOfView;

                parameters.GrassChunkDimension        = 30;
                parameters.GrassChunkStarSeparation   = 266.6f;
                parameters.GrassStartFadingInDistance = 4000.0f;
                parameters.GrassStopFadingInDistance  = 3900.0f;

                parameters.WindStrength  = 100.0f;
                parameters.WindDirection = new Vector4(-1.0f, 0.0f, 0.0f, 0.0f);

                parameters.DoPreprocessing = true;

                parameters.ComputePerspectiveScalingFactor();

                terrain = new TerrainQuadTree(ScreenManager.Game, ScreenManager.Content, parameters);

                // Iniialize the water manager
                waterManager = new WaterManager(ScreenManager.Game, GameOptions.WaterHeight);
            }
        }
예제 #3
0
        /// <summary>
        /// Loads a new set of parameters from an XML file.
        /// </summary>
        public static TerrainQuadTreeParameters Load(string filename)
        {
            FileStream stream = null;
            TerrainQuadTreeParameters paramaters = null;

            try
            {
                stream = File.OpenRead(filename);
                XmlSerializer serializer = new XmlSerializer(typeof(TerrainQuadTreeParameters));
                paramaters = (TerrainQuadTreeParameters)serializer.Deserialize(stream);
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }

            return(paramaters);
        }
예제 #4
0
        /// <summary>
        /// This initializes a new instance of TerrainQuadTree. It automatically loads all graphics resources,
        /// so it must be called at an appropriate time.
        /// </summary>
        public TerrainQuadTree(Game game, ContentManager content, TerrainQuadTreeParameters parameters)
        {
            this.game       = game;
            this.parameters = parameters;

            heightMap    = content.Load <Texture2D>(parameters.HeightMapName);
            layerMap0    = content.Load <Texture2D>(parameters.LayerMap0Name);
            layerMap1    = content.Load <Texture2D>(parameters.LayerMap1Name);
            layerMap2    = content.Load <Texture2D>(parameters.LayerMap2Name);
            grassTexture = content.Load <Texture2D>(parameters.GrassTextureName);

            terrainEffect = content.Load <Effect>("Content\\Shaders\\Terrain");
            grassEffect   = content.Load <Effect>("Content\\Shaders\\Grass");

            this.heightMapResolution = heightMap.Width;

            grid = new Grid(game, 1.0f / 16.0f, 16);

            ComputeNormalMap();

            // Due to a bug with XNA 2.0, it keeps the height map data locked after creating the normal map.
            // The only solution I found was to set the graphics device's reference to the texture to null.

            game.GraphicsDevice.Textures[0] = null;

            // About 99% of the preprocessing time is spent simply doing the detailed computations necessary
            // to determine the geometric errors of the chunks. Therefore, instead of serializing the entire
            // quadtree to speed up initialization, just these values are saved.

            if (!parameters.DoPreprocessing)
            {
                // Load the geometric errors from the chunk data file.

                Stream          stream    = File.Open("Chunks.dat", FileMode.Open);
                BinaryFormatter formatter = new BinaryFormatter();


                chunkGeometricErrors = (Dictionary <int, float>)formatter.Deserialize(stream);
                stream.Close();
            }
            else
            {
                chunkGeometricErrors = new Dictionary <int, float>();
            }

            currentChunkIndex = 0;

            ConstructQuadTree();

            if (parameters.DoPreprocessing)
            {
                // Save the geometric errors to the chunk data file.

                Stream          stream    = File.Open("Chunks.dat", FileMode.Create);
                BinaryFormatter formatter = new BinaryFormatter();

                formatter.Serialize(stream, chunkGeometricErrors);
                stream.Close();
            }

            grassChunk = new GrassChunk(game, this, parameters.GrassChunkDimension, parameters.GrassChunkStarSeparation);
            GenerateGrassChunkPositions();
        }
예제 #5
0
        /// <summary>
        /// This initializes a new instance of TerrainQuadTree. It automatically loads all graphics resources,
        /// so it must be called at an appropriate time.
        /// </summary>
        public TerrainQuadTree(Game game, ContentManager content, TerrainQuadTreeParameters parameters)
        {
            this.game = game;
            this.parameters = parameters;

            heightMap = content.Load<Texture2D>(parameters.HeightMapName);
            layerMap0 = content.Load<Texture2D>(parameters.LayerMap0Name);
            layerMap1 = content.Load<Texture2D>(parameters.LayerMap1Name);
            layerMap2 = content.Load<Texture2D>(parameters.LayerMap2Name);
            grassTexture = content.Load<Texture2D>(parameters.GrassTextureName);

            terrainEffect = content.Load<Effect>("Content\\Shaders\\Terrain");
            grassEffect = content.Load<Effect>("Content\\Shaders\\Grass");

            this.heightMapResolution = heightMap.Width;

            grid = new Grid(game, 1.0f / 16.0f, 16);

            ComputeNormalMap();

            // Due to a bug with XNA 2.0, it keeps the height map data locked after creating the normal map.
            // The only solution I found was to set the graphics device's reference to the texture to null.

            game.GraphicsDevice.Textures[0] = null;

            // About 99% of the preprocessing time is spent simply doing the detailed computations necessary
            // to determine the geometric errors of the chunks. Therefore, instead of serializing the entire
            // quadtree to speed up initialization, just these values are saved.

            if (!parameters.DoPreprocessing)
            {
                // Load the geometric errors from the chunk data file.

                Stream stream = File.Open("Chunks.dat", FileMode.Open);
                BinaryFormatter formatter = new BinaryFormatter();

                chunkGeometricErrors = (Dictionary<int, float>)formatter.Deserialize(stream);
                stream.Close();
            }
            else
            {
                chunkGeometricErrors = new Dictionary<int, float>();
            }

            currentChunkIndex = 0;

            ConstructQuadTree();

            if (parameters.DoPreprocessing)
            {
                // Save the geometric errors to the chunk data file.

                Stream stream = File.Open("Chunks.dat", FileMode.Create);
                BinaryFormatter formatter = new BinaryFormatter();

                formatter.Serialize(stream, chunkGeometricErrors);
                stream.Close();
            }

            grassChunk = new GrassChunk(game, this, parameters.GrassChunkDimension, parameters.GrassChunkStarSeparation);
            GenerateGrassChunkPositions();
        }