/// <summary>
        /// Build the terrain.
        /// </summary>
        public void BuildTerrain()
        {
            if (Heightmap == null) return;

            int width = Heightmap.Width;
            int depth = Heightmap.Depth;

            // Clear the terrain patches.
            Patches.Clear();

            // Compute the world matrix to place the terrain in the middle of the scene.
            //            _world = Matrix.Identity;//Matrix.CreateTranslation(width*-0.5f, 0.0f, depth*-0.5f);

            // Create the terrain patches.
            const int patchWidth = 16;
            const int patchDepth = 16;
            int patchCountX = width/patchWidth;
            int patchCountZ = depth/patchDepth;

            PatchRows = patchCountX;
            PatchColumns = patchCountZ;

            for (int x = 0; x < patchCountX; ++x)
            {
                for (int z = 0; z < patchCountZ; ++z)
                {
                    // It is necessary to use patch width and depths of +1 otherwise there will be
                    // gaps between the patches.. [0,15] and [16,31] have a gap of one unit!
                    var patch = new TerrainPatch(Game, Heightmap, World, patchWidth + 1, patchDepth + 1,
                                                 x*patchWidth, z*patchDepth);
            //                        x*(patchWidth - 1), z*(patchDepth - 1));

                    Patches.Add(patch);
                }
            }

            // Find the minimum bounding box that covers all patches
            BoundingBox box = Patches[0].BoundingBox;
            foreach (TerrainPatch patch in Patches)
                box = BoundingBox.CreateMerged(box, patch.BoundingBox);

            BoundingBox = box;
            PatchCount = Patches.Count;

            _vertexDeclaration = new VertexDeclaration(GraphicsDevice, VertexPositionNormalTexture.VertexElements);
        }
        private static void PopulatePatch(ICollection<Vector3> trees, TerrainPatch patch)
        {
            int treeDistanceZ = patch.Depth/1;
            int treeDistanceX = patch.Width/1;

            for (int z = 0; z < patch.Depth; z += treeDistanceZ)
            {
                for (int x = 0; x < patch.Width; x += treeDistanceX)
                {
                    int index = z*patch.Width + x;
                    trees.Add(patch.Geometry[index].Position);
                }
            }
        }