What am I doing wrong with the the terrain manager?

JoeC

12-06-2008 17:28:24

Hello,
Can someone have a look at the code below and tell me what's wrong with it. Its a pretty standard application code taken from the samples.
All it does is load the sample terrain, move over it and then load it again.
The problem is that the amount of memory the application takes is permanently increasing until it runs out and crashes.
(This is actually a vast simplification of my actual project which uses several different custom terrains but the effect is still the same).
I'm really stuck so any help would be good - even if you could just try it on your own computer and tell me whether you can replicate the problem. (You can use TaskManager or Perfmon to watch the memory increase).
BTW I'm using Mogre 1.4.8, VS 2008, Windows XP and a nVidia 8600 GTS


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
using MogreFramework;
using Mogre;

namespace TestBed
{
static class Program
{
[STAThread]
static void Main()
{
OgreStartup ogre = new OgreStartup();
ogre.Go();
}
}

class OgreStartup
{
Root mRoot = null;
SceneManager mgr;
Camera mCamera;
SceneNode mCameraNode;
float terrainDepth = 1500;


public void Go()
{
CreateRoot();
DefineResources();
SetupRenderSystem();
CreateRenderWindow();
InitializeResourceGroups();
CreateScene();
CreateFrameHandler();
StartRenderLoop();
}

void CreateRoot()
{
mRoot = new Root();
}

void DefineResources()
{
ConfigFile cf = new ConfigFile();
cf.Load("resources.cfg", "\t:=", true);
ConfigFile.SectionIterator seci = cf.GetSectionIterator();
String secName, typeName, archName;

while (seci.MoveNext())
{
secName = seci.CurrentKey;
ConfigFile.SettingsMultiMap settings = seci.Current;
foreach (KeyValuePair<string, string> pair in settings)
{
typeName = pair.Key;
archName = pair.Value;
ResourceGroupManager.Singleton.AddResourceLocation(archName, typeName, secName);
}
}

}

void SetupRenderSystem()
{
if (!mRoot.ShowConfigDialog())
throw new Exception("The user canceled the configuration dialog.");
}

void CreateRenderWindow()
{
mRoot.Initialise(true, "Main Ogre Window");
}

void InitializeResourceGroups()
{
TextureManager.Singleton.DefaultNumMipmaps = 5;
ResourceGroupManager.Singleton.InitialiseAllResourceGroups();
}
void CreateCamera()
{
mCameraNode = mgr.CreateSceneNode("cameraNode");
mCamera = mgr.CreateCamera("PlayerCam");
mCameraNode.AttachObject(mCamera);
mCameraNode.Position = new Vector3(10, 60, 10);
mCamera.LookAt(new Vector3(30, 60, 30));
mCamera.NearClipDistance = 1;

}
void CreateScene()
{
mgr = mRoot.CreateSceneManager(SceneType.ST_EXTERIOR_CLOSE);
CreateCamera();
mRoot.AutoCreatedWindow.AddViewport(mCamera);
mgr.SetWorldGeometry("terrain.cfg");

}

void CreateFrameHandler()
{
mRoot.FrameStarted += new FrameListener.FrameStartedHandler(FrameStarted);
}
bool FrameStarted(FrameEvent evt)
{


// Move camera forward over the terrain
Vector3 newPos = mCameraNode.Position + new Vector3(60, 0, 60) * evt.timeSinceLastFrame;
mCameraNode.Position = newPos;

// if we're at the end of the terrain
if (mCameraNode.Position.z >= terrainDepth)
{
// rebuildScene();
// Reset camera to original place
mCameraNode.Position = new Vector3(10, 60, 10);
// Set world geometry to another terrain - although I'm using the default one again for this demo
mgr.SetWorldGeometry("terrain.cfg");



}
return true;

}
void StartRenderLoop()
{
mRoot.StartRendering();
}
}
}



JoeC

17-06-2008 10:25:06

Hmm, I can see this one has aroused lots of interest :wink:
For what its worth I've found a work around for this. The trick is to make sure that you have a different scene manager for each terrain and swap between them using the Multiple Scene Managers technique described in the tutorials.
That way you can make sure that setWorldGeometry is only ever called once for each scene manager which seems to stop the memory increase.
I did also try completely removing the scene manager but this doesn't help.

I can use use this workaround to solve my problem but it'll mean I have to rewrite my project a lot so if anyone has any other cunning fixes I'd be very interested to know.

Cheers

J