Dynamic terrain loading

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
Tomatix
Posts: 4
Joined: Thu Aug 04, 2011 7:55 pm
Location: Trondheim, Norway
Contact:

Dynamic terrain loading

Post by Tomatix »

I am new to this engine, and actually new to 3D programming in general. My C++ skill is rather intermediate though.

Currently I am experimenting with loading a large heightmap (8192x8192) dynamicly into my scene.
What I've done is splitting the heightmap into smaller parts.
When the camera moves to the boundaries to the "other" parts of the heightmap, I am trying to load that heightmap and delete the previous one.

Currently, I've acheived that issue, but however, it's taking like forever to load (the program "hangs").
What I'd like to try is put this heightmap loading into an own thread, but after searching the forum I've learned that Irrlicht isn't threadsafe.
I've also tried to put the terrain loading into an own pthread, but it segfaults, hence Irrlicht isn't threadsafe.

So, um, Is there any way to load a heightmap (and other nodes for that sake) without having the program locking while loading? I've read a forumthread where someone recommends the ISceneUserDataSerializer class, but I have no idea how to use it.
Insomniacp
Posts: 288
Joined: Wed Apr 16, 2008 1:45 am
Contact:

Re: Dynamic terrain loading

Post by Insomniacp »

They way I have done it is load the height map into memory in another thread so the program doesn't lock while loading it from the disk. When making the actual node you must use the scene manager which means you can't thread that part off. loading a very large one will take a lot of time but since you broke them into smaller ones it will be short. I broke mine down to 129x129 segments and it loads without issues. One thing you should do is only load one map at a time in the scene manager. Since you will most likely be loading multiple ones you don't want it to get stuck loading them so your frame rate drops too much. I for example if I load a map I make it wait 1/8 second before loading another. That way the frame rate remains decent. You can always tweak the variable but the 1/8 second works fine in my application. It may be possible to do more loading before going into the live scene manager but I haven't had the need to do that yet. You would probably be able to load it entirely and then change a few variables and register it in the live scene manager if required.
Tomatix
Posts: 4
Joined: Thu Aug 04, 2011 7:55 pm
Location: Trondheim, Norway
Contact:

Re: Dynamic terrain loading

Post by Tomatix »

I feel stupid asking this, but do you have an code example showing how to acheive this; loading the heightmap to a memory, and then put that to the scene?
I'm one of those who learn by example... Although I do RTFM quite often ;)
Insomniacp
Posts: 288
Joined: Wed Apr 16, 2008 1:45 am
Contact:

Re: Dynamic terrain loading

Post by Insomniacp »

hmmm, it is highly integrated and abstracted... But I can copy the function that loads the maps... Actually I forgot I do not use images for heightmaps. You can use IFileSystem to open the file as an IReadFile, then pass that into the creation of the terrain scene node. I actually just use an ifstream to load my heights in.

Now that I remember you can use http://irrlicht.sourceforge.net/forum/v ... =1&t=44492 I have a post in there which does it. I am not sure if you can use the driver to load the image into memory though I can't remember if that part is thread safe or not.
Tomatix
Posts: 4
Joined: Thu Aug 04, 2011 7:55 pm
Location: Trondheim, Norway
Contact:

Re: Dynamic terrain loading

Post by Tomatix »

Tried your codes, but it gives me weird errors like "Not really a png".
Although, I don't think loading the _files_ is what creates this 5-second program hang, but rather the converting process between PNG heightmap -> actual mesh / node.
So my question would be, how can I convert the PNG heightmap to a usable terrain scenenode, without hanging my program? I'd like to move the camera around while it's loading...
It's meant to load nearby terrain tiles and remove those who are too far away...

I've read slightly about Arras Tiled Terrain, not sure if that's what I need, but the link in that forumthread is dead.
Insomniacp
Posts: 288
Joined: Wed Apr 16, 2008 1:45 am
Contact:

Re: Dynamic terrain loading

Post by Insomniacp »

hmmm, do those errors occur when you load it normally? It could be you didn't allocate enough space to load the entire file. Anyway if you are having time problems where it gets stuck when calling smgr->addTerrainSceneNode, IE a delay when creating the mesh I will look into helping you with that. It may not be possible to speed it up though without a lot of effort.
Tomatix
Posts: 4
Joined: Thu Aug 04, 2011 7:55 pm
Location: Trondheim, Norway
Contact:

Re: Dynamic terrain loading

Post by Tomatix »

These errors does not occur when I load the heightmap normally.

What I am really doing is; I've got 256 Tiles of an 8192x8192 px map.
That makes each tile 512x512 big.
But since I want "seamless" transitions between each tile, I've made a small Qt software to convert each tile into a 1536x1536 "tile", which actually are 3x3 tiles into one .png... if you get what I mean.
I do still have 256 Files after this conversion, where the physical center of each heightmap, is the actual Tile, while the 8 tiles surrounding it (still same heightmap-file), is, the surrounding tiles/heightmap... This is why these heightmap files have to be this large... Hard to explain O_o

What I want to acheive is, seamless transitions, by overlapping an edge... I need them this big because this is ca. whats in a players view.

I could only load the 512x512 by themselves without using these large 3x3 (1536x1536 px), but that gives no seamless transitions because of the terrain smoothing...

So I don't think its possible to speed up the converting process much, because loading a 1536x1536 takes a while anyway...
Insomniacp
Posts: 288
Joined: Wed Apr 16, 2008 1:45 am
Contact:

Re: Dynamic terrain loading

Post by Insomniacp »

I would suggest using the driver to load the image into an IImage, driver->createImageFromFile(), in a separate thread or before hand, then use the code in my last link to convert the image into something that can load a terrain node. This will speed it up slightly since it won't be reading from a file rather from memory. Still loading a 1500x1500 terrain node will take some time in the scene manager. It would be possible to build the entire node before hand but I have not attempted this in the past... I will look into it because I think a lot of people would benefit from threaded loading of nodes and things.
Post Reply