3 * Copyright (C) 2010-2014 celeron55, Perttu Ahola <celeron55@gmail.com>
4 * Copyright (C) 2010-2014 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
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.
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.
31 #include "util/string.h"
33 extern FlagDesc flagdesc_noiseparams[];
38 PseudoRandom(): m_next(0)
41 PseudoRandom(int seed): m_next(seed)
51 m_next = m_next * 1103515245 + 12345;
52 return((unsigned)(m_next/65536) % 32768);
54 int range(int min, int max)
56 if(max-min > 32768/10)
58 //dstream<<"WARNING: PseudoRandom::range: max > 32767"<<std::endl;
66 return (next()%(max-min+1))+min;
72 #define NOISE_FLAG_DEFAULTS 0x01
73 #define NOISE_FLAG_EASED 0x02
74 #define NOISE_FLAG_ABSVALUE 0x04
76 //// TODO(hmmmm): implement these!
77 #define NOISE_FLAG_POINTBUFFER 0x08
78 #define NOISE_FLAG_SIMPLEX 0x10
93 spread = v3f(250, 250, 250);
98 flags = NOISE_FLAG_DEFAULTS;
101 NoiseParams(float offset_, float scale_, v3f spread_, s32 seed_,
102 u16 octaves_, float persist_, float lacunarity_,
103 u32 flags_=NOISE_FLAG_DEFAULTS)
111 lacunarity = lacunarity_;
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))
135 Noise(NoiseParams *np, int seed, int sx, int sy, int sz=1);
138 void setSize(int sx, int sy, int sz=1);
139 void setSpreadFactor(v3f spread);
140 void setOctaves(int octaves);
144 float step_x, float step_y,
147 float x, float y, float z,
148 float step_x, float step_y, float step_z,
151 float *perlinMap2D(float x, float y, float *persistence_map=NULL);
152 float *perlinMap3D(float x, float y, float z, float *persistence_map=NULL);
154 void transformNoiseMap();
158 void resizeNoiseBuf(bool is3d);
159 void updateResults(float g, float *gmap, float *persistence_map, size_t bufsize);
163 // Return value: -1 ... 1
164 float noise2d(int x, int y, int seed);
165 float noise3d(int x, int y, int z, int seed);
167 float noise2d_gradient(float x, float y, int seed, bool eased=true);
168 float noise3d_gradient(float x, float y, float z, int seed, bool eased=false);
170 float noise2d_perlin(float x, float y, int seed,
171 int octaves, float persistence, bool eased=true);
173 float noise2d_perlin_abs(float x, float y, int seed,
174 int octaves, float persistence, bool eased=true);
176 float noise3d_perlin(float x, float y, float z, int seed,
177 int octaves, float persistence, bool eased=false);
179 float noise3d_perlin_abs(float x, float y, float z, int seed,
180 int octaves, float persistence, bool eased=false);
182 inline float easeCurve(float t)
184 return t * t * t * (t * (6.f * t - 15.f) + 10.f);
187 float contour(float v);
189 #define NoisePerlin2D(np, x, y, s) \
190 ((np)->offset + (np)->scale * noise2d_perlin( \
191 (float)(x) / (np)->spread.X, \
192 (float)(y) / (np)->spread.Y, \
193 (s) + (np)->seed, (np)->octaves, (np)->persist))
195 #define NoisePerlin2DNoTxfm(np, x, y, s) \
197 (float)(x) / (np)->spread.X, \
198 (float)(y) / (np)->spread.Y, \
199 (s) + (np)->seed, (np)->octaves, (np)->persist))
201 #define NoisePerlin2DPosOffset(np, x, xoff, y, yoff, s) \
202 ((np)->offset + (np)->scale * noise2d_perlin( \
203 (float)(xoff) + (float)(x) / (np)->spread.X, \
204 (float)(yoff) + (float)(y) / (np)->spread.Y, \
205 (s) + (np)->seed, (np)->octaves, (np)->persist))
207 #define NoisePerlin2DNoTxfmPosOffset(np, x, xoff, y, yoff, s) \
209 (float)(xoff) + (float)(x) / (np)->spread.X, \
210 (float)(yoff) + (float)(y) / (np)->spread.Y, \
211 (s) + (np)->seed, (np)->octaves, (np)->persist))
213 #define NoisePerlin3D(np, x, y, z, s) ((np)->offset + (np)->scale * \
214 noise3d_perlin((float)(x) / (np)->spread.X, (float)(y) / (np)->spread.Y, \
215 (float)(z) / (np)->spread.Z, (s) + (np)->seed, (np)->octaves, (np)->persist))
217 #define NoisePerlin3DEased(np, x, y, z, s) ((np)->offset + (np)->scale * \
218 noise3d_perlin((float)(x) / (np)->spread.X, (float)(y) / (np)->spread.Y, \
219 (float)(z) / (np)->spread.Z, (s) + (np)->seed, (np)->octaves, \
220 (np)->persist, true))