About
Author: lilljohan
Project: MOGRE Editable Terrain Manager (MET)
License: zlib license
Wrapped project: Editable Terrain Manager
Wrapped project license: GPLv2 with runtime exception.
Type: C++/CLI -Wrapper
Dependencies: Ogre/Mogre 1.4.8/1.6.4
Status: Finished
Binaries and sources: TinyRocket site(Broken, New link: Internet Archive link or github backup repository
Binaries for Mogre 1.6.4: here
Binaries for Mogre 1.7.1: here (Github Release)
Current version: MET-2008/10/10
Start Date: Jan-09-2008
Support: Forum topic
MOGRE Editable Terrain Manager (MET) is a C++/CLI wrapper for the Editable Terrain Manager (ETM) and can be used with Mogre. ETM itself is an addon library which simply manages terrain and makes it possible to deform and texture this terrain in real-time in response to user input. You can use it with any other scene manager in theory, though in practice the author recommends the Octree scene manager.
Currently the MET.zip file includes the source code for the wrapper written in C++/CLI and a test application with source code written in C#.
Features
- Terrain deforming - Just like the PLSM you can deform terrain as well as retrieve and set height values at arbitrary positions. You can save your work at any time, the ETM provides convenience functions to save and load terrain in raw file format as well as any image format provided by Ogre using 1 to 4 bytes per pixel. But you can just as well write your own load/save code, it's not much of a challenge Smile
- Terrain texturing - The terrain material is completely your responsibility, so you can do whatever you want. The ETM provides a SplattingManager class which you can use to do editable texture splatting, but it's really up to you.
- More flexible terrain sizes - ETM does not provide paging, but its restraints on valid terrain sizes are a lot less restrictive than those of the TSM. Rectangular terrain is just fine. The tile size must still be 2^n+1, but the terrain size can be anything that fits A * (tilesize-1) + 1 x B * (tilesize-1) + 1.
- Lightmap calculation - You can tell the ETM to calculate a lightmap including terrain shadows. The lightmaps may not be the prettiest ever to have been generated, but the algorithm is pretty fast. In my map editor which uses terrain sizes of up to 256x256 I do a recalculation of the lightmap after every completed editing step with no noticable time cost. The size of the lightmap is completely up to you, although the larger the lightmap, the longer the calculation will need.
- Base texture generation - You can "bake" your splatting layout into a single texture. This texture could be used as an alternative to dynamic splatting on older hardware or as a base texture at distance, so that splatting only takes place close to the camera.
- Minimaps - A simple helper function can combine a lightmap and a base texture to a new image which can be used as a minimap of the terrain.
Usage example
Maybe this code can be used as an example.
How to setup a project
1. Use Visual Studio to create a Windows Form (.NET Framework) project, and add MET.dll and Mogre.dll as references, create a Form called "MainForm", and create a static class (aka. "Program") which include the Main function
static Mogre.Camera camera; static Mogre.SceneManager sceneManager; static Mogre.RenderWindow window; static bool running = false; static MET.TerrainManager terrainManager; static MET.TerrainInfo terrainInfo; static MET.SplattingManager splattingManager; static MET.Brush brush; static bool middleMouseDown = false; static int oX = 0, oY = 0; static bool resetMousePos = false; static bool deform = true; static Mogre.RaySceneQuery raySceneQuery; static Mogre.Vector3 mousePos = new Mogre.Vector3(0, 0, 0); static uint chosenTexture = 0; static bool leftMouseDown = false; static bool rightMouseDown = false;
2. Initialise the Root and Resource group:
MainForm app = new MainForm(); app.ClientSize = new System.Drawing.Size(800, 600); app.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; Mogre.Root root = new Mogre.Root("", "", "MainForm.log"); root.LoadPlugin("RenderSystem_Direct3D9"); root.LoadPlugin("Plugin_OctreeSceneManager"); root.LoadPlugin("Plugin_CgProgramManager"); Mogre.RenderSystem renderSystem = root.GetRenderSystemByName("Direct3D9 Rendering Subsystem"); root.RenderSystem = renderSystem; root.Initialise(false); Mogre.NameValuePairList misc = new Mogre.NameValuePairList(); misc["externalWindowHandle"] = app.Handle.ToString(); window = root.CreateRenderWindow("renderWindow", (uint)app.ClientSize.Width, (uint)app.ClientSize.Height, false, misc); sceneManager = root.CreateSceneManager("OctreeSceneManager"); camera = sceneManager.CreateCamera("the_camera"); camera.SetPosition(200, 500, 200); camera.NearClipDistance = 0.1f; camera.FarClipDistance = 6000.0f; Mogre.Viewport viewport = window.AddViewport(camera); viewport.BackgroundColour = new Mogre.ColourValue(0.5f, 0.5f, 0.9f, 1.0f); camera.AspectRatio = (float)window.Width / (float)window.Height; Mogre.ResourceGroupManager.Singleton.AddResourceLocation("..\\media", "FileSystem", "ET"); Mogre.ResourceGroupManager.Singleton.AddResourceLocation("..\\media\\ET", "FileSystem", "ET"); Mogre.ResourceGroupManager.Singleton.InitialiseAllResourceGroups();
3. Create the Editable Terrain manager:
terrainManager = new MET.TerrainManager(sceneManager); terrainManager.SetLodErrorMargin(2, (uint)viewport.ActualHeight); terrainManager.SetUseLodMorphing(true, 0.2f, "morphFactor"); float[] hm = new float[513 * 513]; for (int i = 0; i < 513 * 513; i++) hm[i] = 0.5f; terrainInfo = new MET.TerrainInfo(513, 513, hm); terrainInfo.Extents = new Mogre.AxisAlignedBox(0, 0, 0, 2000, 512, 2000); terrainManager.CreateTerrain(terrainInfo); terrainInfo.Dispose(); terrainInfo = terrainManager.TerrainInfo; splattingManager = new MET.SplattingManager("ETSplatting", "ET", 512, 512, 3); splattingManager.NumTextures = 6;
4. Assign material and Raycast:
Mogre.MaterialPtr material = Mogre.MaterialManager.Singleton.GetByName("ETTerrainMaterial"); terrainManager.Material = material; Mogre.Image image = new Mogre.Image(); image.Load("brush.png", "ET"); image.Resize(16, 16); brush = MET.Brush.LoadBrushFromImage(image); raySceneQuery = sceneManager.CreateRayQuery(new Mogre.Ray());
5. Mouse and Keyboard Event Callback
app.MouseDown += OnMouseDown; app.MouseUp += OnMouseUp; app.MouseMove += OnMouseMove; app.KeyUp += OnKeyUp; app.KeyDown += OnKeyDown;
6. Main loop
while (running) { float delta = Timer.Delta(); if (rightMouseDown || leftMouseDown) { if (deform) { float intens = delta * 0.2f * ((leftMouseDown) ? 1.0f : -1.0f); int x = terrainInfo.PosToVertexX(mousePos.x); int z = terrainInfo.PosToVertexZ(mousePos.z); terrainManager.Deform(x, z, brush, intens); } else { float intens = delta * 3 * ((leftMouseDown) ? 1.0f : -1.0f); int x = terrainInfo.PosToVertexX(mousePos.x); int z = terrainInfo.PosToVertexZ(mousePos.z); splattingManager.Paint(chosenTexture, x, z, brush, intens); } } root.RenderOneFrame(); System.Windows.Forms.Application.DoEvents(); running = running && !app.IsDisposed; }
7. Dispose the resources
splattingManager.Dispose(); brush.Dispose(); terrainInfo.Dispose(); terrainManager.Dispose(); material.Dispose(); root.Dispose(); root = null;
8. Event Callback
static void OnMouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { if (e.Button == System.Windows.Forms.MouseButtons.Middle) middleMouseDown = true; if (e.Button == System.Windows.Forms.MouseButtons.Left) leftMouseDown = true; if (e.Button == System.Windows.Forms.MouseButtons.Right) rightMouseDown = true; } static void OnMouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { if (e.Button == System.Windows.Forms.MouseButtons.Middle) { middleMouseDown = false; resetMousePos = true; } if (e.Button == System.Windows.Forms.MouseButtons.Left) leftMouseDown = false; if (e.Button == System.Windows.Forms.MouseButtons.Right) rightMouseDown = false; } static void OnMouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { int dx = (resetMousePos) ? 0 : oX - e.X; int dy = (resetMousePos) ? 0 : oY - e.Y; oX = e.X; oY = e.Y; if (middleMouseDown) { camera.Yaw(dx * 0.1f); camera.Pitch(dy * 0.1f); resetMousePos = false; } uint w, h, cd; int l, r; window.GetMetrics(out w, out h, out cd, out l, out r); Mogre.Ray ray = camera.GetCameraToViewportRay((float)e.X / (float)w, (float)e.Y / (float)h); Mogre.Vector3 result = terrainInfo.RayIntersects(ray); if (result != null) { mousePos.x = result.x; mousePos.y = result.y; mousePos.z = result.z; } } static void OnKeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if (middleMouseDown) { if (e.KeyCode == System.Windows.Forms.Keys.W) camera.MoveRelative(new Mogre.Vector3(0, 0, -9)); if (e.KeyCode == System.Windows.Forms.Keys.S) camera.MoveRelative(new Mogre.Vector3(0, 0, 9)); if (e.KeyCode == System.Windows.Forms.Keys.A) camera.MoveRelative(new Mogre.Vector3(-9, 0, 0)); if (e.KeyCode == System.Windows.Forms.Keys.D) camera.MoveRelative(new Mogre.Vector3(9, 0, 0)); } } static void OnKeyUp(object sender, System.Windows.Forms.KeyEventArgs e) { if (e.KeyCode == System.Windows.Forms.Keys.F5) deform = !deform; if (e.KeyCode == System.Windows.Forms.Keys.D1) chosenTexture = 0; if (e.KeyCode == System.Windows.Forms.Keys.D2) chosenTexture = 1; if (e.KeyCode == System.Windows.Forms.Keys.D3) chosenTexture = 2; if (e.KeyCode == System.Windows.Forms.Keys.D4) chosenTexture = 3; if (e.KeyCode == System.Windows.Forms.Keys.D5) chosenTexture = 4; if (e.KeyCode == System.Windows.Forms.Keys.D6) chosenTexture = 5; if (e.KeyCode == System.Windows.Forms.Keys.Escape) running = false; if (e.KeyCode == System.Windows.Forms.Keys.F10) { Mogre.Image img = new Mogre.Image(); MET.TerrainInfo.SaveHeightmapToImage(terrainInfo, img); img.Save("heightmap.png"); splattingManager.SaveMapToImage(0, img); img.Save("splat1.png"); splattingManager.SaveMapToImage(1, img); img.Save("splat2.png"); } }
Full Sample Code included in the MET.zip
Information for maintainers
Please write some information about how to modify the wrapper
See also
- Editable Terrain Manager
- Myrddin Landscape Plugin - a scene manager with editor (for Mogre)