Algorithms: Fault formation
Introduction
This is the first algorithm I’ve implemented with the shaders (mostly the fragment shader), it’s quite simple and it gives good results.
The Algorithm
This algorithm is based on two phases, a generation phase and an erosion phase.
The first phase is based on the following steps:
- compute a random line that divides the map in two sides
- add an height value to the elements of one side and subtract this value to the elements of the other side
repeat this for some iterations (I get a good result with 500~1000 iterations).
After one iteration you get something like this:
The height value that is added or subtracted to the map has to decrease after every iteration, a way to do this is the following:
GLfloat max_inc = 0.1;
GLfloat min_inc = 0.0;
GLfloat diff_inc = max_inc - min_inc;
…
for(int i = 0; i < num_it; i++)
{
…
inc = max_inc - (diff_inc * i / num_it);
…
}
When the first phase is completed you get something like this:
This image is the result of 500 iterations.
As you can see from the previous image, the generated terrain is not so realistic, in fact it’s easy to notice the lines that splits the map and the different zones of color.
To fix this problem it’s needed another phase, that represents the erosion, where the image is blurred.
My blur algorithm is based on a simple average of values along the X and Y axis of a fragment and it’s repeated for six iterations (this number gives me best results, but it can be changed as I want).
These are the results of the erosion phase:
The left image is the terrain generated after 500 iterations, and the right image is the result of 6 iterations of blur.
The left image is the terrain generated after 1000 iterations, and the right image is the result of 6 iterations of blur.
The implementation
At the moment the program uses one vertex and two fragment shaders for the two phases.
The code is not optimized and needs a bit of polishing and rearrangement so I think there’s a good space for improvements, for these reasons I’m not posting any data about execution times yet, they would not be totally true, but I can say that the current tests I’ve made are really promising.




