# terrain smoothing

This topic is 3768 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I created a terrain using a heightmap. I want to smooth it but I don't know how. What is the best way to do it?

##### Share on other sites
I'm not sure what you mean, but I guess you could try bicubic filtering.

##### Share on other sites
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 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 :)

1. 1
Rutin
24
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 38
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631706
• Total Posts
3001834
×