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.
35 PseudoRandom(): m_next(0)
38 PseudoRandom(int seed): m_next(seed)
48 m_next = m_next * 1103515245 + 12345;
49 return((unsigned)(m_next/65536) % 32768);
51 int range(int min, int max)
53 if(max-min > 32768/10)
55 //dstream<<"WARNING: PseudoRandom::range: max > 32767"<<std::endl;
63 return (next()%(max-min+1))+min;
80 NoiseParams(float offset_, float scale_, v3f spread_,
81 int seed_, int octaves_, float persist_, bool eased_=false)
94 // Convenience macros for getting/setting NoiseParams in Settings
96 #define NOISEPARAMS_FMT_STR "f,f,v3,s32,s32,f"
98 #define getNoiseParams(x, y) getStruct((x), NOISEPARAMS_FMT_STR, &(y), sizeof(y))
99 #define setNoiseParams(x, y) setStruct((x), NOISEPARAMS_FMT_STR, &(y))
112 Noise(NoiseParams *np, int seed, int sx, int sy, int sz=1);
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);
122 float step_x, float step_y,
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();
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);
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);
141 float noise2d_perlin(float x, float y, int seed,
142 int octaves, float persistence);
144 float noise2d_perlin_abs(float x, float y, int seed,
145 int octaves, float persistence);
147 float noise3d_perlin(float x, float y, float z, int seed,
148 int octaves, float persistence, bool eased=false);
150 float noise3d_perlin_abs(float x, float y, float z, int seed,
151 int octaves, float persistence, bool eased=false);
153 inline float easeCurve(float t) {
154 return t * t * t * (t * (6.f * t - 15.f) + 10.f);
157 float contour(float v);
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))
165 #define NoisePerlin2DNoTxfm(np, x, y, s) \
167 (float)(x) / (np)->spread.X, \
168 (float)(y) / (np)->spread.Y, \
169 (s) + (np)->seed, (np)->octaves, (np)->persist))
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))
177 #define NoisePerlin2DNoTxfmPosOffset(np, x, xoff, y, yoff, s) \
179 (float)(xoff) + (float)(x) / (np)->spread.X, \
180 (float)(yoff) + (float)(y) / (np)->spread.Y, \
181 (s) + (np)->seed, (np)->octaves, (np)->persist))
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))
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)