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;
79 NoiseParams(float offset_, float scale_, v3f spread_,
80 int seed_, int octaves_, float persist_)
92 // Convenience macros for getting/setting NoiseParams in Settings
94 #define NOISEPARAMS_FMT_STR "f,f,v3,s32,s32,f"
96 #define getNoiseParams(x, y) getStruct((x), NOISEPARAMS_FMT_STR, &(y), sizeof(y))
97 #define setNoiseParams(x, y) setStruct((x), NOISEPARAMS_FMT_STR, &(y))
110 Noise(NoiseParams *np, int seed, int sx, int sy, int sz=1);
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);
120 float step_x, float step_y,
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();
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);
136 float noise2d_gradient(float x, float y, int seed);
137 float noise3d_gradient(float x, float y, float z, int seed);
139 float noise2d_perlin(float x, float y, int seed,
140 int octaves, float persistence);
142 float noise2d_perlin_abs(float x, float y, int seed,
143 int octaves, float persistence);
145 float noise3d_perlin(float x, float y, float z, int seed,
146 int octaves, float persistence);
148 float noise3d_perlin_abs(float x, float y, float z, int seed,
149 int octaves, float persistence);
151 inline float easeCurve(float t) {
152 return t * t * t * (t * (6.f * t - 15.f) + 10.f);
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))
161 #define NoisePerlin2DNoTxfm(np, x, y, s) \
163 (float)(x) / (np)->spread.X, \
164 (float)(y) / (np)->spread.Y, \
165 (s) + (np)->seed, (np)->octaves, (np)->persist))
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))
173 #define NoisePerlin2DNoTxfmPosOffset(np, x, xoff, y, yoff, s) \
175 (float)(xoff) + (float)(x) / (np)->spread.X, \
176 (float)(yoff) + (float)(y) / (np)->spread.Y, \
177 (s) + (np)->seed, (np)->octaves, (np)->persist))
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))