• 15
• 15
• 11
• 9
• 10

# Fractal mountain generation

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

## Recommended Posts

Hello i want to do a random fractal mountain generation. I read about random midpoint displacement algorithm but no i dead how to apply it in my program =I cant draw line cause every time I'm taking mid point of the line so the only way to do it by pixels. But i tried to do it and go sh**. I have no idea how to use this algorithm. I looked over all google and cant find any tutorial in programing language of view not mathematical way, also cant find source or even example.. Thanks a lot.

##### Share on other sites
There are two logical steps: first you get an height map h(x,y) by midpoint displacement or any other algorithm, then you render a 3D surface interpolating these height samples.
Three easy ways using lines are:
1) At every grid point, draw a vertical cylinder or a thick line from (x,y,0) to z,y,h(x,y). Can have gaps.
2) Draw lines connecting all samples in each column and in each row of the heightmap.
3) For every rectangle with four samples at its corners, make two triangles with its sides and either diagonal. The resulting triangle mesh can be rendered with suitable colours or textures, possibly with the smoothing trick of using identical (averaged) normal directions for each vertex in every triangle.

##### Share on other sites
Ok i think I got your idea.
But when i said fractal mountain i mentioned this:

##### Share on other sites
So you're looking for a midpoint displacement description or something similar?

For having your terrain look more realistic you could mix some generation methods like the guys from Oddlabs did: http://oddlabs.com/blog/?p=94. They combined a midpoint displacement map with a voronoi map then perturbed it and finally applied an erosion filter. This produces great looking terrain.

If you have generated a heightmap you can render it using the methods LorenzoGatti mentioned.

As for the midpoint displacement method:

Rather than using a recursive approach I'd suggest an iterative approach since this will save you some headaches (e.g. when referenced points/samples are not initialized yet).

The algorithm would look like this:
[soucre]
for each level up to max_level
get the size of the squares in that level (usually size_of_prev_level / 2)
for each row in level
for square in row
do diamond step (calculate the height of the midpoint = sum(heights of corners) / 4 + random offset)
for each row in level
for square in row
do square step (calculate heights of the edge midpoints = sum(heights of edge endpoints and adjecent squares' midpoints)/num_points_considered + random offset)
[/source]
The reason for doing two separate loops is that you need the adjacent squares' midpoints in order to calculate the edge midpoints. You could implement an algorithm that needs only one loop but using two loops is much simpler.

Btw, there's plenty of material on midpoint displacement on the web.

##### Share on other sites
I think you will fing perlin noise, fBm, and erosion algorithms interesting. Try google ;)

##### Share on other sites
If i understand the OP correct this is about 1D-Midpoint-Displacement and not about generating a heightmap.

I recommend first creating an array with your fractal and then drawing the lines.
Here's some pseudocode:
Start with a single horizontal line segment.Repeat for a sufficiently large number of times { Repeat over each line segment in the scene {  Find the midpoint of the line segment.  Displace the midpoint in Y by a random amount.  Reduce the range for random numbers. }}

regards,
m4gnus