TerrainMesh issues in 1.7.1

You discovered a bug in the engine, and you are sure that it is not a problem of your code? Just post it in here. Please read the bug posting guidelines first.
Post Reply
CarlS
Posts: 86
Joined: Wed May 09, 2007 1:21 am
Contact:

TerrainMesh issues in 1.7.1

Post by CarlS »

Hi all,

I've run into some issues using the TerrainMesh when I updated my 3d viewer from Irrlicht-1.6 to 1.7.1.
It looks like the textures are being applied incorrectly to the terrain and the memory needed by the textures has increased.
I'm including a screenshot comparing the two versions of Irrlicht driven by the same program, logs showing what got piped to the screen and the memory usage, and the source code used to do the tests.

If there's something I need to modify in my code to use the TerrainMesh in 1.7.1, or some patch that could be applied to Irrlicht, I'd like to hear about it.
If this is an actual bug in Irrlicht, feel free to move this into the Bug Reports secion.

Here's the screenshot:

Image

Here's the log from version 1.6:
Irrlicht Engine version 1.6
Microsoft Windows XP Professional Service Pack 3 (Build 2600)
Using renderer: Direct3D 9.0
NVIDIA GeForce 8600 GT nv4_disp.dll 6.14.11.5827
Generated terrain texture (256x256): terrain175225_0
Generated terrain texture (256x256): terrain175225_1
Generated terrain texture (256x256): terrain175225_2
Generated terrain texture (256x256): terrain175225_3
Generated terrain texture (16x256): terrain175225_4
Generated terrain texture (256x256): terrain175225_5
Generated terrain texture (256x256): terrain175225_6
Generated terrain texture (256x256): terrain175225_7
Generated terrain texture (256x256): terrain175225_8
Generated terrain texture (16x256): terrain175225_9
Generated terrain texture (256x256): terrain175225_10
Generated terrain texture (256x256): terrain175225_11
Generated terrain texture (256x256): terrain175225_12
Generated terrain texture (256x256): terrain175225_13
Generated terrain texture (16x256): terrain175225_14
Generated terrain texture (256x256): terrain175225_15
Generated terrain texture (256x256): terrain175225_16
Generated terrain texture (256x256): terrain175225_17
Generated terrain texture (256x256): terrain175225_18
Generated terrain texture (16x256): terrain175225_19
Generated terrain texture (256x16): terrain175225_20
Generated terrain texture (256x16): terrain175225_21
Generated terrain texture (256x16): terrain175225_22
Generated terrain texture (256x16): terrain175225_23
Generated terrain texture (16x16): terrain175225_24

CPU = 50%
Memory Usage = 24,440K

Here's the log from version 1.7.1:
Irrlicht Engine version 1.7.1
Microsoft Windows XP Professional Service Pack 3 (Build 2600)
Using renderer: Direct3D 9.0
NVIDIA GeForce 8600 GT nv4_disp.dll 6.14.11.5827
Generated terrain texture (1024x1024): terrain174839_0
Generated terrain texture (1024x1024): terrain174839_1
Generated terrain texture (1024x1024): terrain174839_2
Generated terrain texture (1024x1024): terrain174839_3
Generated terrain texture (1024x1024): terrain174839_4
Generated terrain texture (1024x1024): terrain174839_5
Generated terrain texture (1024x1024): terrain174839_6
Generated terrain texture (1024x1024): terrain174839_7
Generated terrain texture (1024x1024): terrain174839_8
Generated terrain texture (1024x1024): terrain174839_9
Generated terrain texture (1024x1024): terrain174839_10
Generated terrain texture (1024x1024): terrain174839_11
Generated terrain texture (1024x1024): terrain174839_12
Generated terrain texture (1024x1024): terrain174839_13
Generated terrain texture (1024x1024): terrain174839_14
Generated terrain texture (1024x1024): terrain174839_15
Generated terrain texture (1024x1024): terrain174839_16
Generated terrain texture (1024x1024): terrain174839_17
Generated terrain texture (1024x1024): terrain174839_18
Generated terrain texture (1024x1024): terrain174839_19
Generated terrain texture (1024x1024): terrain174839_20
Generated terrain texture (1024x1024): terrain174839_21
Generated terrain texture (1024x1024): terrain174839_22
Generated terrain texture (1024x1024): terrain174839_23
Generated terrain texture (1024x1024): terrain174839_24

CPU = 50%
Memory Usage = 156,388K
Here's the source code derived from the terrain tute and run with VS2005:

Code: Select all

// Edit following 3 lines for correct version of Irrlicht
#include "C:\Program Files\irrlicht-1.7.1\include\irrlicht.h"
#pragma comment(lib, "C:\\Program Files\\irrlicht-1.7.1\\lib\\Win32-visualstudio\\Irrlicht.lib")
// Remember to add "C:\Program Files\irrlicht-1.7.1\bin\Win32-VisualStudio" to Path in Environment Variables

#include <iostream>
#include <iomanip>
using namespace irr;
using namespace scene; // needed for mesh import
using namespace video; // needed for EMF_LIGHTING

class MyEventReceiver : public IEventReceiver
{
public:
	MyEventReceiver(scene::ISceneNode* terrain)
	{
		// store pointer to terrain so we can change its drawing mode
		Terrain = terrain;
	}
	bool OnEvent(const SEvent& event)
	{
		// check if user presses the key 'W' or 'D'
		if (event.EventType == irr::EET_KEY_INPUT_EVENT &&
			!event.KeyInput.PressedDown)
		{
			switch (event.KeyInput.Key)
		{
			case irr::KEY_KEY_W: // switch wire frame mode
				Terrain->setMaterialFlag(video::EMF_WIREFRAME,
					!Terrain->getMaterial(0).Wireframe);
				return true;
			case irr::KEY_KEY_D: // toggle detail map
				Terrain->setMaterialType(
					Terrain->getMaterial(0).MaterialType
					== video::EMT_SOLID ?
					video::EMT_DETAIL_MAP : video::EMT_SOLID);
				return true;
			}
		}
		return false;
	}
private:
	scene::ISceneNode* Terrain;
};

int main()
{
	float fTerrainScale = 40.f;
	int iTerrainType;
	iTerrainType = 1;
	std::cout << "Enter Terrain Type 1 (mesh) or 2 (LOD)" << std::endl;
	std::cin >> iTerrainType;

	video::E_DRIVER_TYPE driverType = video::EDT_DIRECT3D9;
	// create device
	irr::SIrrlichtCreationParameters params;
	params.DriverType=driverType;
	params.WindowSize=core::dimension2d<u32>(320, 240);
	IrrlichtDevice* device = createDeviceEx(params);

	if (device == 0)
		return 1; // could not create selected driver.

	video::IVideoDriver* driver = device->getVideoDriver();
	scene::ISceneManager* smgr = device->getSceneManager();
	gui::IGUIEnvironment* env = device->getGUIEnvironment();
	driver->setTextureCreationFlag(video::ETCF_ALWAYS_32_BIT, true);

// add camera
	scene::ICameraSceneNode* camera = smgr->addCameraSceneNodeFPS(0,100.0f,2.0f);
	camera->setPosition(core::vector3df(2000.0f,5000.f,0.0f));
	camera->setTarget(core::vector3df(0.0f,0.0f,0.0f));
	camera->setFarValue(200000.0f);

	device->getCursorControl()->setVisible(false); // disable mouse cursor

	if (iTerrainType == 1){ //Terrain Mesh

		video::IImage* colorMapImage = driver->createImageFromFile("C:/Program Files/irrlicht-1.7.1/media/terrain-texture.jpg");
		video::IImage* heightMapImage = driver->createImageFromFile("C:/Program Files/irrlicht-1.7.1/media/terrain-heightmap.bmp");

		scene::IAnimatedMesh* terrain =   smgr->addTerrainMesh ("TerrainMeshName", colorMapImage, heightMapImage,
			core::dimension2d< f32 >(fTerrainScale, fTerrainScale), // size of a pixel
			8.f*fTerrainScale); // maximum height

		scene::ISceneNode* room = 0;
		ISceneNode* node = smgr->addAnimatedMeshSceneNode(terrain);
		node->setMaterialFlag(video::EMF_LIGHTING, false);
		node->setPosition(core::vector3df(-5000,0,-5000));

}	else if (iTerrainType == 2){ // height map needs to be 2n+1 resolution
// add terrain scene node
		scene::ITerrainSceneNode* terrain = smgr->addTerrainSceneNode(
			"C:/Program Files/irrlicht-1.7.1/media/terrain-heightmap.bmp"
			,0,-1
			,core::vector3df(0.0f,0.0f,0.0f) // position
			,core::vector3df(0.0f,0.0f,0.0f) // rotation
			,core::vector3df(1.0f,1.0f,1.0f) // scale
			,video::SColor(255,255,255,255)
			,0 // Level of detail
			,ETPS_17 // terrain patch size
			,0 //smooth factor
			);

	terrain->setScale(core::vector3df(fTerrainScale, 1.0f, fTerrainScale));
	terrain->setPosition(core::vector3df(-5000,0,-5000));
	terrain->setMaterialFlag(video::EMF_LIGHTING, false);
	terrain->setMaterialTexture(0, driver->getTexture("C:/Program Files/irrlicht-1.7.1/media/terrain-texture.jpg"));
	terrain->setMaterialTexture(1, driver->getTexture("C:/Program Files/irrlicht-1.7.1/media/detailmap3.jpg"));
	terrain->setMaterialType(video::EMT_DETAIL_MAP);
	terrain->scaleTexture(1.0f, 20.0f);
}

	int lastFPS = -1;
	while(device->run())
		if (device->isWindowActive())
		{
			driver->beginScene(true, true, 0 );
			smgr->drawAll();
			env->drawAll();
			driver->endScene();

		// display frames per second in window title

		int fps = driver->getFPS();
		if (lastFPS != fps)
		{
			core::stringw str = L"Terrain Mesh - Irr-1.7.1 ";
			str += " FPS:";
			str += fps;
			device->setWindowCaption(str.c_str());
			lastFPS = fps;
		}
		}
	device->drop();
	return 0;
}
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

I don't see any place where you create your textures. But obviously, you're creating 1024x1024 textures instead of 256x256 ones, which of course uses much more memory. And since the terrain is not necessarily working at such huge dimensions, this should also be the source of your other problems.
CarlS
Posts: 86
Joined: Wed May 09, 2007 1:21 am
Contact:

Post by CarlS »

Hi Hybrid,

Sorry if I wasn't that clear.

The code that I pasted works fine as a standalone example program with Irrlicht-1.6 and also 1.5, where a TerrainMesh is created using images for the height and color maps.
The problem is that this program no longer works in Irrlicht-1.7.1.

At my job, we’ve been using the TerrainMesh in our 3d viewer for the past couple of years, and it would be a hard sell to change that approach to keep up-to-date with Irrlicht.

The example I posted should probably have been cut back a bit to just show the TerrainMesh and not the TerrainSceneNode.

Thanks,
--Carl
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Ok, now I understood what you made. This is indeed a bug, due to some changes in the ITexture interface. I fixed the code of the texture creation wrongly, and we did not have a test for the terrain mesh. I'll add one soon, and also fix the texture generation back to the usual numbers. You'll need to fix the code manually, though, because the next release might take some time.
CarlS
Posts: 86
Joined: Wed May 09, 2007 1:21 am
Contact:

Post by CarlS »

If a fix is possible, then that's good news.
I can rebuild a patched version of 1.7.1 if you can let me know what changes need to be made in which files.

Thanks,
--Carl
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

A first patch is here: http://irrlicht.svn.sourceforge.net/vie ... threv=3230
However, there are still some lines in the output, so I think I need to fix that code a little more.
CarlS
Posts: 86
Joined: Wed May 09, 2007 1:21 am
Contact:

Post by CarlS »

Thanks very much.
I just did a rebuild and a preliminary check; it looks much improved.
I'll take a better look at it tomorrow and also see if there have been any further updates to the code.

--Carl
vik
Posts: 2
Joined: Sun Oct 14, 2012 10:40 pm
Location: OZ land

Re: TerrainMesh issues in 1.7.1

Post by vik »

i did a rebuild and it worked as hydrid said.

is it possible to set other textures on the terrain ( setMaterialTexture(1,... )) so they will be stretched over the whole of terrain and not tiled. I tried using "makePlanarTextureMapping", but I need it to stretch X different to Y.

In my hlsl shader, if I set a pixel to something from other then material texture 0, looks like it sets it at the block position (terrain mesh = collection of generated blocks from height map), so it makes it very hard to use other textures.

I understand these 2 questions are linked, as only texture 0 is loaded during the creation of terrain mesh, is it somehow possible to "copy texture 0 attributes" if i want to add more textures later on?

am i missing something simple and making an idiot out of myself here? (btw i use 8 textures limit, not sure if it makes any difference)
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: TerrainMesh issues in 1.7.1

Post by hendu »

In a shader, _you_ control what texcoords are used. Irrespective of what texture is used.
vik
Posts: 2
Joined: Sun Oct 14, 2012 10:40 pm
Location: OZ land

Re: TerrainMesh issues in 1.7.1

Post by vik »

Sorry hendu, you have totally miss understood my question

when you set another texture or even a texture 0 after the the terrain mesh has been created, it sets the texture on each block and not the right part of the texture on the right block.
Post Reply