Sign in to follow this  
Trulezz

terrain smoothing

Recommended Posts

I'm assuming you setup your map using nodes. If so, try something like this:

void HEIGHTMAP::SmoothTerrain()
{
//create temp heightmap
float *hm=new float[hmap_size.x*hmap_size.y];
memset(hm,0,sizeof(float)*hmap_size.x*hmap_size.y);

for(int y=0;y<hmap_size.y;y++)
for(int x=0;x<hmap_size.x;x++)
{
float totalHeight=0.0f;
int noNodes=0;

for(int y1=y-1;y1<=y+1;y1++)
{
for(int x1=x-1;x<=x+1;x1++)
if(x1>=0 && x1<hmap_size.x && y1>=0 && y1<hmap_size.y)
{
totalHeight+=heightMap[x1+y1*hmap_size.x];
noNodes++;
}
}

hm[x+y*hmap_size.x]=totalHeight/(float)noNodes;
}

//replace heightMap with hm for smoothing
delete [] heightMap;
heightMap=hm;
}

Not sure if I got all those nests correct, but this should be very close or perfect. Its from an rts Im programming, so I know it works, unless I misprinted those nests. It takes all the surrounding nodes for any given node, then sums up the height and divides by the total number of nodes. Looks pretty nice too. Good Luck!

Share this post


Link to post
Share on other sites
Basically for smoothing / filtering my terrain, i go through each value in the heightmap and average it with all values next to it.

So for instance pick the pixel at : (5, 5)
then you can do something like this.

float fValue;

fValue += HeightMapHeight(4, 6),
fValue += HeightMapHeight(5, 6),
fValue += HeightMapHeight(6, 6),
fValue += HeightMapHeight(4, 5),
fValue += HeightMapHeight(6, 5),
fValue += HeightMapHeight(4, 4),
fValue += HeightMapHeight(5, 4),
fValue += HeightMapHeight(6, 4),

then set the height of 5, 5 to fValue / 8

You can put this in a for loop to make it look nicer, was just giving you the general idea, and this works very well :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this