3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
24 #define NOISE_MAGIC_X 1619
25 #define NOISE_MAGIC_Y 31337
26 #define NOISE_MAGIC_SEED 1013
28 double cos_lookup[16] = {
29 1.0,0.9238,0.7071,0.3826,0,-0.3826,-0.7071,-0.9238,
30 1.0,-0.9238,-0.7071,-0.3826,0,0.3826,0.7071,0.9238
33 double dotProduct(double vx, double vy, double wx, double wy){
37 double easeCurve(double t){
38 return 6*pow(t,5)-15*pow(t,4)+10*pow(t,3);
41 double linearInterpolation(double x0, double x1, double t){
45 double biLinearInterpolation(double x0y0, double x1y0, double x0y1, double x1y1, double x, double y){
46 double tx = easeCurve(x);
47 double ty = easeCurve(y);
48 double u = linearInterpolation(x0y0,x1y0,tx);
49 double v = linearInterpolation(x0y1,x1y1,tx);
50 return linearInterpolation(u,v,ty);
53 double noise2d(int x, int y, int seed)
55 int n = (NOISE_MAGIC_X * x + NOISE_MAGIC_Y * y
56 + NOISE_MAGIC_SEED * seed) & 0x7fffffff;
58 n = (n * (n*n*60493+19990303) + 1376312589) & 0x7fffffff;
59 return 1.0 - (double)n/1073741824;
62 double noise2d_gradient(double x, double y, int seed)
64 int x0 = (x > 0.0 ? (int)x : (int)x - 1);
65 int y0 = (y > 0.0 ? (int)y : (int)y - 1);
66 double xl = x - (double)x0;
67 double yl = y - (double)y0;
68 int n00 = (int)((noise2d(x0, y0, seed)+1)*8);
69 int n10 = (int)((noise2d(x0+1, y0, seed)+1)*8);
70 int n01 = (int)((noise2d(x0, y0+1, seed)+1)*8);
71 int n11 = (int)((noise2d(x0+1, y0+1, seed)+1)*8);
72 /* In this format, these fail to work on MSVC8 if n00 < 4
73 double s = dotProduct(cos_lookup[n00], cos_lookup[(n00-4)%16], xl, yl);
74 double u = dotProduct(-cos_lookup[n10], cos_lookup[(n10-4)%16], 1.-xl, yl);
75 double v = dotProduct(cos_lookup[n01], -cos_lookup[(n01-4)%16], xl, 1.-yl);
76 double w = dotProduct(-cos_lookup[n11], -cos_lookup[(n11-4)%16], 1.-xl, 1.-yl);*/
77 double s = dotProduct(cos_lookup[n00], cos_lookup[(n00+12)%16], xl, yl);
78 double u = dotProduct(-cos_lookup[n10], cos_lookup[(n10+12)%16], 1.-xl, yl);
79 double v = dotProduct(cos_lookup[n01], -cos_lookup[(n01+12)%16], xl, 1.-yl);
80 double w = dotProduct(-cos_lookup[n11], -cos_lookup[(n11+12)%16], 1.-xl, 1.-yl);
81 /*std::cout<<"x="<<x<<" y="<<y<<" x0="<<x0<<" y0="<<y0<<" xl="<<xl<<" yl="<<yl<<" n00="<<n00<<" n10="<<n01<<" s="<<s<<std::endl;
82 std::cout<<"cos_lookup[n00]="<<(cos_lookup[n00])<<" cos_lookup[(n00-4)%16]="<<(cos_lookup[(n00-4)%16])<<std::endl;*/
83 return biLinearInterpolation(s,u,v,w,xl,yl);
86 double noise2d_perlin(double x, double y, int seed,
87 int octaves, double persistence)
92 for(int i=0; i<octaves; i++)
94 a += g * noise2d_gradient(x*f, y*f, seed+i);