1 // perlin.cpp
2 //
3 // Copyright (C) 2003, 2004 Jason Bevins
4 //
5 // This library is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation; either version 2.1 of the License, or (at
8 // your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
13 // License (COPYING.txt) for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library; if not, write to the Free Software Foundation,
17 // Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 //
19 // The developer's email is jlbezigvins@gmzigail.com (for great email, take
20 // off every 'zig'.)
21 //
22 
23 #include "perlin.h"
24 
25 using namespace noise::module;
26 
Perlin()27 Perlin::Perlin ():
28   Module (GetSourceModuleCount ()),
29   m_frequency    (DEFAULT_PERLIN_FREQUENCY   ),
30   m_lacunarity   (DEFAULT_PERLIN_LACUNARITY  ),
31   m_noiseQuality (DEFAULT_PERLIN_QUALITY     ),
32   m_octaveCount  (DEFAULT_PERLIN_OCTAVE_COUNT),
33   m_persistence  (DEFAULT_PERLIN_PERSISTENCE ),
34   m_seed         (DEFAULT_PERLIN_SEED)
35 {
36 }
37 
GetValue(double x,double y,double z) const38 double Perlin::GetValue (double x, double y, double z) const
39 {
40   double value = 0.0;
41   double signal = 0.0;
42   double curPersistence = 1.0;
43   double nx, ny, nz;
44   int seed;
45 
46   x *= m_frequency;
47   y *= m_frequency;
48   z *= m_frequency;
49 
50   for (int curOctave = 0; curOctave < m_octaveCount; curOctave++) {
51 
52     // Make sure that these floating-point values have the same range as a 32-
53     // bit integer so that we can pass them to the coherent-noise functions.
54     nx = MakeInt32Range (x);
55     ny = MakeInt32Range (y);
56     nz = MakeInt32Range (z);
57 
58     // Get the coherent-noise value from the input value and add it to the
59     // final result.
60     seed = (m_seed + curOctave) & 0xffffffff;
61     signal = GradientCoherentNoise3D (nx, ny, nz, seed, m_noiseQuality);
62     value += signal * curPersistence;
63 
64     // Prepare the next octave.
65     x *= m_lacunarity;
66     y *= m_lacunarity;
67     z *= m_lacunarity;
68     curPersistence *= m_persistence;
69   }
70 
71   return value;
72 }
73