New Tiled Terrain Scene Node [works with Irr 1.5]

Announce new projects or updates of Irrlicht Engine related tools, games, and applications.
Also check the Wiki
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

radubolovan >> yep ...you did found it. Its memory leak evman had discovered already. I did use older versions of files and overlooked that. In fact I was looking on that code exactly for this error but for some strange reason I did not see it (it was just too late probably).

code should be:

Code: Select all

virtual ~array2d()
   {
      if(w && h)
      {
         for(int i=0; i<w; i++) delete [] data[i];// [] was missing
         delete [] data;
      }
   }
That should solve it. Please replace it for now yourself I need still to set material problems so I will release patch for all at once. (It may take some time)

Thanks.
radubolovan
Posts: 60
Joined: Tue Nov 13, 2007 7:03 pm
Location: Bucharest - Romania
Contact:

Post by radubolovan »

The problem is not resolved! :(

EDIT: why don't you use the irrList class or irrArray class for that?
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

radubolovan >>
why don't you use the irrList class or irrArray class for that?
Because I can't. array2d is 2 dimensional array while irrlicht list and array are linear.
You can't do irrarray[x][y] but you can do myarray2d(x,y). There of course is workaround which allow using 1D array as 2D array but you need to calculate 2D position from 1D or back each time you want to access element inside.
The problem is not resolved!
Well, I am out of ideas here. I did run whole demo through my compiler but I do not have crash on program exit so I cant track it. If you will be able to dig it up I'll be thankful.
Darktib
Posts: 167
Joined: Sun Mar 23, 2008 8:25 pm
Location: France

Post by Darktib »

Thanks for the update! :D

I've downloaded it, i will test on DX and OGL.
Darktib
Posts: 167
Joined: Sun Mar 23, 2008 8:25 pm
Location: France

Post by Darktib »

All works well :D !

I have made a few changes, so now in example the detailmap texture is devided in 25 parts.
When you call randomizeUV, sometimes some tiles has the UV coord 0-1, 0-0, 1-0, 1-1...

@arras : check your PM, i have send the zip (DX dll + compiled demo + sources + C::B project + some artwork i have made when i had free time^^ inside), you will do what you want whis this^^
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

Thanks Darktib. I'll look at it later, I am bit short of time right now. Did you just placed those 25 tiles around randomly or in some ordered fashion?
radubolovan
Posts: 60
Joined: Tue Nov 13, 2007 7:03 pm
Location: Bucharest - Romania
Contact:

Post by radubolovan »

I found the crashing bug :D
The next code it's not good:

Code: Select all

virtual ~array2d()
{
	if(w && h)
	{
		for(int i=0; i<w; i++)
		{
			for(int i=0; i<h; i++) delete data[i];
			delete [] data;
		}
		delete [] data;
	}
}
replace with this one

Code: Select all

virtual ~array2d()
{
	if(w && h)
	{
		for( int i=0; i<w; i++)
		{
			delete [] data[i];
		}
		delete [] data;
	}
}
In constructor you have this code

Code: Select all

array2d(s32 width, s32 height) : w(width), h(height)
{
	data = new T*[w];
	for(int i=0; i<w; i++) data[i] = new T[h];
}
So why in the destructor you have a double for?
B@z
Posts: 876
Joined: Thu Jan 31, 2008 5:05 pm
Location: Hungary

Post by B@z »

yeah it solved

Code: Select all

   virtual ~array2d()
   {
      if(w && h)
      {
         for(int i=0; i<w; i++) delete data[i]; // change this to delete [] data[i]; and it works
         delete [] data;
      }
   }
thanks~!
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

radubolovan >> I wonder where do you have that code from?:

Code: Select all

virtual ~array2d()
{
   if(w && h)
   {
      for(int i=0; i<w; i++)
      {
         for(int i=0; i<h; i++) delete data[i];
         delete [] data;
      }
      delete [] data;
   }
}
It's not code from my download link.
radubolovan
Posts: 60
Joined: Tue Nov 13, 2007 7:03 pm
Location: Bucharest - Romania
Contact:

Post by radubolovan »

Sorry! My bad! :(
Darktib
Posts: 167
Joined: Sun Mar 23, 2008 8:25 pm
Location: France

Post by Darktib »

The 25 texture tiles are placed randomly, but the principle can be used to set several textures( rock, grass, etc...)

I think i have found another bug, with the normals : apperently the normals smoothing doesn't work...
So , i have coded a smoothing function (which smooth vertex), for the moment it's coded ine darkbasic pro, but if i have time i will put this function on the forum.

EDIT : Here is the smoothing function:

Header

Code: Select all

    // smooth the terrain
    // the treshold is the avoid a flat terrain if the 'pass' param is too big
    // the pass parameter control how many the algorithm smooth the terrain
    // the first function smooth the whole terrain, whereas the second smooth only
    // the specified part of terrain
    virtual void smoothVertex(f32 treshold, s32 pass = 1);
    virtual void smoothVertex(s32 w, s32 h, s32 ww, s32 hh, f32 treshold, s32 pass = 1);
CPP

Code: Select all

// smooth the terrain
void ShTlTerrainSceneNode::smoothVertex(f32 treshold, s32 pass)
{
    for(int currentPass = 1; currentPass <= pass; currentPass++)
    {
        for(int y = 0; y <= Size.Height; y++){
        for(int x = 0; x <= Size.Width; x++){
            if(x > 0 && x < Size.Width){
                if(fabs(getHeight(x, y) - (getHeight(x-1, y) + getHeight(x+1, y) + 2*getHeight(x, y))/4.0) >= treshold)
                    setHeight(x, y, (getHeight(x-1, y) + getHeight(x+1, y) + 2*getHeight(x, y))/4.0);
            }
            if(y > 0 && y < Size.Width){
                if(fabs(getHeight(x, y) - (getHeight(x, y-1) + getHeight(x, y+1) + 2*getHeight(x, y))/4.0) >= treshold)
                    setHeight(x, y, (getHeight(x, y-1) + getHeight(x, y+1) + 2*getHeight(x, y))/4.0);
            }
        } // x
        } // y
    } // pass
}



// smooth a part of the terrain.
void ShTlTerrainSceneNode::smoothVertex(s32 w, s32 h, s32 ww, s32 hh, f32 treshold, s32 pass)
{
    if(w < 0){ w = 0; }
    if(h < 0){ h = 0; }
    if(w > Size.Width){ w = Size.Width; }
    if(h > Size.Height){ h = Size.Height; }
    if(ww < 0){ ww = 0;}
    if(hh < 0){ hh = 0;}
    if(ww > Size.Width){ ww = Size.Width; }
    if(hh > Size.Height){ hh = Size.Height; }

    if(w > ww)  // switch values
    {
        s32 temp;
        temp = w;
        w = ww;
        ww = temp;
    }
    if(h > hh)  // switch values
    {
        s32 temp;
        temp = h;
        h = hh;
        hh = temp;
    }

    for(int currentPass = 1; currentPass <= pass; currentPass++)
    {
        for(int y = h; y <= hh; y++){
        for(int x = w; x <= ww; x++){
            if(x > 0 && x < Size.Width){
                if(fabs(getHeight(x, y) - (getHeight(x-1, y) + getHeight(x+1, y) + 2*getHeight(x, y))/4.0) >= treshold)
                    setHeight(x, y, (getHeight(x-1, y) + getHeight(x+1, y) + 2*getHeight(x, y))/4.0);
            }
            if(y > 0 && y < Size.Width){
                if(fabs(getHeight(x, y) - (getHeight(x, y-1) + getHeight(x, y+1) + 2*getHeight(x, y))/4.0) >= treshold)
                    setHeight(x, y, (getHeight(x, y-1) + getHeight(x, y+1) + 2*getHeight(x, y))/4.0);
            }
        } // x
        } // y
    } // pass
}
The treshold is the minimal distance between 2 height of the vertex : the 'normal' height and the new smoothed height. If > treshold, the vertex will be updated.
The pass parameter is the number of times the algorithm smooth the terrain.
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

Corrected sources now available. array2d memory leak corrected, solved problems with setting material attributes and texture.

Darktib >> why do you think normal smoothing code does not work? If you turn on normals in debug mode in demo they seems to be correct.
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

It's pretty easy to modify this to work with VBOs, just use MeshBuffers instead of vertex and index arrays, and only update the sectors when you need to.
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

Hi BlindSide, I think Irrlicht 1.4 does not support VBOs yet ...or?
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

Most people I know are using the SVN, which supports VBOs in OpenGL. 1.5 will also support them in OpenGL and DX.

Changing the structure to meshbuffers and appyling the EHM_DYNAMIC hardware mapping hint is good future proofing for this scene node.

I already did this myself so I can just post up the changes if you like, (Along with an endless tiling-water scene node that incorporates with it.).

Cheers
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
Post Reply