Remove terrain scene node from memory and scenemanager

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Mattan360
Posts: 5
Joined: Mon Aug 16, 2010 1:51 pm

Remove terrain scene node from memory and scenemanager

Post by Mattan360 »

Hi!
I can't seem to find an answear to this one;
I've got a couple of terrains (9 * (1025 * 1025)), wich need to be loaded and displayed as they are in the same game world, I tried to do this, I took about one minute to load it and I got a descent frame rate, ~60.
When I checked the memory useage it was about 1,5 gb of ram wich is just too much. So I thought of loading them during run-time when needed, I did this, got it loading, when I got close to them.
But to my problem: When I was trying to unload them, when I got far enought away from them, I call the function terrain->removeAll();
This doesn't remove the terrain from the memory, neither from the scene, I tried to call ZeroMemory on it, but then I get an exception from the scenemanager when I try to draw the scene!

Please help me!
(Sorry for bad gramma, I'm a bit tired :P)
Grz-
Posts: 62
Joined: Wed Dec 26, 2007 1:06 pm

Post by Grz- »

You need to call removeAllHardwareBuffers like that after:

Code: Select all

                terrain->remove();
                driver->removeAllHardwareBuffers();
Mattan360
Posts: 5
Joined: Mon Aug 16, 2010 1:51 pm

Post by Mattan360 »

Grz- wrote:You need to call removeAllHardwareBuffers like that after:

Code: Select all

                terrain->remove();
                driver->removeAllHardwareBuffers();
It worked fine :)

But I found this bug when the third terrain is created it is a little bit off :P
With other words it has been moved about a patch (33 units), down, wich makes the LOD fail as it thinks the camera is further down the terrain than it is, displaying only low LOD :(

Here is my code:

Code: Select all

		for(int y = 0; y < 3; y++)
		{
			for(int x = 0; x < 3; x++)
			{
				//Load it
				f64 distance = camera->getAbsolutePosition().getDistanceFrom(core::vector3df(x * (1025 * TERRAINSIZE) + 512 * TERRAINSIZE, camera->getAbsolutePosition().Y,
					y * (1025 * TERRAINSIZE) + 512 * TERRAINSIZE));
				if(distance < (1025 * TERRAINSIZE))
				{
					if(!loaded[x][y])
					{
						core::stringw heightmap = "0000";
						heightmap += x;
						heightmap += "_0000";
						heightmap += y;
						heightmap += ".bmp";
			
						terrains[x][y] = smgr->addTerrainSceneNode(heightmap, 0, -1, core::vector3df(x * (1025 * TERRAINSIZE), 0, y * (1025 * TERRAINSIZE)), core::vector3df(0, 180, 0),
							core::vector3df(TERRAINSIZE, 100, TERRAINSIZE), video::SColor (255, 255, 255, 255), 5, scene::ETPS_33, 15);
						terrains[x][y]->setMaterialTexture(0, textures[x][y]);
						terrains[x][y]->setMaterialFlag(EMF_LIGHTING, true);
						terrains[x][y]->setMaterialFlag(video::EMF_WIREFRAME, true);

						/*scene::ITriangleSelector* selector = smgr->createTerrainTriangleSelector(terrains[x][y], 0);
						terrains[x][y]->setTriangleSelector(selector);

						// create collision response animator and attach it to the camera
						scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
							selector, camera, core::vector3df(60, 400, 60), core::vector3df(0, 0, 0));
						selector->drop();
						camera->addAnimator(anim);
						anim->drop();*/

						//std::cout << "Created terrain at " << x << "," << y << std::endl;
						loaded[x][y] = true;
					}
				}
				//Unload it
				else
				{
					if(loaded[x][y])
					{
						terrains[x][y]->remove();
						driver->removeAllHardwareBuffers();
						//ZeroMemory(terrains[x][y], sizeof(terrains[x][y]));
						loaded[x][y] = false;
					}
				}
			}
		}
Also, how do I thread the loading of the terrain as it takes too long time to load the terrain in the same thread as the game (3 secs) :P

Thanks :)
Post Reply