vgo
09-09-2007, 10:04 AM
I've been trying to covert some C code to Pascal, but I'm not sure if I got it right, at least the results seem to be completely wrong.
What can you make out of this?
/*
* Heterogeneous procedural terrain function: stats by altitude method.
* Evaluated at point; returns value stored in value.
*
* Parameters:
* H determines the fractal dimension of the roughest areas
* lacunarity is the gap between successive frequencies
* octaves is the number of frequencies in the fBm
* offset raises the terrain from sea level
*/
double
Hetero_Terrain( Vector point,
double H, double lacunarity, double octaves, double offset )
{
double value, increment, frequency, remainder, Noise3();
int i;
static int first = TRUE;
static double *exponent_array;
/* precompute and store spectral weights, for efficiency */
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;
}
/* first unscaled octave of function; later octaves are scaled */
value = offset + Noise3( point ); point.x *= lacunarity;
point.y *= lacunarity; point.z *= lacunarity;
/* spectral construction inner loop, where the fractal is built */
for (1=1; i<octaves; 1++) {
/* obtain displaced noise value */
increment = Noise3( point ) + offset;
/* scale amplitude appropriately for this frequency */
increment *= exponent_array[i];
/* scale increment by current altitude of function */
increment *= value;
/* add increment to value */
value += increment;
/* raise spatial 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 */
/* note that the main loop code is made shorter here */
/* you may want to make that loop more like this */
increment = (Noise3( point ) + offset) * exponent_array[i];
value += remainder * increment * value;
}
return( value );
} /* Hetero_Terrain() */
I've converted the exponent_array table to a variable inside the class, so that's not a problem. The problem is with the variable i and that strange for (1=1; i<octaves; 1++) loop. For all that I know i is UNDEFINED when the function is executed, that should be the case with the C version too, except after that static initialization that is done when the function is called for the first time.
I just don't understand the relation of that inner loop to variable i... So how do I convert this properly?
What can you make out of this?
/*
* Heterogeneous procedural terrain function: stats by altitude method.
* Evaluated at point; returns value stored in value.
*
* Parameters:
* H determines the fractal dimension of the roughest areas
* lacunarity is the gap between successive frequencies
* octaves is the number of frequencies in the fBm
* offset raises the terrain from sea level
*/
double
Hetero_Terrain( Vector point,
double H, double lacunarity, double octaves, double offset )
{
double value, increment, frequency, remainder, Noise3();
int i;
static int first = TRUE;
static double *exponent_array;
/* precompute and store spectral weights, for efficiency */
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;
}
/* first unscaled octave of function; later octaves are scaled */
value = offset + Noise3( point ); point.x *= lacunarity;
point.y *= lacunarity; point.z *= lacunarity;
/* spectral construction inner loop, where the fractal is built */
for (1=1; i<octaves; 1++) {
/* obtain displaced noise value */
increment = Noise3( point ) + offset;
/* scale amplitude appropriately for this frequency */
increment *= exponent_array[i];
/* scale increment by current altitude of function */
increment *= value;
/* add increment to value */
value += increment;
/* raise spatial 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 */
/* note that the main loop code is made shorter here */
/* you may want to make that loop more like this */
increment = (Noise3( point ) + offset) * exponent_array[i];
value += remainder * increment * value;
}
return( value );
} /* Hetero_Terrain() */
I've converted the exponent_array table to a variable inside the class, so that's not a problem. The problem is with the variable i and that strange for (1=1; i<octaves; 1++) loop. For all that I know i is UNDEFINED when the function is executed, that should be the case with the C version too, except after that static initialization that is done when the function is called for the first time.
I just don't understand the relation of that inner loop to variable i... So how do I convert this properly?