A third try on terrain generation. No trees yet.
[oweals/minetest.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 u = linearInterpolation(x0y0,x1y0,tx);
50     double v = linearInterpolation(x0y1,x1y1,tx);
51     return linearInterpolation(u,v,ty);
52 }
53
54 double triLinearInterpolation(
55                 double v000, double v100, double v010, double v110,
56                 double v001, double v101, double v011, double v111,
57                 double x, double y, double z)
58 {
59     /*double tx = easeCurve(x);
60     double ty = easeCurve(y);
61     double tz = easeCurve(z);*/
62     double tx = x;
63     double ty = y;
64     double tz = z;
65         return(
66                 v000*(1-tx)*(1-ty)*(1-tz) +
67                 v100*tx*(1-ty)*(1-tz) +
68                 v010*(1-tx)*ty*(1-tz) +
69                 v110*tx*ty*(1-tz) +
70                 v001*(1-tx)*(1-ty)*tz +
71                 v101*tx*(1-ty)*tz +
72                 v011*(1-tx)*ty*tz +
73                 v111*tx*ty*tz
74         );
75 }
76
77 double noise2d(int x, int y, int seed)
78 {
79         int n = (NOISE_MAGIC_X * x + NOISE_MAGIC_Y * y
80                         + NOISE_MAGIC_SEED * seed) & 0x7fffffff;
81         n = (n>>13)^n;
82         n = (n * (n*n*60493+19990303) + 1376312589) & 0x7fffffff;
83         return 1.0 - (double)n/1073741824;
84 }
85
86 double noise3d(int x, int y, int z, int seed)
87 {
88         int n = (NOISE_MAGIC_X * x + NOISE_MAGIC_Y * y + NOISE_MAGIC_Z * z
89                         + NOISE_MAGIC_SEED * seed) & 0x7fffffff;
90         n = (n>>13)^n;
91         n = (n * (n*n*60493+19990303) + 1376312589) & 0x7fffffff;
92         return 1.0 - (double)n/1073741824;
93 }
94
95 #if 1
96 double noise2d_gradient(double x, double y, int seed)
97 {
98         // Calculate the integer coordinates
99         int x0 = (x > 0.0 ? (int)x : (int)x - 1);
100         int y0 = (y > 0.0 ? (int)y : (int)y - 1);
101         // Calculate the remaining part of the coordinates
102         double xl = x - (double)x0;
103         double yl = y - (double)y0;
104         // Calculate random cosine lookup table indices for the integer corners.
105         // They are looked up as unit vector gradients from the lookup table.
106         int n00 = (int)((noise2d(x0, y0, seed)+1)*8);
107         int n10 = (int)((noise2d(x0+1, y0, seed)+1)*8);
108         int n01 = (int)((noise2d(x0, y0+1, seed)+1)*8);
109         int n11 = (int)((noise2d(x0+1, y0+1, seed)+1)*8);
110         // Make a dot product for the gradients and the positions, to get the values
111         double s = dotProduct(cos_lookup[n00], cos_lookup[(n00+12)%16], xl, yl);
112         double u = dotProduct(-cos_lookup[n10], cos_lookup[(n10+12)%16], 1.-xl, yl);
113         double v = dotProduct(cos_lookup[n01], -cos_lookup[(n01+12)%16], xl, 1.-yl);
114         double w = dotProduct(-cos_lookup[n11], -cos_lookup[(n11+12)%16], 1.-xl, 1.-yl);
115         // Interpolate between the values
116         return biLinearInterpolation(s,u,v,w,xl,yl);
117 }
118 #endif
119
120 #if 0
121 double noise2d_gradient(double x, double y, int seed)
122 {
123         // Calculate the integer coordinates
124         int x0 = (x > 0.0 ? (int)x : (int)x - 1);
125         int y0 = (y > 0.0 ? (int)y : (int)y - 1);
126         // Calculate the remaining part of the coordinates
127         double xl = x - (double)x0;
128         double yl = y - (double)y0;
129         // Get values for corners of cube
130         double v00 = noise2d(x0, y0, seed);
131         double v10 = noise2d(x0+1, y0, seed);
132         double v01 = noise2d(x0, y0+1, seed);
133         double v11 = noise2d(x0+1, y0+1, seed);
134         // Interpolate
135         return biLinearInterpolation(v00,v10,v01,v11,xl,yl);
136 }
137 #endif
138
139 double noise3d_gradient(double x, double y, double z, int seed)
140 {
141         // Calculate the integer coordinates
142         int x0 = (x > 0.0 ? (int)x : (int)x - 1);
143         int y0 = (y > 0.0 ? (int)y : (int)y - 1);
144         int z0 = (z > 0.0 ? (int)z : (int)z - 1);
145         // Calculate the remaining part of the coordinates
146         double xl = x - (double)x0;
147         double yl = y - (double)y0;
148         double zl = z - (double)z0;
149         // Get values for corners of cube
150         double v000 = noise3d(x0, y0, z0, seed);
151         double v100 = noise3d(x0+1, y0, z0, seed);
152         double v010 = noise3d(x0, y0+1, z0, seed);
153         double v110 = noise3d(x0+1, y0+1, z0, seed);
154         double v001 = noise3d(x0, y0, z0+1, seed);
155         double v101 = noise3d(x0+1, y0, z0+1, seed);
156         double v011 = noise3d(x0, y0+1, z0+1, seed);
157         double v111 = noise3d(x0+1, y0+1, z0+1, seed);
158         // Interpolate
159         return triLinearInterpolation(v000,v100,v010,v110,v001,v101,v011,v111,xl,yl,zl);
160 }
161
162 double noise2d_perlin(double x, double y, int seed,
163                 int octaves, double persistence)
164 {
165         double a = 0;
166         double f = 1.0;
167         double g = 1.0;
168         for(int i=0; i<octaves; i++)
169         {
170                 a += g * noise2d_gradient(x*f, y*f, seed+i);
171                 f *= 2.0;
172                 g *= persistence;
173         }
174         return a;
175 }
176
177 double noise2d_perlin_abs(double x, double y, int seed,
178                 int octaves, double persistence)
179 {
180         double a = 0;
181         double f = 1.0;
182         double g = 1.0;
183         for(int i=0; i<octaves; i++)
184         {
185                 a += g * fabs(noise2d_gradient(x*f, y*f, seed+i));
186                 f *= 2.0;
187                 g *= persistence;
188         }
189         return a;
190 }
191
192 double noise3d_perlin(double x, double y, double z, int seed,
193                 int octaves, double persistence)
194 {
195         double a = 0;
196         double f = 1.0;
197         double g = 1.0;
198         for(int i=0; i<octaves; i++)
199         {
200                 a += g * noise3d_gradient(x*f, y*f, z*f, seed+i);
201                 f *= 2.0;
202                 g *= persistence;
203         }
204         return a;
205 }
206
207 double noise3d_perlin_abs(double x, double y, double z, int seed,
208                 int octaves, double persistence)
209 {
210         double a = 0;
211         double f = 1.0;
212         double g = 1.0;
213         for(int i=0; i<octaves; i++)
214         {
215                 a += g * fabs(noise3d_gradient(x*f, y*f, z*f, seed+i));
216                 f *= 2.0;
217                 g *= persistence;
218         }
219         return a;
220 }
221