Example #1
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();
        }
        /// <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();
        }