Ejemplo n.º 1
0
        public void Create(int size, int gridSpacing, float minHeight, float maxHeight, HeightMapGenerationMethod method, float bumpiness)
        {
            Texture2D terrain_bitmap = null;
            if (method == HeightMapGenerationMethod.FromBitMap)
            {
                terrain_bitmap = content.Load<Texture2D>("terrain");
                size = terrain_bitmap.Width;
            }

            heightMap = new HeightMap(size, gridSpacing, minHeight, maxHeight);

            // Generate the terrain vertices.

            int totalVertices = size * size;
            //int totalIndices = (size - 1) * (size * 2 + 1);
            int totalIndices = (size - 1) * (size - 1) * 6;

            // NOTE: migration to XNA 4.0
            // NOTE: vertexDeclaration = new VertexDeclaration(graphicsDevice,
            // NOTE:                     VertexPositionNormalTexture.VertexElements);
            // NOTE:
            // NOTE: vertices = new VertexPositionNormalTexture[totalVertices];
            // NOTE:
            // NOTE: vertexBuffer = new DynamicVertexBuffer(graphicsDevice,
            // NOTE:                VertexPositionNormalTexture.SizeInBytes * vertices.Length,
            // NOTE:                BufferUsage.WriteOnly);
            // NOTE:
            vertices = new VertexPositionNormalTexture[totalVertices];

            vertexBuffer = new DynamicVertexBuffer(graphicsDevice, typeof(VertexPositionNormalTexture), vertices.Length, BufferUsage.WriteOnly);
            //vertexBuffer.ContentLost += new EventHandler(vertexBuffer_ContentLost);
            //vertexBuffer.SetData(particles);

            //vertexBuffer = new VertexBuffer(graphicsDevice, typeof(VertexBuffer), vertices.Length, BufferUsage.WriteOnly);

            switch (method)
            {
                case HeightMapGenerationMethod.DiamondSquareFractal:
                    GenerateUsingDiamondSquareFractal(1.2f);
                    break;
                case HeightMapGenerationMethod.FromBitMap:
                    GenerateFromBitmap(terrain_bitmap, bumpiness);
                    break;
                default:
                    GenerateUsingDiamondSquareFractal(1.2f);
                    break;
            }

            // Generete the terrain triangle indices.
            if (Using16BitIndices())
            {
                indices16Bit = new ushort[totalIndices];
                GenerateIndices();

                indexBuffer = new IndexBuffer(graphicsDevice,
                              typeof(ushort),
                              indices16Bit.Length,
                              BufferUsage.WriteOnly);

                indexBuffer.SetData(indices16Bit);
            }
            else
            {
                indices32Bit = new uint[totalIndices];
                GenerateIndices();

                indexBuffer = new IndexBuffer(graphicsDevice,
                              typeof(uint),
                              indices32Bit.Length,
                              BufferUsage.WriteOnly);

                indexBuffer.SetData(indices32Bit);
            }

            // Calculate the bounding box and bounding sphere for the terrain.

            Vector3 min = new Vector3(), max = new Vector3();

            min.X = 0.0f;
            min.Y = heightMap.MinHeight;
            min.Z = 0.0f;

            max.X = heightMap.Size * heightMap.GridSpacing;
            max.Y = heightMap.MaxHeight;
            max.Z = heightMap.Size * heightMap.GridSpacing;

            boundingBox = new BoundingBox(min, max);
            boundingSphere = BoundingSphere.CreateFromBoundingBox(boundingBox);

            // Load the effect file used to render the terrain.

            try
            {
                effect = content.Load<Effect>(@"Shaders\TerrainShader");
            }
            catch (ContentLoadException)
            {
                try
                {
                    effect = content.Load<Effect>("TerrainShader");
                }
                catch (ContentLoadException)
                {
                }
            }
        }
Ejemplo n.º 2
0
        public void Create(int size, int gridSpacing, float minHeight, float maxHeight, HeightMapGenerationMethod method, float bumpiness)
        {
            Texture2D terrain_bitmap = null;

            if (method == HeightMapGenerationMethod.FromBitMap)
            {
                terrain_bitmap = content.Load <Texture2D>("terrain");
                size           = terrain_bitmap.Width;
            }

            heightMap = new HeightMap(size, gridSpacing, minHeight, maxHeight);

            // Generate the terrain vertices.

            int totalVertices = size * size;
            //int totalIndices = (size - 1) * (size * 2 + 1);
            int totalIndices = (size - 1) * (size - 1) * 6;


            // NOTE: migration to XNA 4.0
            // NOTE: vertexDeclaration = new VertexDeclaration(graphicsDevice,
            // NOTE:                     VertexPositionNormalTexture.VertexElements);
            // NOTE:
            // NOTE: vertices = new VertexPositionNormalTexture[totalVertices];
            // NOTE:
            // NOTE: vertexBuffer = new DynamicVertexBuffer(graphicsDevice,
            // NOTE:                VertexPositionNormalTexture.SizeInBytes * vertices.Length,
            // NOTE:                BufferUsage.WriteOnly);
            // NOTE:
            vertices = new VertexPositionNormalTexture[totalVertices];

            vertexBuffer = new DynamicVertexBuffer(graphicsDevice, typeof(VertexPositionNormalTexture), vertices.Length, BufferUsage.WriteOnly);
            //vertexBuffer.ContentLost += new EventHandler(vertexBuffer_ContentLost);
            //vertexBuffer.SetData(particles);

            //vertexBuffer = new VertexBuffer(graphicsDevice, typeof(VertexBuffer), vertices.Length, BufferUsage.WriteOnly);

            switch (method)
            {
            case HeightMapGenerationMethod.DiamondSquareFractal:
                GenerateUsingDiamondSquareFractal(1.2f);
                break;

            case HeightMapGenerationMethod.FromBitMap:
                GenerateFromBitmap(terrain_bitmap, bumpiness);
                break;

            default:
                GenerateUsingDiamondSquareFractal(1.2f);
                break;
            }

            // Generete the terrain triangle indices.
            if (Using16BitIndices())
            {
                indices16Bit = new ushort[totalIndices];
                GenerateIndices();

                indexBuffer = new IndexBuffer(graphicsDevice,
                                              typeof(ushort),
                                              indices16Bit.Length,
                                              BufferUsage.WriteOnly);

                indexBuffer.SetData(indices16Bit);
            }
            else
            {
                indices32Bit = new uint[totalIndices];
                GenerateIndices();

                indexBuffer = new IndexBuffer(graphicsDevice,
                                              typeof(uint),
                                              indices32Bit.Length,
                                              BufferUsage.WriteOnly);

                indexBuffer.SetData(indices32Bit);
            }

            // Calculate the bounding box and bounding sphere for the terrain.

            Vector3 min = new Vector3(), max = new Vector3();

            min.X = 0.0f;
            min.Y = heightMap.MinHeight;
            min.Z = 0.0f;

            max.X = heightMap.Size * heightMap.GridSpacing;
            max.Y = heightMap.MaxHeight;
            max.Z = heightMap.Size * heightMap.GridSpacing;

            boundingBox    = new BoundingBox(min, max);
            boundingSphere = BoundingSphere.CreateFromBoundingBox(boundingBox);

            // Load the effect file used to render the terrain.

            try
            {
                effect = content.Load <Effect>(@"Shaders\TerrainShader");
            }
            catch (ContentLoadException)
            {
                try
                {
                    effect = content.Load <Effect>("TerrainShader");
                }
                catch (ContentLoadException)
                {
                }
            }
        }