1 | /*****************************************************************
|
---|
2 |
|
---|
3 | Implementation of the fractional Brownian motion algorithm. These
|
---|
4 | functions were originally the work of F. Kenton Musgrave.
|
---|
5 | For documentation of the different functions please refer to the
|
---|
6 | book:
|
---|
7 | "Texturing and modeling: a procedural approach"
|
---|
8 | by David S. Ebert et. al.
|
---|
9 |
|
---|
10 | ******************************************************************/
|
---|
11 |
|
---|
12 | #if defined (_MSC_VER)
|
---|
13 | #include <qglobal.h>
|
---|
14 | #endif
|
---|
15 |
|
---|
16 | #include <time.h>
|
---|
17 | #include <stdlib.h>
|
---|
18 | #include "fbm.h"
|
---|
19 |
|
---|
20 | #if defined(Q_CC_MSVC)
|
---|
21 | #pragma warning(disable:4244)
|
---|
22 | #endif
|
---|
23 |
|
---|
24 | /* Definitions used by the noise2() functions */
|
---|
25 |
|
---|
26 | //#define B 0x100
|
---|
27 | //#define BM 0xff
|
---|
28 | #define B 0x20
|
---|
29 | #define BM 0x1f
|
---|
30 |
|
---|
31 | #define N 0x1000
|
---|
32 | #define NP 12 /* 2^N */
|
---|
33 | #define NM 0xfff
|
---|
34 |
|
---|
35 | static int p[B + B + 2];
|
---|
36 | static float g3[B + B + 2][3];
|
---|
37 | static float g2[B + B + 2][2];
|
---|
38 | static float g1[B + B + 2];
|
---|
39 | static int start = 1;
|
---|
40 |
|
---|
41 | static void init(void);
|
---|
42 |
|
---|
43 | #define s_curve(t) ( t * t * (3. - 2. * t) )
|
---|
44 |
|
---|
45 | #define lerp(t, a, b) ( a + t * (b - a) )
|
---|
46 |
|
---|
47 | #define setup(i,b0,b1,r0,r1)\
|
---|
48 | t = vec[i] + N;\
|
---|
49 | b0 = ((int)t) & BM;\
|
---|
50 | b1 = (b0+1) & BM;\
|
---|
51 | r0 = t - (int)t;\
|
---|
52 | r1 = r0 - 1.;
|
---|
53 | #define at3(rx,ry,rz) ( rx * q[0] + ry * q[1] + rz * q[2] )
|
---|
54 |
|
---|
55 | /* Fractional Brownian Motion function */
|
---|
56 |
|
---|
57 | double fBm( Vector point, double H, double lacunarity, double octaves,
|
---|
58 | int init )
|
---|
59 | {
|
---|
60 |
|
---|
61 | double value, frequency, remainder;
|
---|
62 | int i;
|
---|
63 | static double exponent_array[10];
|
---|
64 | float vec[3];
|
---|
65 |
|
---|
66 | /* precompute and store spectral weights */
|
---|
67 | if ( init ) {
|
---|
68 | start = 1;
|
---|
69 | srand( time(0) );
|
---|
70 | /* seize required memory for exponent_array */
|
---|
71 | frequency = 1.0;
|
---|
72 | for (i=0; i<=octaves; i++) {
|
---|
73 | /* compute weight for each frequency */
|
---|
74 | exponent_array[i] = pow( frequency, -H );
|
---|
75 | frequency *= lacunarity;
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | value = 0.0; /* initialize vars to proper values */
|
---|
80 | frequency = 1.0;
|
---|
81 | vec[0]=point.x;
|
---|
82 | vec[1]=point.y;
|
---|
|
---|