Fix NoisePerlin3DEased return value
[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
32 class PseudoRandom
33 {
34 public:
35         PseudoRandom(): m_next(0)
36         {
37         }
38         PseudoRandom(int seed): m_next(seed)
39         {
40         }
41         void seed(int seed)
42         {
43                 m_next = seed;
44         }
45         // Returns 0...32767
46         int next()
47         {
48                 m_next = m_next * 1103515245 + 12345;
49                 return((unsigned)(m_next/65536) % 32768);
50         }
51         int range(int min, int max)
52         {
53                 if(max-min > 32768/10)
54                 {
55                         //dstream<<"WARNING: PseudoRandom::range: max > 32767"<<std::endl;
56                         assert(0);
57                 }
58                 if(min > max)
59                 {
60                         assert(0);
61                         return max;
62                 }
63                 return (next()%(max-min+1))+min;
64         }
65 private:
66         int m_next;
67 };
68
69 struct NoiseParams {
70         float offset;
71         float scale;
72         v3f spread;
73         int seed;
74         int octaves;
75         float persist;
76         bool eased;
77
78         NoiseParams() {}
79
80         NoiseParams(float offset_, float scale_, v3f spread_,
81                 int seed_, int octaves_, float persist_, bool eased_=false)
82         {
83                 offset  = offset_;
84                 scale   = scale_;
85                 spread  = spread_;
86                 seed    = seed_;
87                 octaves = octaves_;
88                 persist = persist_;
89                 eased   = eased_;
90         }
91 };
92
93
94 // Convenience macros for getting/setting NoiseParams in Settings
95
96 #define NOISEPARAMS_FMT_STR "f,f,v3,s32,s32,f"
97
98 #define getNoiseParams(x, y) getStruct((x), NOISEPARAMS_FMT_STR, &(y), sizeof(y))
99 #define setNoiseParams(x, y) setStruct((x), NOISEPARAMS_FMT_STR, &(y))
100
101 class Noise {
102 public:
103         NoiseParams *np;
104         int seed;
105         int sx;
106         int sy;
107         int sz;
108         float *noisebuf;
109         float *buf;
110         float *result;
111
112         Noise(NoiseParams *np, int seed, int sx, int sy, int sz=1);
113         ~Noise();
114
115         void setSize(int sx, int sy, int sz=1);
116         void setSpreadFactor(v3f spread);
117         void setOctaves(int octaves);
118         void resizeNoiseBuf(bool is3d);
119
120         void gradientMap2D(
121                 float x, float y,
122                 float step_x, float step_y,
123                 int seed);
124         void gradientMap3D(
125                 float x, float y, float z,
126                 float step_x, float step_y, float step_z,
127                 int seed, bool eased=false);
128         float *perlinMap2D(float x, float y);
129         float *perlinMap2DModulated(float x, float y, float *persist_map);
130         float *perlinMap3D(float x, float y, float z, bool eased=false);
131         void transformNoiseMap();
132 };
133
134 // Return value: -1 ... 1
135 float noise2d(int x, int y, int seed);
136 float noise3d(int x, int y, int z, int seed);
137
138 float noise2d_gradient(float x, float y, int seed);
139 float noise3d_gradient(float x, float y, float z, int seed, bool eased=false);
140
141 float noise2d_perlin(float x, float y, int seed,
142                 int octaves, float persistence);
143
144 float noise2d_perlin_abs(float x, float y, int seed,
145                 int octaves, float persistence);
146
147 float noise3d_perlin(float x, float y, float z, int seed,
148                 int octaves, float persistence, bool eased=false);
149
150 float noise3d_perlin_abs(float x, float y, float z, int seed,
151                 int octaves, float persistence, bool eased=false);
152
153 inline float easeCurve(float t) {
154         return t * t * t * (t * (6.f * t - 15.f) + 10.f);
155 }
156
157 float contour(float v);
158
159 #define NoisePerlin2D(np, x, y, s) \
160                 ((np)->offset + (np)->scale * noise2d_perlin( \
161                 (float)(x) / (np)->spread.X, \
162                 (float)(y) / (np)->spread.Y, \
163                 (s) + (np)->seed, (np)->octaves, (np)->persist))
164
165 #define NoisePerlin2DNoTxfm(np, x, y, s) \
166                 (noise2d_perlin( \
167                 (float)(x) / (np)->spread.X, \
168                 (float)(y) / (np)->spread.Y, \
169                 (s) + (np)->seed, (np)->octaves, (np)->persist))
170
171 #define NoisePerlin2DPosOffset(np, x, xoff, y, yoff, s) \
172                 ((np)->offset + (np)->scale * noise2d_perlin( \
173                 (float)(xoff) + (float)(x) / (np)->spread.X, \
174                 (float)(yoff) + (float)(y) / (np)->spread.Y, \
175                 (s) + (np)->seed, (np)->octaves, (np)->persist))
176
177 #define NoisePerlin2DNoTxfmPosOffset(np, x, xoff, y, yoff, s) \
178                 (noise2d_perlin( \
179                 (float)(xoff) + (float)(x) / (np)->spread.X, \
180                 (float)(yoff) + (float)(y) / (np)->spread.Y, \
181                 (s) + (np)->seed, (np)->octaves, (np)->persist))
182
183 #define NoisePerlin3D(np, x, y, z, s) ((np)->offset + (np)->scale * \
184                 noise3d_perlin((float)(x) / (np)->spread.X, (float)(y) / (np)->spread.Y, \
185                 (float)(z) / (np)->spread.Z, (s) + (np)->seed, (np)->octaves, (np)->persist))
186
187 #define NoisePerlin3DEased(np, x, y, z, s) ((np)->offset + (np)->scale * \
188                 noise3d_perlin((float)(x) / (np)->spread.X, (float)(y) / (np)->spread.Y, \
189                 (float)(z) / (np)->spread.Z, (s) + (np)->seed, (np)->octaves, \
190                 (np)->persist, true))
191
192 #endif
193