private void SaveTerrain(string filename)
        {
            if (File.Exists(filename))
            {
                File.Delete(filename);
            }

            XMLReader xmlReader = new XMLReader();

            xmlReader.CreateDocument();
            xmlReader.SetRoot("Terrain");

            xmlReader.AddElement("drawskybox", Editor.bDrawSkybox.ToString());
            xmlReader.AddElement("skybox", selectedSky);

            if (Editor.heightmap != null)
            {
                Editor.heightmap.UpdateHeightFile();

                //Heightmap Texture
                string title = Path.GetFileNameWithoutExtension(filename);
                Editor.heightmap.heightmap.Save(title + "_height.bmp", ImageFileFormat.Bmp);
                xmlReader.AddElement("heightmap", title + "_height.bmp");

                //Colormap Texture
                Editor.heightmap.colormap.Save(title + "_color.tga", ImageFileFormat.Tga);
                xmlReader.AddElement("colormap", title + "_color.tga");

                //Heightmap Settings
                xmlReader.AddElement("cellsize", Editor.heightmap.cellSize.X + ";" + Editor.heightmap.cellSize.Y);
                xmlReader.AddElement("maxheight", Editor.heightmap.maxHeight.ToString());
                xmlReader.AddElement("smooth", Editor.heightmap.bSmooth.ToString());
                xmlReader.AddElement("drawdetail", Editor.heightmap.bDrawDetail.ToString());
                xmlReader.AddElement("ambientlight", Editor.heightmap.ambientLight.X + ";" + Editor.heightmap.ambientLight.Y + ";" + Editor.heightmap.ambientLight.Z);

                //Texture Layers Settings
                if (Editor.heightmap.textureLayer != null)
                {
                    for (int i = 0; i < Editor.heightmap.textureLayer.Length; i++)
                    {
                        xmlReader.AddElement("layer" + i + "tex", Editor.heightmap.textureLayer[i].layerTex);
                        xmlReader.AddElement("layer" + i + "texScale", Editor.heightmap.textureLayer[i].scale.ToString().Replace('.', ';'));
                    }
                }
            }

            //Sun Settings
            xmlReader.AddElement("drawsun", Editor.bDrawSun.ToString());
            xmlReader.AddElement("suncolor", Editor.sun.color.X + ";" + Editor.sun.color.Y + ";" + Editor.sun.color.Z);
            xmlReader.AddElement("angle", MathHelper.ToDegrees(Editor.sun.rotation.Y).ToString());
            xmlReader.AddElement("elevation", MathHelper.ToDegrees(Editor.sun.rotation.X).ToString());
            xmlReader.AddElement("longitudespeed", Editor.sun.LongitudeSpeed.ToString());
            xmlReader.AddElement("latitudespeed", Editor.sun.LatitudeSpeed.ToString());
            xmlReader.AddElement("intensity", Editor.sun.lightPower.ToString());
            xmlReader.AddElement("sunraycollision", checkBox4.Checked.ToString());

            //Fog Settings
            xmlReader.AddElement("usefog", Editor.bUseFog.ToString());
            xmlReader.AddElement("fogcolor", Editor.fogColor.ToVector3().X + ";" + Editor.fogColor.ToVector3().Y + ";" + Editor.fogColor.ToVector3().Z);
            xmlReader.AddElement("fogstart", Editor.fogStart.ToString());
            xmlReader.AddElement("fogend", Editor.fogEnd.ToString());
            xmlReader.AddElement("fogdensity", Editor.fogDensity.ToString());

            xmlReader.Save(filename);

            Editor.console.Add("File saved successfuly!");
        }
        private void LoadTerrain(string filename)
        {
            if (!File.Exists(filename))
            {
                return;
            }

            XMLReader xmlReader = new XMLReader();

            xmlReader.Open(filename);

            Editor.bDrawSkybox = Convert.ToBoolean(xmlReader.GetElementValue("drawskybox"));

            //Skybox
            if (Editor.skybox == null)
            {
                Editor.skybox = new Skybox();
            }
            Editor.skybox.LoadTextures(xmlReader.GetElementValue("skybox"));

            //Sun Settings
            Editor.bDrawSun = Convert.ToBoolean(xmlReader.GetElementValue("drawsun"));
            Editor.sun      = new Sun(Convert.ToDouble(xmlReader.GetElementValue("angle")), Convert.ToDouble(xmlReader.GetElementValue("elevation")));
            string[] sColor = xmlReader.GetElementValue("suncolor").Split(';');
            Editor.sun.color                  = new Vector3((float)Convert.ToDouble(sColor[0]), (float)Convert.ToDouble(sColor[1]), (float)Convert.ToDouble(sColor[2]));
            Editor.sun.LongitudeSpeed         = Convert.ToDouble(xmlReader.GetElementValue("longitudespeed"));
            Editor.sun.LatitudeSpeed          = Convert.ToDouble(xmlReader.GetElementValue("latitudespeed"));
            Editor.sun.lightPower             = (float)Convert.ToDouble(xmlReader.GetElementValue("intensity"));
            Editor.sun.bCheckTerrainCollision = Convert.ToBoolean(xmlReader.GetElementValue("sunraycollision"));

            //Fog Settings
            Editor.bUseFog = Convert.ToBoolean(xmlReader.GetElementValue("usefog"));
            string[] fColor = xmlReader.GetElementValue("fogcolor").Split(';');
            Microsoft.Xna.Framework.Graphics.Color fogColor = new Microsoft.Xna.Framework.Graphics.Color(new Vector3((float)Convert.ToDouble(fColor[0]), (float)Convert.ToDouble(fColor[1]), (float)Convert.ToDouble(fColor[2])));
            Console.WriteLine("FogColor: " + fogColor.ToString());
            Editor.fogColor   = fogColor;
            Editor.fogStart   = (float)Convert.ToDouble(xmlReader.GetElementValue("fogstart"));
            Editor.fogEnd     = (float)Convert.ToDouble(xmlReader.GetElementValue("fogend"));
            Editor.fogDensity = (float)Convert.ToDouble(xmlReader.GetElementValue("fogdensity"));

            Editor.InitFog();

            //Heightmap
            if (xmlReader.GetElementValue("heightmap") != string.Empty)
            {
                string[] cSize = xmlReader.GetElementValue("cellsize").Split(';');
                Editor.heightmap = new Heightmap(new Vector2((float)Convert.ToDouble(cSize[0]), (float)Convert.ToDouble(cSize[1])));

                Editor.heightmap.LoadHeightMap(xmlReader.GetElementValue("heightmap"));
                Editor.heightmap.LoadColormap(xmlReader.GetElementValue("colormap"));

                Editor.heightmap.maxHeight   = (float)Convert.ToDouble(xmlReader.GetElementValue("maxheight"));
                Editor.heightmap.bSmooth     = Convert.ToBoolean(xmlReader.GetElementValue("smooth"));
                Editor.heightmap.bDrawDetail = Convert.ToBoolean(xmlReader.GetElementValue("drawdetail"));
                string[] aLight = xmlReader.GetElementValue("ambientlight").Split(';');
                Editor.heightmap.ambientLight   = new Vector3();
                Editor.heightmap.ambientLight.X = (float)Convert.ToDouble(aLight[0]);
                Editor.heightmap.ambientLight.Y = (float)Convert.ToDouble(aLight[1]);
                Editor.heightmap.ambientLight.Z = (float)Convert.ToDouble(aLight[2]);

                //Texture Layers
                for (int i = 0; i < 4; i++)
                {
                    Editor.heightmap.textureLayer[i].layerTex = xmlReader.GetElementValue("layer" + i + "tex");
                    Editor.heightmap.textureLayer[i].scale    = (float)Convert.ToDouble(xmlReader.GetElementValue("layer" + i + "texScale"));
                }

                Editor.heightmap.Init();
            }

            GetSkyboxes();
            GetSunData();
            GetHeightmapData();
        }