technologies/reference/musgrave.c
double 
HybridMultifractal( Vector point, double H, double lacunarity, 
                    double octaves, double offset )
{
      double          frequency, result, signal, weight, remainder; 
      double          Noise3();
      int             i;
      static int      first = TRUE;
      static double   *exponent_array;

      /* precompute and store spectral weights */
      if ( first ) {
            /* seize required memory for exponent_array */
            exponent_array = 
                        (double *)malloc( (octaves+1) * sizeof(double) );
            frequency = 1.0;
            for (i=0; i<=octaves; i++) {
                  /* compute weight for each frequency */
                  exponent_array[i] = pow( frequency, -H);
                  frequency *= lacunarity;
            }
            first = FALSE;
      }

      /* get first octave of function */
      result = ( Noise3( point ) + offset ) * exponent_array[0];
      weight = result;
      /* increase frequency */
      point.x *= lacunarity;
      point.y *= lacunarity;
      point.z *= lacunarity;

      /* spectral construction inner loop, where the fractal is built */
      for (i=1; i<octaves; i++) {
            /* prevent divergence */
            if ( weight > 1.0 )  weight = 1.0;

            /* get next higher frequency */
            signal = ( Noise3( point ) + offset ) * exponent_array[i];
            /* add it in, weighted by previous freq's local value */
            result += weight * signal;
            /* update the (monotonically decreasing) weighting value */
            /* (this is why H must specify a high fractal dimension) */
            weight *= signal;

            /* increase frequency */
            point.x *= lacunarity;
            point.y *= lacunarity;
            point.z *= lacunarity;
      } /* for */

      /* take care of remainder in ``octaves''  */
      remainder = octaves - (int)octaves;
      if ( remainder )
            /* ``i''  and spatial freq. are preset in loop above */
            result += remainder * Noise3( point ) * exponent_array[i];

      return( result );

}
/* Hybrid additive/multiplicative multifractal terrain model.
 *
 * Copyright 1994 F. Kenton Musgrave 
 *
 * Some good parameter values to start with:
 *
 *      H:           0.25
 *      offset:      0.7
 */
Hybrid additive/multiplicative multifractal terrain model.
Copyright 1994 F. Kenton Musgrave

Some good parameter values to start with.


Hybrid additive / multiplicative multifractal terrain model . Copyright 1994 F . Kenton Musgrave Some good parameter values to start with .
double 
HybridMultifractal( Vector point, double H, double lacunarity, 
                    double octaves, double offset )
{
      double          frequency, result, signal, weight, remainder; 
      double          Noise3();
      int             i;
      static int      first = TRUE;
      static double   *exponent_array;
      if ( first ) {
            exponent_array = 
                        (double *)malloc( (octaves+1) * sizeof(double) );
            frequency = 1.0;
            for (i=0; i<=octaves; i++) {
                  exponent_array[i] = pow( frequency, -H);
                  frequency *= lacunarity;
            }
            first = FALSE;
      }
      result = ( Noise3( point ) + offset ) * exponent_array[0];
      weight = result;
      point.x *= lacunarity;
      point.y *= lacunarity;
      point.z *= lacunarity;
      for (i=1; i<octaves; i++) {
            if ( weight > 1.0 )  weight = 1.0;
            signal = ( Noise3( point ) + offset ) * exponent_array[i];
            result += weight * signal;
            weight *= signal;
            point.x *= lacunarity;
            point.y *= lacunarity;
            point.z *= lacunarity;
      } 
      remainder = octaves - (int)octaves;
      if ( remainder )
            result += remainder * Noise3( point ) * exponent_array[i];
      return( result );
}
double HybridMultifractal ( Vector point , double H , double lacunarity , double octaves , double offset ) { double frequency , result , signal , weight , remainder ; double Noise3 ( ) ; int i ; static int first = TRUE ; static double * exponent_array ; if ( first ) { exponent_array = ( double * ) malloc ( ( octaves + 1 ) * sizeof ( double ) ) ; frequency = 1.0 ; for ( i = 0 ; i <= octaves ; i ++ ) { exponent_array [ i ] = pow ( frequency , - H ) ; frequency *= lacunarity ; } first = FALSE ; } result = ( Noise3 ( point ) + offset ) * exponent_array [ 0 ] ; weight = result ; point . x *= lacunarity ; point . y *= lacunarity ; point . z *= lacunarity ; for ( i = 1 ; i < octaves ; i ++ ) { if ( weight > 1.0 ) weight = 1.0 ; signal = ( Noise3 ( point ) + offset ) * exponent_array [ i ] ; result += weight * signal ; weight *= signal ; point . x *= lacunarity ; point . y *= lacunarity ; point . z *= lacunarity ; } remainder = octaves - ( int ) octaves ; if ( remainder ) result += remainder * Noise3 ( point ) * exponent_array [ i ] ; return ( result ) ; }
Hybrid additive/multiplicative multifractal terrain model.
Hybrid additive / multiplicative multifractal terrain model .
/* precompute and store spectral weights */ /* seize required memory for exponent_array */ /* compute weight for each frequency */ /* get first octave of function */ /* increase frequency */ /* spectral construction inner loop, where the fractal is built */ /* prevent divergence */ /* get next higher frequency */ /* add it in, weighted by previous freq's local value */ /* update the (monotonically decreasing) weighting value */ /* (this is why H must specify a high fractal dimension) */ /* increase frequency */ /* for */ /* take care of remainder in ``octaves''  */ /* ``i''  and spatial freq. are preset in loop above */