3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2010-2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 PseudoRandom(): m_next(0)
33 PseudoRandom(int seed): m_next(seed)
43 m_next = m_next * 1103515245 + 12345;
44 return((unsigned)(m_next/65536) % 32768);
46 int range(int min, int max)
48 if(max-min > 32768/10)
50 //dstream<<"WARNING: PseudoRandom::range: max > 32767"<<std::endl;
58 return (next()%(max-min+1))+min;
74 NoiseParams(float offset_, float scale_, v3f spread_,
75 int seed_, int octaves_, float persist_)
87 // Convenience macros for getting/setting NoiseParams in Settings
89 #define NOISEPARAMS_FMT_STR "f,f,v3,s32,s32,f"
91 #define getNoiseParams(x, y) getStruct((x), NOISEPARAMS_FMT_STR, &(y), sizeof(y))
92 #define setNoiseParams(x, y) setStruct((x), NOISEPARAMS_FMT_STR, &(y))
105 Noise(NoiseParams *np, int seed, int sx, int sy);
106 Noise(NoiseParams *np, int seed, int sx, int sy, int sz);
109 virtual void init(NoiseParams *np, int seed, int sx, int sy, int sz);
110 void setSize(int sx, int sy);
111 void setSize(int sx, int sy, int sz);
112 void setSpreadFactor(v3f spread);
113 void setOctaves(int octaves);
114 void resizeNoiseBuf(bool is3d);
118 float step_x, float step_y,
121 float x, float y, float z,
122 float step_x, float step_y, float step_z,
124 float *perlinMap2D(float x, float y);
125 float *perlinMap2DModulated(float x, float y, float *persist_map);
126 float *perlinMap3D(float x, float y, float z);
127 void transformNoiseMap();
130 // Return value: -1 ... 1
131 float noise2d(int x, int y, int seed);
132 float noise3d(int x, int y, int z, int seed);
134 float noise2d_gradient(float x, float y, int seed);
135 float noise3d_gradient(float x, float y, float z, int seed);
137 float noise2d_perlin(float x, float y, int seed,
138 int octaves, float persistence);
140 float noise2d_perlin_abs(float x, float y, int seed,
141 int octaves, float persistence);
143 float noise3d_perlin(float x, float y, float z, int seed,
144 int octaves, float persistence);
146 float noise3d_perlin_abs(float x, float y, float z, int seed,
147 int octaves, float persistence);
149 inline float easeCurve(float t) {
150 return t * t * t * (t * (6.f * t - 15.f) + 10.f);
153 #define NoisePerlin2D(np, x, y, s) \
154 ((np)->offset + (np)->scale * noise2d_perlin( \
155 (float)(x) / (np)->spread.X, \
156 (float)(y) / (np)->spread.Y, \
157 (s) + (np)->seed, (np)->octaves, (np)->persist))
159 #define NoisePerlin2DNoTxfm(np, x, y, s) \
161 (float)(x) / (np)->spread.X, \
162 (float)(y) / (np)->spread.Y, \
163 (s) + (np)->seed, (np)->octaves, (np)->persist))
165 #define NoisePerlin2DPosOffset(np, x, xoff, y, yoff, s) \
166 ((np)->offset + (np)->scale * noise2d_perlin( \
167 (float)(xoff) + (float)(x) / (np)->spread.X, \
168 (float)(yoff) + (float)(y) / (np)->spread.Y, \
169 (s) + (np)->seed, (np)->octaves, (np)->persist))
171 #define NoisePerlin2DNoTxfmPosOffset(np, x, xoff, y, yoff, s) \
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 NoisePerlin3D(np, x, y, z, s) ((np)->offset + (np)->scale * \
178 noise3d_perlin((float)(x) / (np)->spread.X, (float)(y) / (np)->spread.Y, \
179 (float)(z) / (np)->spread.Z, (s) + (np)->seed, (np)->octaves, (np)->persist))