public static PluginsLoader GetInstance() { instance.LoadAssemblyPlugins(); Config.GetInstance(); Terrain.GetInstance(); HeightMapPersistence.GetInstance(); SlopeMapPersistence.GetInstance(); MouseFilterSdlMouseCache.GetInstance(); KeyFilterSdlKeyCache.GetInstance(); KeyFilterConfigMappingsFactory.GetInstance(); //HeightEditor.GetInstance(); EditController.GetInstance(); Camera.GetInstance(); Framerate.GetInstance(); FrustrumCulling.GetInstance(); BrushShapeController.GetInstance().Register(new RoundBrush()); BrushShapeController.GetInstance().Register(new SquareBrush()); BrushEffectController.GetInstance().Register(new RaiseLower()); BrushEffectController.GetInstance().Register(new Flatten()); BrushEffectController.GetInstance().Register(new PaintTexture()); BrushEffectController.GetInstance().Register(new AddFeature()); BrushEffectController.GetInstance().Register(new FixedHeight()); //Sm3Persistence.GetInstance().LoadSm3("maps/Whakamatunga_Riri.sm3"); //EditTexture.GetInstance(); //FeatureEditing.GetInstance(); MainUI.GetInstance(); return(instance); }
public void Load(string filename) { //Bitmap bitmap = Bitmap.FromFile(filename) as Bitmap; //Bitmap bitmap = DevIL.DevIL.LoadBitmap(filename); Image image = new Image(filename); int width = image.Width; int height = image.Height; Terrain.GetInstance().HeightMapWidth = width; Terrain.GetInstance().HeightMapHeight = height; Terrain.GetInstance().Map = new double[width, height]; LogFile.GetInstance().WriteLine("loaded bitmap " + width + " x " + height); double minheight = Config.GetInstance().minheight; double maxheight = Config.GetInstance().maxheight; double heightmultiplier = (maxheight - minheight) / 255; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { Terrain.GetInstance().Map[i, j] = (float)(minheight + heightmultiplier * image.GetBlue(i, j)); } } Terrain.GetInstance().HeightmapFilename = filename; Terrain.GetInstance().OnTerrainModified(); MainUI.GetInstance().uiwindow.InfoMessage("Heightmap loaded"); }
public void Save(string filename) { double[,] mesh = SlopeMap.GetInstance().GetSlopeMap(); int width = mesh.GetUpperBound(0) + 1; int height = mesh.GetUpperBound(1) + 1; double maxslopetoexport = Config.GetInstance().SlopemapExportMaxSlope; Image image = new Image(width, height); //Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb); //Graphics g = Graphics.FromImage(bitmap); // cache pencolors; List <MovementAreaConfig> movementareas = Config.GetInstance().movementareas; SortedList <double, Color> sortedcolorbymaxslope = new SortedList <double, Color>(); foreach (MovementAreaConfig movementarea in movementareas) { if (movementarea.MaxSlope >= 0) { sortedcolorbymaxslope.Add(movementarea.MaxSlope, movementarea.color); } else { sortedcolorbymaxslope.Add(double.PositiveInfinity, movementarea.color); } } for (int area = 0; area < sortedcolorbymaxslope.Count; area++) { LogFile.GetInstance().WriteLine(sortedcolorbymaxslope.Keys[area] + " " + sortedcolorbymaxslope.Values[area].ToString()); } for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { Color colortouse = new Color(1, 1, 1); for (int area = 0; area < sortedcolorbymaxslope.Count; area++) { if (mesh[i, j] < sortedcolorbymaxslope.Keys[area]) { colortouse = sortedcolorbymaxslope.Values[area]; break; } } int valuetowrite = (int)(mesh[i, j] * 255 / maxslopetoexport); valuetowrite = Math.Max(0, valuetowrite); valuetowrite = Math.Min(255, valuetowrite); image.SetPixel(i, j, (byte)(colortouse.r * 255 * valuetowrite), (byte)(colortouse.g * 255 * valuetowrite), (byte)(colortouse.b * 255 * valuetowrite), 255 ); } } image.Save(filename); exportheightmapfilename = filename; MainUI.GetInstance().uiwindow.InfoMessage("Slopemap exported"); }
public void Register(IBrushShape brushshape) { Console.WriteLine(this.GetType() + " registering " + brushshape); this.brushshapes.Add(brushshape.GetType(), brushshape); if (CurrentEditBrush.GetInstance().BrushShape == null) { CurrentEditBrush.GetInstance().BrushShape = brushshape; } MainUI.GetInstance().uiwindow.AddBrushShape(brushshape.Name, brushshape.Description, brushshape); }
public void Register(IBrushEffect brusheffect) { Console.WriteLine(this.GetType() + " registering " + brusheffect); this.brusheffects.Add(brusheffect.GetType(), brusheffect); if (CurrentEditBrush.GetInstance().BrushEffect == null) { CurrentEditBrush.GetInstance().BrushEffect = brusheffect; } MainUI.GetInstance().uiwindow.AddBrushEffect(brusheffect.Name, brusheffect.Description, brusheffect); }
public void LoadSm3(string filename) { terrain.tdfparser = TdfParser.FromFile(filename); TdfParser.Section terrainsection = terrain.tdfparser.RootSection.SubSection("map/terrain"); string tdfdirectory = Path.GetDirectoryName(Path.GetDirectoryName(filename)); LoadTextureStages(tdfdirectory, terrainsection); LoadHeightMap(tdfdirectory, terrainsection); terrain.OnTerrainModified(); MainUI.GetInstance().uiwindow.InfoMessage("SM3 load completed"); }
public void on_btnLoadFeatures_clicked(object o, EventArgs e) { MainUI.GetInstance().uiwindow.InfoMessage("Load. We're going to ask you for two filepaths. One is the featurelist textfile and one is the feature data binary file"); string featurefilepath = MainUI.GetInstance().uiwindow.GetFilePath("Load. Please enter filepath for featurelist text file:", "features.tdf"); if (featurefilepath != "") { string featurebinfilepath = MainUI.GetInstance().uiwindow.GetFilePath("Load. Please enter filepath for feature data binary file:", "features.features"); if (featurebinfilepath != "") { FeaturePersistence.GetInstance().LoadFeatures(featurefilepath, featurebinfilepath); } } }
public void Save(string filename) { double[,] mesh = Terrain.GetInstance().Map; int width = mesh.GetUpperBound(0) + 1; int height = mesh.GetUpperBound(1) + 1; Image image = new Image(width, height); //Bitmap bitmap = new Bitmap( width, height, PixelFormat.Format24bppRgb ); //Graphics g = Graphics.FromImage(bitmap); //Pen[] pens = new Pen[256]; //for (int i = 0; i < 256; i++) //{ // pens[i] = new Pen(System.Drawing.Color.FromArgb(i, i, i)); //} double minheight = Config.GetInstance().minheight; double maxheight = Config.GetInstance().maxheight; double heightmultiplier = 255 / (maxheight - minheight); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int normalizedmeshvalue = (int)((mesh[i, j] - minheight) * heightmultiplier); normalizedmeshvalue = Math.Max(0, normalizedmeshvalue); normalizedmeshvalue = Math.Min(255, normalizedmeshvalue); byte normalizedmeshvaluebyte = (byte)normalizedmeshvalue; image.SetPixel(i, j, normalizedmeshvaluebyte, normalizedmeshvaluebyte, normalizedmeshvaluebyte, 255); //g.DrawRectangle(pens[ normalizedmeshvalue ], i, j, 1, 1); } } if (File.Exists(filename)) { File.Delete(filename); } image.Save(filename); //DevIL.DevIL.SaveBitmap(filename, bitmap); //bitmap.Save(filename, ImageFormat.Bmp); Terrain.GetInstance().HeightmapFilename = filename; Terrain.GetInstance().OnTerrainModified(); MainUI.GetInstance().uiwindow.InfoMessage("Heightmap saved"); }
// what we need to do is to render the splatted texture in ortho mode with lighting off // or normals off (to be tested) // then to export the generated bitmap // viewport should be set to heightmapwidth x heightmapheight public void Export(string filepath) { Terrain terrain = Terrain.GetInstance(); int picturewidth = terrain.MapWidth * Terrain.SquareSize; int pictureheight = terrain.MapHeight * Terrain.SquareSize; LogFile.GetInstance().WriteLine("Export to " + filepath + " picturewidth " + picturewidth + " pictureheight: " + pictureheight); //int windowwidth = RendererSdl.GetInstance().WindowWidth; //int windowheight = RendererSdl.GetInstance().WindowHeight; int windowwidth = 256; int windowheight = 256; Gl.glViewport(0, 0, windowwidth, windowheight); byte[] buffer = new byte[windowwidth * windowheight * 4]; //System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap( picturewidth, pictureheight ); //byte[] imagedata = new byte[picturewidth * pictureheight * 4]; Image image = new Image(picturewidth, pictureheight); List <RendererPass> rendererpasses = new List <RendererPass>(); bool multipass = true; // force multipass for now for simplicity int maxtexels = RendererSdl.GetInstance().MaxTexelUnits; if (multipass) { for (int i = 0; i < terrain.texturestages.Count; i++) { MapTextureStage maptexturestage = terrain.texturestages[i]; int numtexturestagesrequired = maptexturestage.NumTextureStagesRequired; if (numtexturestagesrequired > 0) // exclude Nops { RendererPass rendererpass = new RendererPass(maxtexels); for (int j = 0; j < maptexturestage.NumTextureStagesRequired; j++) { rendererpass.AddStage(new RendererTextureStage(maptexturestage, j, true, picturewidth, pictureheight)); } rendererpasses.Add(rendererpass); } } } GraphicsHelperGl g = new GraphicsHelperGl(); Gl.glMatrixMode(Gl.GL_PROJECTION); Gl.glPushMatrix(); Gl.glLoadIdentity(); Gl.glOrtho(0, windowwidth, windowheight, 0, -1, 1); Gl.glMatrixMode(Gl.GL_MODELVIEW); Gl.glPushMatrix(); Gl.glLoadIdentity(); Gl.glDisable(Gl.GL_CULL_FACE); Gl.glDisable(Gl.GL_LIGHTING); g.EnableBlendSrcAlpha(); Gl.glDepthFunc(Gl.GL_LEQUAL); for (int chunkx = 0; chunkx < Math.Ceiling((double)picturewidth / windowwidth); chunkx++) { for (int chunky = 0; chunky < Math.Ceiling((double)pictureheight / windowheight); chunky++) { Console.WriteLine("chunkx " + chunkx + " chunky " + chunky); Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT); // Clear The Screen And Depth Buffer foreach (RendererPass rendererpass in rendererpasses) { rendererpass.Apply(); Gl.glBegin(Gl.GL_QUADS); double ul = (chunkx * windowwidth); double ur = (chunkx * windowwidth + windowwidth); double vt = (chunky * windowheight); double vb = (chunky * windowheight + windowheight); Gl.glTexCoord2d(ul, vt); Gl.glMultiTexCoord2dARB(Gl.GL_TEXTURE1_ARB, ul, vt); Gl.glVertex2i(0, 0); Gl.glTexCoord2d(ul, vb); Gl.glMultiTexCoord2dARB(Gl.GL_TEXTURE1_ARB, ul, vb); Gl.glVertex2i(0, windowheight); Gl.glTexCoord2d(ur, vb); Gl.glMultiTexCoord2dARB(Gl.GL_TEXTURE1_ARB, ur, vb); Gl.glVertex2i(windowwidth, windowheight); Gl.glTexCoord2d(ur, vt); Gl.glMultiTexCoord2dARB(Gl.GL_TEXTURE1_ARB, ur, vt); Gl.glVertex2i(windowwidth, 0); Gl.glEnd(); } IntPtr ptr = Marshal.AllocHGlobal(windowwidth * windowheight * 4); Gl.glReadPixels(0, 0, windowwidth, windowheight, Gl.GL_RGBA, Gl.GL_UNSIGNED_BYTE, ptr); Marshal.Copy(ptr, buffer, 0, windowwidth * windowheight * 4); Marshal.FreeHGlobal(ptr); for (int x = 0; x < windowwidth; x++) { for (int y = 0; y < windowheight; y++) { if ((chunky * windowheight + y < pictureheight) && (chunkx * windowwidth + x < picturewidth)) { int pixeloffset = (windowheight - y - 1) * windowwidth * 4 + x * 4; //bitmap.SetPixel(x + chunkx * windowwidth, y + chunky * windowheight, System.Drawing.Color.FromArgb(buffer[pixeloffset + 0], //buffer[pixeloffset + 1], buffer[pixeloffset + 2])); image.SetPixel(x + chunkx * windowwidth, y + chunky * windowheight, buffer[pixeloffset + 0], buffer[pixeloffset + 1], buffer[pixeloffset + 2], 255 ); } } } } } if (File.Exists(filepath)) { File.Delete(filepath); } image.Save(filepath); //DevIL.DevIL.SaveBitmap( filepath, bitmap); Gl.glPopMatrix(); Gl.glMatrixMode(Gl.GL_PROJECTION); Gl.glPopMatrix(); Gl.glMatrixMode(Gl.GL_MODELVIEW); Gl.glEnable(Gl.GL_LIGHTING); g.ActiveTexture(1); g.DisableTexture2d(); g.SetTextureScale(1); g.ActiveTexture(0); g.SetTextureScale(1); Gl.glEnable(Gl.GL_CULL_FACE); Gl.glEnable(Gl.GL_LIGHTING); Gl.glDisable(Gl.GL_BLEND); g.EnableModulate(); Gl.glViewport(0, 0, RendererSdl.GetInstance().OuterWindowWidth, RendererSdl.GetInstance().OuterWindowHeight); g.CheckError(); MainUI.GetInstance().uiwindow.InfoMessage("Exported blended terrain texture to " + filepath); }
public void LoadFeatures(string featurelistfilename, string featuredatafilename) { LogFile.GetInstance().WriteLine("LoadFeatures()"); TdfParser tdfparser = TdfParser.FromFile(featurelistfilename); Terrain terrain = Terrain.GetInstance(); terrain.FeatureMap = new Unit[terrain.MapWidth, terrain.MapHeight]; Dictionary <int, string> featurenamebynumber = new Dictionary <int, string>(); int numfeaturetypes = tdfparser.RootSection.GetIntValue("map/featuretypes/numtypes"); LogFile.GetInstance().WriteLine("Num types: " + numfeaturetypes); for (int i = 0; i < numfeaturetypes; i++) { string featurename = tdfparser.RootSection.GetStringValue("map/featuretypes/type" + i); if (!File.Exists(Path.Combine("objects3d", featurename + ".s3o"))) { MainUI.GetInstance().uiwindow.WarningMessage("Warning: objects3d/" + featurename + ".s3o not found"); } else { LogFile.GetInstance().WriteLine("Feature type " + i + " " + featurename.ToLower()); featurenamebynumber.Add(i, featurename.ToLower()); } } List <Sm3Feature> features = new List <Sm3Feature>(); // from FeaturePlacer Form1.cs by Jelmer Cnossen FileStream fs = new FileStream(featuredatafilename, FileMode.Open); if (fs != null) { BinaryReader br = new BinaryReader(fs); if (br.ReadByte() != 0) { MainUI.GetInstance().uiwindow.WarningMessage("The featuredata you are trying to load was saved using a different version."); return; } int numFeatures = br.ReadInt32(); features.Clear(); for (int a = 0; a < numFeatures; a++) { Sm3Feature f = new Sm3Feature(); features.Add(f); f.type = br.ReadInt32(); f.x = br.ReadSingle(); f.y = br.ReadSingle(); f.z = br.ReadSingle(); f.rotation = br.ReadSingle(); } } foreach (Sm3Feature sm3feature in features) { if (featurenamebynumber.ContainsKey(sm3feature.type)) { string featurename = featurenamebynumber[sm3feature.type].ToLower(); if (!UnitCache.GetInstance().UnitsByName.ContainsKey(featurename)) { LogFile.GetInstance().WriteLine("Loading unit " + Path.Combine("objects3d", featurename + ".s3o") + " ... "); Unit unit = new S3oLoader().LoadS3o(Path.Combine("objects3d", featurename + ".s3o")); UnitCache.GetInstance().UnitsByName.Add(featurename, unit); } LogFile.GetInstance().WriteLine("Adding " + featurename + " at " + (int)(sm3feature.x / Terrain.SquareSize) + " " + (int)(sm3feature.y / Terrain.SquareSize)); terrain.FeatureMap[(int)(sm3feature.x / Terrain.SquareSize), (int)(sm3feature.y / Terrain.SquareSize)] = UnitCache.GetInstance().UnitsByName[featurename]; } } terrain.OnTerrainModified(); }
public void SaveFeatures(string featurelistfilename, string featuredatafilename) { Dictionary <string, int> featurenumberbyname = new Dictionary <string, int>(); List <Sm3Feature> features = new List <Sm3Feature>(); int nextnumber = 0; StreamWriter featurelistfile = new StreamWriter(featurelistfilename, false, Encoding.UTF8); for (int x = 0; x < Terrain.GetInstance().MapWidth; x++) { for (int y = 0; y < Terrain.GetInstance().MapHeight; y++) { Unit thisunit = Terrain.GetInstance().FeatureMap[x, y]; if (thisunit != null) { if (!featurenumberbyname.ContainsKey(thisunit.Name)) { featurenumberbyname.Add(thisunit.Name, nextnumber); nextnumber++; } int featurenumber = featurenumberbyname[thisunit.Name]; features.Add(new Sm3Feature(featurenumber, x * Terrain.SquareSize, y * Terrain.SquareSize, Terrain.GetInstance().Map[x, y])); // note to self: unsure whether to multiply by squaresize or not ??? } } } featurelistfile.WriteLine("[MAP]"); featurelistfile.WriteLine("{"); featurelistfile.WriteLine(" [FeatureTypes]"); featurelistfile.WriteLine(" {"); featurelistfile.WriteLine(" NumTypes=" + featurenumberbyname.Count + ";"); foreach (KeyValuePair <string, int> kvp in featurenumberbyname) { featurelistfile.WriteLine(" type" + kvp.Value + "=" + kvp.Key + ";"); } featurelistfile.WriteLine(" }"); featurelistfile.WriteLine("}"); featurelistfile.Close(); // from FeaturePlacer Form1.cs by Jelmer Cnossen // for reference, FeaturePlacer uses following data types: // public class feature // { // public float x, y, z; // public float rotation; // public int type; // } FileStream fs = new FileStream(featuredatafilename, FileMode.Create); if (fs != null) { fs.WriteByte(0); // version BinaryWriter bw = new BinaryWriter(fs); bw.Write((UInt32)features.Count); foreach (Sm3Feature f in features) { bw.Write(f.type); bw.Write(f.x); bw.Write(f.y); bw.Write(f.z); bw.Write(f.rotation); } } fs.Close(); MainUI.GetInstance().uiwindow.InfoMessage("Features stored to file"); }