Generating terrain from a plain c++ array

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.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Hmm, maybe try with a 129x129 field first. Maybe something breaks due to the change from 16bit to 32bit indices.
Alastriona
Posts: 23
Joined: Sat Apr 17, 2010 3:36 pm

Post by Alastriona »

hybrid wrote:Hmm, maybe try with a 129x129 field first. Maybe something breaks due to the change from 16bit to 32bit indices.
Nope thats not it. I'm going to take a little break and clear my head. Then I should prolly start over from scratch and only add exactly what I need so no other code could possibly be errorneous.
bonsalty
Posts: 120
Joined: Thu Dec 10, 2009 1:30 pm
Location: Budapest,Hungary

Post by bonsalty »

I tried fifty different combinations without progress.
The first should be a parameter indicating the beginning of the pointer?! and when casting you have this:

(void *)&lpBits[0]

The other question is the size. It is in bytes if Im right. So this seems to be OK for you, but for a vector size= sizeof(vector<float>) * lpBits.size()??? Uhh seems too extreme...

The rawloader will cause error anyway:

"Could not load terrain, because file could not be opened." If you have a jpeg format or bmp it will load it, but simple bytestream or floatstream, how? Has anybody done this before, I checked 500 posts without any example or solution to this. I bet we are doing something wrong . :roll:
Tomi
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

You don't have to cast addresses to void*, just take the address. And the size is sizeof(elementtype)*elementcount. Just make sure you use the correct numbers as RAW loader params as well, and don't give the RAW image to the usual constructor right away.
Alastriona
Posts: 23
Joined: Sat Apr 17, 2010 3:36 pm

Post by Alastriona »

hybrid wrote:You don't have to cast addresses to void*, just take the address. And the size is sizeof(elementtype)*elementcount. Just make sure you use the correct numbers as RAW loader params as well, and don't give the RAW image to the usual constructor right away.
By correct numbers you mean the amount of bits per pixel? So 32 for an int, 16 for short and 8 for char?
Alastriona
Posts: 23
Joined: Sat Apr 17, 2010 3:36 pm

Post by Alastriona »

I'm about to give up on this. Can't seem to get it to work. Whenever I call the loadHeightMapRAW() function everything goes wrong. I don't get any errors from any of the other code if I comment out the call to loadHeightMapRAW().

Heres the complete code. All I excluded is the code that generates the array. terGen is my terrainGen object and getArray returns a char* to an array of char.

Code: Select all

video::E_DRIVER_TYPE driverType=driverChoiceConsole();
        if (driverType==video::EDT_COUNT)
                return 1;
	
	irr::SIrrlichtCreationParameters params;
	params.DriverType=driverType;
	params.WindowSize=core::dimension2d<u32>(640, 480);
	IrrlichtDevice* device = createDeviceEx(params);

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

	scene::ICameraSceneNode* camera = smgr->addCameraSceneNodeFPS(0,100.0f,1.2f);
	IAnimatedMesh* mesh = smgr->getMesh("../media/sydney.md2");
    
	//start terrain part
	io::IReadFile* file = device->getFileSystem()->createMemoryReadFile(terGen->getArray(), 129*129*sizeof(char), "heightmap");
	scene::ITerrainSceneNode* terrain = smgr->addTerrainSceneNode(	
		NULL,	
		0,					// parent node
		-1,					// node id
		core::vector3df(0.f, 0.f, 0.f),		// position
		core::vector3df(0.f, 0.f, 0.f),		// rotation
		core::vector3df(40.f, 4.4f, 40.f),	// scale
		video::SColor ( 0, 0, 0, 255 ),	// vertexColor
		5,					// maxLOD
		scene::ETPS_17,				// patchSize
		4,					// smoothFactor
		true
		); 		
	terrain->loadHeightMapRAW(file,8,true);
	//end terrain part

	scene::ISceneNode* skybox=smgr->addSkyBoxSceneNode(
                driver->getTexture("../media/irrlicht2_up.jpg"),
                driver->getTexture("../media/irrlicht2_dn.jpg"),
                driver->getTexture("../media/irrlicht2_lf.jpg"),
                driver->getTexture("../media/irrlicht2_rt.jpg"),
                driver->getTexture("../media/irrlicht2_ft.jpg"),
                driver->getTexture("../media/irrlicht2_bk.jpg"));
	scene::ISceneNode* skydome=smgr->addSkyDomeSceneNode(driver->getTexture("../media/skydome.jpg"),16,8,0.95f,2.0f);
	
	IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode( mesh );

	while(device->run())
	{        
		driver->beginScene(true, true);
		smgr->drawAll();                
		env->drawAll();
		driver->endScene();
	}
    device->drop(); 
The errors that I get when I call loadHeightMapRAW() are either Buffer overflow or Access violation. This hints that the function is trying to access memory it wasn't assigned. What I don't know if it is my fault for passing the wrong parameters to the function or because the function is broken.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

Alastriona wrote:The errors that I get when I call loadHeightMapRAW() are either Buffer overflow or Access violation. This hints that the function is trying to access memory it wasn't assigned. What I don't know if it is my fault for passing the wrong parameters to the function or because the function is broken.
it seems to work for me, I get no errors !!! :shock:
of course I must say that I created a simple randomizing getArray function, bc I don't know your function for this... ;)
so my getArray function looks like this:

Code: Select all

char* getArray(){
  char* ca = (char*)malloc(129*129*sizeof(char));
  for(u32 t = 0; t < 129*129; ++t){
    ca[t] = rand() % 20;
  }
  return ca;
}
and then the terrain looks like this (using the texture from the sdk):
Image
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Alastriona
Posts: 23
Joined: Sat Apr 17, 2010 3:36 pm

Post by Alastriona »

What plaform/IDE/compiler are you using? I'll be trying with your function just to make sure it isn't my terrain generation at fault here.

EDIT: It does not work for me. still getting a buffer overflow. Even after I disable all my generation code and replace it with your function.

I'm using microsoft visual studio 2008 on windows to build. Irrlicht 1.7.1.
Last edited by Alastriona on Sun Apr 18, 2010 8:02 pm, edited 1 time in total.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

Alastriona wrote:What plaform/IDE/compiler are you using?
Irrlicht 1.7.1 and Code::Blocks with MinGW under WinXP-pro (sp2)...
Last edited by Acki on Sun Apr 18, 2010 8:01 pm, edited 1 time in total.
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Even if no values would be generated, the array could be used. So it's obviously something else. Just try a debugger.
Alastriona
Posts: 23
Joined: Sat Apr 17, 2010 3:36 pm

Post by Alastriona »

hybrid wrote:Even if no values would be generated, the array could be used. So it's obviously something else. Just try a debugger.
I have been using a debugger. But since I used the precompiled version of irrlicht it doesn't have debug info built into it so I can only step through my own code atm. I'm going to rebuilt Irrlicht now.

If this doesn't work I guess I could try MinGW as a compiler and see if that does anything.
Alastriona
Posts: 23
Joined: Sat Apr 17, 2010 3:36 pm

Post by Alastriona »

Uuuuhm okaaaay. I just rebuilt Irrlicht engine in debug mode to start debugging my issue. Now everything works fine though. I'll try to rebuild in release mode as well and see if it is a compiler setting issue.

Using my terrain generating function doesn't give the desired result yet. but at least I'm not stuck on buffer overflow errors anymore.

The moral of this story? I'm not quite sure. Maybe: Always build from source and never use precompiled binaries?
bonsalty
Posts: 120
Joined: Thu Dec 10, 2009 1:30 pm
Location: Budapest,Hungary

Post by bonsalty »

I will try your code . Isnt the multibyte,unicode settings the source of the problem?

I think you will have a headache if you want to change the size from 129x129 to custom size.Thats why resizing textures is easier then writing your own interpolation routine.

Hybrid is the code working correctly on your machine,I posted about the terrainscenenode mem bug. I have a bad feeling, if you want to alter your terrain constantly you will have to call the rawloader. And if you do this, it will
pump up your memory. I will check this out.
Tomi
Alastriona
Posts: 23
Joined: Sat Apr 17, 2010 3:36 pm

Post by Alastriona »

Success! It all looks quite good in Irrlicht with this standard texture. Now I guess I'm going to have to try some different algorithms such as perlin noise. Hoping to perhaps turn this thing into a terrain generation library someday. Big thanks for all the support guys.
bonsalty wrote:I will try your code . Isnt the multibyte,unicode settings the source of the problem?

I think you will have a headache if you want to change the size from 129x129 to custom size.Thats why resizing textures is easier then writing your own interpolation routine.
I am able to resize to other sizes easily. Though I can't say I've tried many. and all my terrain sizes are 2x+1 as per requirement of the algorithm I'm using.


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

Post by hybrid »

Larger sizes should work as expected, I did work with huge terrains some time ago. Was just to make sure that it's not the problems here.
I didn't check any of the code for real, just checking with my eyes. No more time for anything else, sorry. Also no time for the debugging of the terrain node yet. Moreover, it's probably a windows only problem...
Post Reply