Вы находитесь на странице: 1из 3

RMD

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.
}
}
/**
* If a width is provided that does not conform to the pattern 2n + 1,
* then the next highest value conforming to the pattern is chosen,
* and the result is then clipped to the desired size.
*
* @param
width
* @param
smoothness
* @param
seed
* @return
*/
public static function generate1DHeightmap(width:int, smoothness:Number = 1, seed:Number = Number.NaN):Vector.
{
if (isNaN(seed)) {
seed = Math.random() * 0xFFFFFF;
}
smoothness = Math.max(Math.min(1, smoothness), 0);
var i:int;
var heightmap:Vector. = new Vector.();
var size:int = isPowerOfTwo(width - 1) ? width : adjustUp(width);
// init
for (i = 0; i < size; i++) {
heightmap.push(0);
}
// setup
Rndm.seed = seed;
heightmap[0] = Rndm.random();
heightmap[size-1] = Rndm.random();
var toProcess:Vector. = new Vector.();
toProcess.push(0, size-1, smoothness);
// process
var min:Number = Math.min(heightmap[0], heightmap[size-1]);
var max:Number = Math.max(heightmap[0], heightmap[size-1]);
while (toProcess.length != 0) {
var low:int = toProcess.shift();
var high:int = toProcess.shift();
var offset:Number = toProcess.shift();
var midPoint:int = (low + high) / 2;
heightmap[midPoint] = ((heightmap[low] + heightmap[high]) / 2) + Rndm.float( -offset, offset);
min = Math.min(heightmap[midPoint], min);
max = Math.max(heightmap[midPoint], max);
// terminating condition
if (high - midPoint != 1) {
toProcess.push(low, midPoint, offset / Math.pow(2, smoothness));
toProcess.push(midPoint, high, offset / Math.pow(2, smoothness));
}
}
// clip length
heightmap = heightmap.slice(0, width);
// normalize
var range:Number = max - min;
for (i = 0; i < heightmap.length; i++) {
heightmap[i] = (heightmap[i] - min) / range;
}
return heightmap;
}

/**
* Determines if a value is a power of 2
*
* @param
val
* @return
*/
private static function isPowerOfTwo(val:uint):Boolean {
return (val != 0) && ((val & (val - 1)) == 0);
}
/**
* Calculates returns a number that will contain val
* which is of the pattern 2n +1
*
* @param
val
* @return
*/
private static function adjustUp(val:int):int {
var size:int = 2;
while (size + 1 < val) {
size *= 2;
}
return size + 1;
}

2.1 Random midpoint displacement method


Random midpoint displacement method introduced by Fouriner et al. [4] represents de facto
standard in fractal terrains generation techniques. The principle is as follows.
An initial square is subdivided into four smaller squares (see Figure 2). Let us have four points
. In the first
step we add one vertex into the middle. The vertex is denoted by
where

The added vertex is shifted in z-coordinate direction by random value denoted by . This
procedure is recursively repeated for each subsquare, then for every their descendants, and so on.

Figure 2: First four steps in random midpoint displacement method

In order to be resulting surface fBm, the random number must be generated with Gaussian
distribution
according to

and in the i-th iteration step the variation

have to be modified

where H denotes Hurst exponent [6] (


). From equation (1) we can see, that the first
iteration has the biggest influence to the resulting shape of the surface and influence of the others
decreases.
In the second step we calculate the points on the edges of initial square. We virtually rotate
square by
and calculate the values as in the previous step. The problem is in the cases when
the new point has just three neighbors (the encircled points in Figure 2). In this case we calculate
the average of three neighbors only. The error produced on the border could be neglected.
In the next step we virtually rotate the square back by
and we recursively apply the first two
steps on the four new squares as is mentioned above. This recursive process ends after given
number of iteration.
Fractal dimension D of surface is obtained by

An example of fractal terrain obtained with random midpoint displacement algorithm is in


Figure 3. The fractal dimension of this surface is D=2.5.

Figure: Example of fractal terrain with fractal dimension D=2.5. (a) wire frame model (b) the
same model textured

Вам также может понравиться