Clean up Noise macros
[oweals/minetest.git] / src / noise.h
1 /*
2  * Minetest
3  * Copyright (C) 2010-2014 celeron55, Perttu Ahola <celeron55@gmail.com>
4  * Copyright (C) 2010-2014 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification, are
8  * permitted provided that the following conditions are met:
9  *  1. Redistributions of source code must retain the above copyright notice, this list of
10  *     conditions and the following disclaimer.
11  *  2. Redistributions in binary form must reproduce the above copyright notice, this list
12  *     of conditions and the following disclaimer in the documentation and/or other materials
13  *     provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED
16  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #ifndef NOISE_HEADER
27 #define NOISE_HEADER
28
29 #include "debug.h"
30 #include "irr_v3d.h"
31 #include "util/string.h"
32
33 extern FlagDesc flagdesc_noiseparams[];
34
35 class PseudoRandom
36 {
37 public:
38         PseudoRandom(): m_next(0)
39         {
40         }
41         PseudoRandom(int seed): m_next(seed)
42         {
43         }
44         void seed(int seed)
45         {
46                 m_next = seed;
47         }
48         // Returns 0...32767
49         int next()
50         {
51                 m_next = m_next * 1103515245 + 12345;
52                 return((unsigned)(m_next/65536) % 32768);
53         }
54         int range(int min, int max)
55         {
56                 if(max-min > 32768/10)
57                 {
58                         //dstream<<"WARNING: PseudoRandom::range: max > 32767"<<std::endl;
59                         assert(0);
60                 }
61                 if(min > max)
62                 {
63                         assert(0);
64                         return max;
65                 }
66                 return (next()%(max-min+1))+min;
67         }
68 private:
69         int m_next;
70 };
71
72 #define NOISE_FLAG_DEFAULTS    0x01
73 #define NOISE_FLAG_EASED       0x02
74 #define NOISE_FLAG_ABSVALUE    0x04
75
76 //// TODO(hmmmm): implement these!
77 #define NOISE_FLAG_POINTBUFFER 0x08
78 #define NOISE_FLAG_SIMPLEX     0x10
79
80 struct NoiseParams {
81         float offset;
82         float scale;
83         v3f spread;
84         s32 seed;
85         u16 octaves;
86         float persist;
87         float lacunarity;
88         u32 flags;
89
90         NoiseParams() {
91                 offset     = 0.0f;
92                 scale      = 1.0f;
93                 spread     = v3f(250, 250, 250);
94                 seed       = 12345;
95                 octaves    = 3;
96                 persist    = 0.6f;
97                 lacunarity = 2.0f;
98                 flags      = NOISE_FLAG_DEFAULTS;
99         }
100
101         NoiseParams(float offset_, float scale_, v3f spread_, s32 seed_,
102                 u16 octaves_, float persist_, float lacunarity_,
103                 u32 flags_=NOISE_FLAG_DEFAULTS)
104         {
105                 offset     = offset_;
106                 scale      = scale_;
107                 spread     = spread_;
108                 seed       = seed_;
109                 octaves    = octaves_;
110                 persist    = persist_;
111                 lacunarity = lacunarity_;
112                 flags      = flags_;
113         }
114 };
115
116
117 // Convenience macros for getting/setting NoiseParams in Settings as a string
118 // WARNING:  Deprecated, use Settings::getNoiseParamsFromValue() instead
119 #define NOISEPARAMS_FMT_STR "f,f,v3,s32,u16,f"
120 //#define getNoiseParams(x, y) getStruct((x), NOISEPARAMS_FMT_STR, &(y), sizeof(y))
121 //#define setNoiseParams(x, y) setStruct((x), NOISEPARAMS_FMT_STR, &(y))
122
123 class Noise {
124 public:
125         NoiseParams np;
126         int seed;
127         int sx;
128         int sy;
129         int sz;
130         float *noise_buf;
131         float *gradient_buf;
132         float *persist_buf;
133         float *result;
134
135         Noise(NoiseParams *np, int seed, int sx, int sy, int sz=1);
136         ~Noise();
137
138         void setSize(int sx, int sy, int sz=1);
139         void setSpreadFactor(v3f spread);
140         void setOctaves(int octaves);
141
142         void gradientMap2D(
143                 float x, float y,
144                 float step_x, float step_y,
145                 int seed);
146         void gradientMap3D(
147                 float x, float y, float z,
148                 float step_x, float step_y, float step_z,
149                 int seed);
150
151         float *perlinMap2D(float x, float y, float *persistence_map=NULL);
152         float *perlinMap3D(float x, float y, float z, float *persistence_map=NULL);
153
154         inline float *perlinMap2D_PO(float x, float xoff, float y, float yoff,
155                 float *persistence_map=NULL)
156         {
157                 return perlinMap2D(
158                         x + xoff * np.spread.X,
159                         y + yoff * np.spread.Y,
160                         persistence_map);
161         }
162
163         inline float *perlinMap3D_PO(float x, float xoff, float y, float yoff,
164                 float z, float zoff, float *persistence_map=NULL)
165         {
166                 return perlinMap3D(
167                         x + xoff * np.spread.X,
168                         y + yoff * np.spread.Y,
169                         z + zoff * np.spread.Z,
170                         persistence_map);
171         }
172
173 private:
174         void allocBuffers();
175         void resizeNoiseBuf(bool is3d);
176         void updateResults(float g, float *gmap, float *persistence_map, size_t bufsize);
177
178 };
179
180 float NoisePerlin2D(NoiseParams *np, float x, float y, int seed);
181 float NoisePerlin3D(NoiseParams *np, float x, float y, float z, int seed);
182
183 inline float NoisePerlin2D_PO(NoiseParams *np, float x, float xoff,
184         float y, float yoff, int seed)
185 {
186         return NoisePerlin2D(np,
187                 x + xoff * np->spread.X,
188                 y + yoff * np->spread.Y,
189                 seed);
190 }
191
192 inline float NoisePerlin3D_PO(NoiseParams *np, float x, float xoff,
193         float y, float yoff, float z, float zoff, int seed)
194 {
195         return NoisePerlin3D(np,
196                 x + xoff * np->spread.X,
197                 y + yoff * np->spread.Y,
198                 z + zoff * np->spread.Z,
199                 seed);
200 }
201
202 // Return value: -1 ... 1
203 float noise2d(int x, int y, int seed);
204 float noise3d(int x, int y, int z, int seed);
205
206 float noise2d_gradient(float x, float y, int seed, bool eased=true);
207 float noise3d_gradient(float x, float y, float z, int seed, bool eased=false);
208
209 float noise2d_perlin(float x, float y, int seed,
210                 int octaves, float persistence, bool eased=true);
211
212 float noise2d_perlin_abs(float x, float y, int seed,
213                 int octaves, float persistence, bool eased=true);
214
215 float noise3d_perlin(float x, float y, float z, int seed,
216                 int octaves, float persistence, bool eased=false);
217
218 float noise3d_perlin_abs(float x, float y, float z, int seed,
219                 int octaves, float persistence, bool eased=false);
220
221 inline float easeCurve(float t)
222 {
223         return t * t * t * (t * (6.f * t - 15.f) + 10.f);
224 }
225
226 float contour(float v);
227
228 #endif
229