]> git.lizzy.rs Git - minetest.git/blob - src/noise.h
34dcb7374ebda7dd887a123edb0442517cdd5b9a
[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
77         NoiseParams() {}
78
79         NoiseParams(float offset_, float scale_, v3f spread_,
80                 int seed_, int octaves_, float persist_)
81         {
82                 offset  = offset_;
83                 scale   = scale_;
84                 spread  = spread_;
85                 seed    = seed_;
86                 octaves = octaves_;
87                 persist = persist_;
88         }
89 };
90
91
92 // Convenience macros for getting/setting NoiseParams in Settings
93
94 #define NOISEPARAMS_FMT_STR "f,f,v3,s32,s32,f"
95
96 #define getNoiseParams(x, y) getStruct((x), NOISEPARAMS_FMT_STR, &(y), sizeof(y))
97 #define setNoiseParams(x, y) setStruct((x), NOISEPARAMS_FMT_STR, &(y))
98
99 class Noise {
100 public:
101         NoiseParams *np;
102         int seed;
103         int sx;
104         int sy;
105         int sz;
106         float *noisebuf;
107         float *buf;
108         float *result;
109
110         Noise(NoiseParams *np, int seed, int sx, int sy, int sz=1);
111         ~Noise();
112
113         void setSize(int sx, int sy, int sz=1);
114         void setSpreadFactor(v3f spread);
115         void setOctaves(int octaves);
116         void resizeNoiseBuf(bool is3d);
117
118         void gradientMap2D(
119                 float x, float y,
120                 float step_x, float step_y,
121                 int seed);
122         void gradientMap3D(
123                 float x, float y, float z,
124                 float step_x, float step_y, float step_z,
125                 int seed, bool eased=false);
126         float *perlinMap2D(float x, float y);
127         float *perlinMap2DModulated(float x, float y, float *persist_map);
128         float *perlinMap3D(float x, float y, float z, bool eased=false);
129         void transformNoiseMap();
130 };
131
132 // Return value: -1 ... 1
133 float noise2d(int x, int y, int seed);
134 float noise3d(int x, int y, int z, int seed);
135
136 float noise2d_gradient(float x, float y, int seed);
137 float noise3d_gradient(float x, float y, float z, int seed);
138
139 float noise2d_perlin(float x, float y, int seed,
140                 int octaves, float persistence);
141
142 float noise2d_perlin_abs(float x, float y, int seed,
143                 int octaves, float persistence);
144
145 float noise3d_perlin(float x, float y, float z, int seed,
146                 int octaves, float persistence);
147
148 float noise3d_perlin_abs(float x, float y, float z, int seed,
149                 int octaves, float persistence);
150
151 inline float easeCurve(float t) {
152         return t * t * t * (t * (6.f * t - 15.f) + 10.f);
153 }
154
155 #define NoisePerlin2D(np, x, y, s) \
156                 ((np)->offset + (np)->scale * noise2d_perlin( \
157                 (float)(x) / (np)->spread.X, \
158                 (float)(y) / (np)->spread.Y, \
159                 (s) + (np)->seed, (np)->octaves, (np)->persist))
160
161 #define NoisePerlin2DNoTxfm(np, x, y, s) \
162                 (noise2d_perlin( \
163                 (float)(x) / (np)->spread.X, \
164                 (float)(y) / (np)->spread.Y, \
165                 (s) + (np)->seed, (np)->octaves, (np)->persist))
166
167 #define NoisePerlin2DPosOffset(np, x, xoff, y, yoff, s) \
168                 ((np)->offset + (np)->scale * noise2d_perlin( \
169                 (float)(xoff) + (float)(x) / (np)->spread.X, \
170                 (float)(yoff) + (float)(y) / (np)->spread.Y, \
171                 (s) + (np)->seed, (np)->octaves, (np)->persist))
172
173 #define NoisePerlin2DNoTxfmPosOffset(np, x, xoff, y, yoff, s) \
174                 (noise2d_perlin( \
175                 (float)(xoff) + (float)(x) / (np)->spread.X, \
176                 (float)(yoff) + (float)(y) / (np)->spread.Y, \
177                 (s) + (np)->seed, (np)->octaves, (np)->persist))
178
179 #define NoisePerlin3D(np, x, y, z, s) ((np)->offset + (np)->scale * \
180                 noise3d_perlin((float)(x) / (np)->spread.X, (float)(y) / (np)->spread.Y, \
181                 (float)(z) / (np)->spread.Z, (s) + (np)->seed, (np)->octaves, (np)->persist))
182
183 #endif
184