History: MOGRE Editable Terrain Manager
Source of version: 13 (current)
Copy to clipboard
{img src="img/wiki_up/MOGRE_Editable_Terrain_Manager_big.jpg" alt="thumb|Click to enlarge" imalign="right" width="316"}
!!About
__Author__: [http://www.ogre3d.org/phpBB2addons/profile.php?mode=viewprofile&u=8791|lilljohan]
__Project__: MOGRE Editable Terrain Manager (MET)
__License__: [http://en.wikipedia.org/wiki/Zlib_License|zlib license]
__Wrapped project__: ((ETM|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__: [http://www.tinyrocket.se/MET.zip|TinyRocket site](Broken, New link: [https://web.archive.org/web/20100818132506if_/http://www.tinyrocket.se/MET.zip|Internet Archive link] or [https://github.com/cookgreen/MET|github backup repository]
Binaries for Mogre 1.6.4: [https://web.archive.org/web/20100818132506if_/http://www.tinyrocket.se/MET.zip|here]
Binaries for Mogre 1.7.1: [https://github.com/cookgreen/MET/releases/download/met-20221110/MET-Binaries-Mogre-1.7.1.7z|here (Github Release)]
__Current version__: ''MET-2008/10/10''
__Start Date__: Jan-09-2008
__Support__: [https://www.ogre3d.org/addonforums/8/t-6165.html?f=8&t=6165&start=1|Forum topic]
---
MOGRE Editable Terrain Manager (MET) is a [http://en.wikipedia.org/wiki/C%2B%2B/CLI|C++/CLI] ((wrapper)) for the ((ETM|Editable Terrain Manager (ETM))) and can be used with ((MOGRE|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 [https://web.archive.org/web/20100818132506if_/http://www.tinyrocket.se/MET.zip|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 [https://www.ogre3d.org/addonforums/8/t-6165.html?f=8&t=6165&start=60|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
{CODE(theme="vb")}
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;{CODE}
2. Initialise the Root and Resource group:
{CODE(theme="vb")}
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();
{CODE}
3. Create the Editable Terrain manager:
{CODE(theme="vb")}
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;
{CODE}
4. Assign material and Raycast:
{CODE(theme="vb")}
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());
{CODE}
5. Mouse and Keyboard Event Callback
{CODE(theme="vb")}
app.MouseDown += OnMouseDown;
app.MouseUp += OnMouseUp;
app.MouseMove += OnMouseMove;
app.KeyUp += OnKeyUp;
app.KeyDown += OnKeyDown;
{CODE}
6. Main loop
{CODE(theme="vb")}
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;
}
{CODE}
7. Dispose the resources
{CODE(theme="vb")}
splattingManager.Dispose();
brush.Dispose();
terrainInfo.Dispose();
terrainManager.Dispose();
material.Dispose();
root.Dispose();
root = null;
{CODE}
8. Event Callback
{CODE(theme="vb")}
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");
}
}
{CODE}
''Full Sample Code included in the MET.zip''
{img src="https://media.moddb.com/cache/images/members/4/3399/3398047/thumb_620x2000/2022-11-10_18-50-10-25.png"}
!!Information for maintainers
''Please write some information about how to modify the wrapper''
!!See also
* ((ETM|Editable Terrain Manager))
* ((Myrddin Landscape Plugin)) - a scene manager with editor (for Mogre)