]> git.lizzy.rs Git - dragonfireclient.git/blob - src/noise.cpp
still a missing file
[dragonfireclient.git] / src / noise.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include <math.h>
21 #include "noise.h"
22 #include <iostream>
23
24 #define NOISE_MAGIC_X 1619
25 #define NOISE_MAGIC_Y 31337
26 #define NOISE_MAGIC_Z 52591
27 #define NOISE_MAGIC_SEED 1013
28
29 double cos_lookup[16] = {
30         1.0,0.9238,0.7071,0.3826,0,-0.3826,-0.7071,-0.9238,
31         1.0,-0.9238,-0.7071,-0.3826,0,0.3826,0.7071,0.9238
32 };
33
34 double dotProduct(double vx, double vy, double wx, double wy){
35     return vx*wx+vy*wy;
36 }
37  
38 double easeCurve(double t){
39     return 6*pow(t,5)-15*pow(t,4)+10*pow(t,3);
40 }
41  
42 double linearInterpolation(double x0, double x1, double t){
43     return x0+(x1-x0)*t;
44 }
45  
46 double biLinearInterpolation(double x0y0, double x1y0, double x0y1, double x1y1, double x, double y){
47     double tx = easeCurve(x);
48     double ty = easeCurve(y);
49         /*double tx = x;
50         double ty = y;*/
51     double u = linearInterpolation(x0y0,x1y0,tx);
52     double v = linearInterpolation(x0y1,x1y1,tx);
53     return linearInterpolation(u,v,ty);
54 }
55
56 double triLinearInterpolation(
57                 double v000, double v100, double v010, double v110,
58                 double v001, double v101, double v011, double v111,
59                 double x, double y, double z)
60 {
61     /*double tx = easeCurve(x);
62     double ty = easeCurve(y);
63     double tz = easeCurve(z);*/
64     double tx = x;
65     double ty = y;
66     double tz = z;
67         return(
68                 v000*(1-tx)*(1-ty)*(1-tz) +
69                 v100*tx*(1-ty)*(1-tz) +
70                 v010*(1-tx)*ty*(1-tz) +
71                 v110*tx*ty*(1-tz) +
72                 v001*(1-tx)*(1-ty)*tz +
73                 v101*tx*(1-ty)*tz +
74                 v011*(1-tx)*ty*tz +
75                 v111*tx*ty*tz
76         );
77 }
78
79 double noise2d(int x, int y, int seed)
80 {
81         int n = (NOISE_MAGIC_X * x + NOISE_MAGIC_Y * y
82                         + NOISE_MAGIC_SEED * seed) & 0x7fffffff;
83         n = (n>>13)^n;
84         n = (n * (n*n*60493+19990303) + 1376312589) & 0x7fffffff;
85         return 1.0 - (double)n/1073741824;
86 }
87
88 double noise3d(int x, int y, int z, int seed)
89 {
90         int n = (NOISE_MAGIC_X * x + NOISE_MAGIC_Y * y + NOISE_MAGIC_Z * z
91                         + NOISE_MAGIC_SEED * seed) & 0x7fffffff;
92         n = (n>>13)^n;
93         n = (n * (n*n*60493+19990303) + 1376312589) & 0x7fffffff;
94         return 1.0 - (double)n/1073741824;
95 }
96
97 #if 0
98 double noise2d_gradient(double x, double y, int seed)
99 {
100         // Calculate the integer coordinates
101         int x0 = (x > 0.0 ? (int)x : (int)x - 1);
102         int y0 = (y > 0.0 ? (int)y : (int)y - 1);
103         // Calculate the remaining part of the coordinates
104         double xl = x - (double)x0;
105         double yl = y - (double)y0;
106         // Calculate random cosine lookup table indices for the integer corners.
107         // They are looked up as unit vector gradients from the lookup table.
108         int n00 = (int)((noise2d(x0, y0, seed)+1)*8);
109         int n10 = (int)((noise2d(x0+1, y0, seed)+1)*8);
110         int n01 = (int)((noise2d(x0, y0+1, seed)+1)*8);
111         int n11 = (int)((noise2d(x0+1, y0+1, seed)+1)*8);
112         // Make a dot product for the gradients and the positions, to get the values
113         double s = dotProduct(cos_lookup[n00], cos_lookup[(n00+12)%16], xl, yl);
114         double u = dotProduct(-cos_lookup[n10], cos_lookup[(n10+12)%16], 1.-xl, yl);
115         double v = dotProduct(cos_lookup[n01], -cos_lookup[(n01+12)%16], xl, 1.-yl);
116         double w = dotProduct(-cos_lookup[n11], -cos_lookup[(n11+12)%16], 1.-xl, 1.-yl);
117         // Interpolate between the values
118         return biLinearInterpolation(s,u,v,w,xl,yl);
119 }
120 #endif
121
122 #if 1
123 double noise2d_gradient(double x, double y, int seed)
124 {
125         // Calculate the integer coordinates
126         int x0 = (x > 0.0 ? (int)x : (int)x - 1);
127         int y0 = (y > 0.0 ? (int)y : (int)y - 1);
128         // Calculate the remaining part of the coordinates
129         double xl = x - (double)x0;
130         double yl = y - (double)y0;
131         // Get values for corners of cube
132         double v00 = noise2d(x0, y0, seed);
133         double v10 = noise2d(x0+1, y0, seed);
134         double v01 = noise2d(x0, y0+1, seed);
135         double v11 = noise2d(x0+1, y0+1, seed);
136         // Interpolate
137         return biLinearInterpolation(v00,v10,v01,v11,xl,yl);
138 }
139 #endif
140
141 double noise3d_gradient(double x, double y, double z, int seed)
142 {
143         // Calculate the integer coordinates
144         int x0 = (x > 0.0 ? (int)x : (int)x - 1);
145         int y0 = (y > 0.0 ? (int)y : (int)y - 1);
146         int z0 = (z > 0.0 ? (int)z : (int)z - 1);
147         // Calculate the remaining part of the coordinates
148         double xl = x - (double)x0;
149         double yl = y - (double)y0;
150         double zl = z - (double)z0;
151         // Get values for corners of cube
152         double v000 = noise3d(x0, y0, z0, seed);
153         double v100 = noise3d(x0+1, y0, z0, seed);
154         double v010 = noise3d(x0, y0+1, z0, seed);
155         double v110 = noise3d(x0+1, y0+1, z0, seed);
156         double v001 = noise3d(x0, y0, z0+1, seed);
157         double v101 = noise3d(x0+1, y0, z0+1, seed);
158         double v011 = noise3d(x0, y0+1, z0+1, seed);
159         double v111 = noise3d(x0+1, y0+1, z0+1, seed);
160         // Interpolate
161         return triLinearInterpolation(v000,v100,v010,v110,v001,v101,v011,v111,xl,yl,zl);
162 }
163
164 double noise2d_perlin(double x, double y, int seed,
165                 int octaves, double persistence)
166 {
167         double a = 0;
168         double f = 1.0;
169         double g = 1.0;
170         for(int i=0; i<octaves; i++)
171         {
172                 a += g * noise2d_gradient(x*f, y*f, seed+i);
173                 f *= 2.0;
174                 g *= persistence;
175         }
176         return a;
177 }
178
179 double noise2d_perlin_abs(double x, double y, int seed,
180                 int octaves, double persistence)
181 {
182         double a = 0;
183         double f = 1.0;
184         double g = 1.0;
185         for(int i=0; i<octaves; i++)
186         {
187                 a += g * fabs(noise2d_gradient(x*f, y*f, seed+i));
188                 f *= 2.0;
189                 g *= persistence;
190         }
191         return a;
192 }
193
194 double noise3d_perlin(double x, double y, double z, int seed,
195                 int octaves, double persistence)
196 {
197         double a = 0;
198         double f = 1.0;
199         double g = 1.0;
200         for(int i=0; i<octaves; i++)
201         {
202                 a += g * noise3d_gradient(x*f, y*f, z*f, seed+i);
203                 f *= 2.0;
204                 g *= persistence;
205         }
206         return a;
207 }
208
209 double noise3d_perlin_abs(double x, double y, double z, int seed,
210                 int octaves, double persistence)
211 {
212         double a = 0;
213         double f = 1.0;
214         double g = 1.0;
215         for(int i=0; i<octaves; i++)
216         {
217                 a += g * fabs(noise3d_gradient(x*f, y*f, z*f, seed+i));
218                 f *= 2.0;
219                 g *= persistence;
220         }
221         return a;
222 }
223