]> git.lizzy.rs Git - minetest.git/blob - src/noise.h
Fix regression (increase/decrease viewing range with +/- keys)
[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         s32 seed;
74         u16 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 as a string
95 // WARNING:  Deprecated, use Settings::getNoiseParamsFromValue() instead
96 #define NOISEPARAMS_FMT_STR "f,f,v3,s32,u16,f"
97 //#define getNoiseParams(x, y) getStruct((x), NOISEPARAMS_FMT_STR, &(y), sizeof(y))
98 //#define setNoiseParams(x, y) setStruct((x), NOISEPARAMS_FMT_STR, &(y))
99
100 class Noise {
101 public:
102         NoiseParams *np;
103         int seed;
104         int sx;
105         int sy;
106         int sz;
107         float *noisebuf;
108         float *buf;
109         float *result;
110
111         Noise(NoiseParams *np, int seed, int sx, int sy, int sz=1);
112         ~Noise();
113
114         void setSize(int sx, int sy, int sz=1);
115         void setSpreadFactor(v3f spread);
116         void setOctaves(int octaves);
117         void resizeNoiseBuf(bool is3d);
118
119         void gradientMap2D(
120                 float x, float y,
121                 float step_x, float step_y,
122                 int seed);
123         void gradientMap3D(
124                 float x, float y, float z,
125                 float step_x, float step_y, float step_z,
126                 int seed, bool eased=false);
127         float *perlinMap2D(float x, float y);
128         float *perlinMap2DModulated(float x, float y, float *persist_map);
129         float *perlinMap3D(float x, float y, float z, bool eased=false);
130         void transformNoiseMap();
131 };
132
133 // Return value: -1 ... 1
134 float noise2d(int x, int y, int seed);
135 float noise3d(int x, int y, int z, int seed);
136
137 float noise2d_gradient(float x, float y, int seed);
138 float noise3d_gradient(float x, float y, float z, int seed, bool eased=false);
139
140 float noise2d_perlin(float x, float y, int seed,
141                 int octaves, float persistence);
142
143 float noise2d_perlin_abs(float x, float y, int seed,
144                 int octaves, float persistence);
145
146 float noise3d_perlin(float x, float y, float z, int seed,
147                 int octaves, float persistence, bool eased=false);
148
149 float noise3d_perlin_abs(float x, float y, float z, int seed,
150                 int octaves, float persistence, bool eased=false);
151
152 inline float easeCurve(float t) {
153         return t * t * t * (t * (6.f * t - 15.f) + 10.f);
154 }
155
156 float contour(float v);
157
158 #define NoisePerlin2D(np, x, y, s) \
159                 ((np)->offset + (np)->scale * noise2d_perlin( \
160                 (float)(x) / (np)->spread.X, \
161                 (float)(y) / (np)->spread.Y, \
162                 (s) + (np)->seed, (np)->octaves, (np)->persist))
163
164 #define NoisePerlin2DNoTxfm(np, x, y, s) \
165                 (noise2d_perlin( \
166                 (float)(x) / (np)->spread.X, \
167                 (float)(y) / (np)->spread.Y, \
168                 (s) + (np)->seed, (np)->octaves, (np)->persist))
169
170 #define NoisePerlin2DPosOffset(np, x, xoff, y, yoff, s) \
171                 ((np)->offset + (np)->scale * noise2d_perlin( \
172                 (float)(xoff) + (float)(x) / (np)->spread.X, \
173                 (float)(yoff) + (float)(y) / (np)->spread.Y, \
174                 (s) + (np)->seed, (np)->octaves, (np)->persist))
175
176 #define NoisePerlin2DNoTxfmPosOffset(np, x, xoff, y, yoff, s) \
177                 (noise2d_perlin( \
178                 (float)(xoff) + (float)(x) / (np)->spread.X, \
179                 (float)(yoff) + (float)(y) / (np)->spread.Y, \
180                 (s) + (np)->seed, (np)->octaves, (np)->persist))
181
182 #define NoisePerlin3D(np, x, y, z, s) ((np)->offset + (np)->scale * \
183                 noise3d_perlin((float)(x) / (np)->spread.X, (float)(y) / (np)->spread.Y, \
184                 (float)(z) / (np)->spread.Z, (s) + (np)->seed, (np)->octaves, (np)->persist))
185
186 #define NoisePerlin3DEased(np, x, y, z, s) ((np)->offset + (np)->scale * \
187                 noise3d_perlin((float)(x) / (np)->spread.X, (float)(y) / (np)->spread.Y, \
188                 (float)(z) / (np)->spread.Z, (s) + (np)->seed, (np)->octaves, \
189                 (np)->persist, true))
190
191 #endif
192