1 /* OpenSceneGraph example, osgshaders.
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 /************************************************************************
21 * Copyright (C) 2002 3Dlabs Inc. Ltd. *
23 ************************************************************************/
28 /* Coherent noise function over 1, 2 or 3 dimensions */
29 /* (copyright Ken Perlin) */
33 #define NP 12 /* 2^N */
36 #define s_curve(t) ( t * t * (3. - 2. * t) )
37 #define lerp(t, a, b) ( a + t * (b - a) )
38 #define setup(i,b0,b1,r0,r1)\
44 #define at2(rx,ry) ( rx * q[0] + ry * q[1] )
45 #define at3(rx,ry,rz) ( rx * q[0] + ry * q[1] + rz * q[2] )
47 static void initNoise(void);
49 static int p[MAXB + MAXB + 2];
50 static double g3[MAXB + MAXB + 2][3];
51 static double g2[MAXB + MAXB + 2][2];
52 static double g1[MAXB + MAXB + 2];
59 void SetNoiseFrequency(int frequency)
66 double noise1(double arg)
69 double rx0, rx1, sx, t, u, v, vec[1];
77 setup(0,bx0,bx1,rx0,rx1);
80 u = rx0 * g1[ p[ bx0 ] ];
81 v = rx1 * g1[ p[ bx1 ] ];
83 return(lerp(sx, u, v));
86 double noise2(double vec[2])
88 int bx0, bx1, by0, by1, b00, b10, b01, b11;
89 double rx0, rx1, ry0, ry1, *q, sx, sy, a, b, t, u, v;
97 setup(0, bx0,bx1, rx0,rx1);
98 setup(1, by0,by1, ry0,ry1);
111 q = g2[ b00 ] ; u = at2(rx0,ry0);
112 q = g2[ b10 ] ; v = at2(rx1,ry0);
115 q = g2[ b01 ] ; u = at2(rx0,ry1);
116 q = g2[ b11 ] ; v = at2(rx1,ry1);
119 return lerp(sy, a, b);
122 double noise3(double vec[3])
124 int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11;
125 double rx0, rx1, ry0, ry1, rz0, rz1, *q, sy, sz, a, b, c, d, t, u, v;
133 setup(0, bx0,bx1, rx0,rx1);
134 setup(1, by0,by1, ry0,ry1);
135 setup(2, bz0,bz1, rz0,rz1);
149 q = g3[ b00 + bz0 ] ; u = at3(rx0,ry0,rz0);
150 q = g3[ b10 + bz0 ] ; v = at3(rx1,ry0,rz0);
153 q = g3[ b01 + bz0 ] ; u = at3(rx0,ry1,rz0);
154 q = g3[ b11 + bz0 ] ; v = at3(rx1,ry1,rz0);
159 q = g3[ b00 + bz1 ] ; u = at3(rx0,ry0,rz1);
160 q = g3[ b10 + bz1 ] ; v = at3(rx1,ry0,rz1);
163 q = g3[ b01 + bz1 ] ; u = at3(rx0,ry1,rz1);
164 q = g3[ b11 + bz1 ] ; v = at3(rx1,ry1,rz1);
169 //fprintf(stderr, "%f\n", lerp(sz, c, d));
171 return lerp(sz, c, d);
174 void normalize2(double v[2])
178 s = sqrt(v[0] * v[0] + v[1] * v[1]);
183 void normalize3(double v[3])
187 s = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
198 for (i = 0 ; i < B ; i++) {
200 g1[i] = (double)((rand() % (B + B)) - B) / B;
202 for (j = 0 ; j < 2 ; j++)
203 g2[i][j] = (double)((rand() % (B + B)) - B) / B;
206 for (j = 0 ; j < 3 ; j++)
207 g3[i][j] = (double)((rand() % (B + B)) - B) / B;
213 p[i] = p[j = rand() % B];
217 for (i = 0 ; i < B + 2 ; i++) {
220 for (j = 0 ; j < 2 ; j++)
221 g2[B + i][j] = g2[i][j];
222 for (j = 0 ; j < 3 ; j++)
223 g3[B + i][j] = g3[i][j];
227 /* --- My harmonic summing functions - PDB --------------------------*/
230 In what follows "alpha" is the weight when the sum is formed.
231 Typically it is 2, As this approaches 1 the function is noisier.
232 "beta" is the harmonic scaling/spacing, typically 2.
235 double PerlinNoise1D(double x,double alpha,double beta,int n)
251 double PerlinNoise2D(double x,double y,double alpha,double beta,int n)
255 double p[2],scale = 1;
269 double PerlinNoise3D(double x,double y,double z,double alpha,double beta,int n)
273 double p[3],scale = 1;