Panda3D
stackedPerlinNoise3.cxx
1 // Filename: stackedPerlinNoise3.cxx
2 // Created by: drose (05Oct05)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "stackedPerlinNoise3.h"
16 
17 ////////////////////////////////////////////////////////////////////
18 // Function: StackedPerlinNoise3::Constructor
19 // Access: Published
20 // Description: Creates num_levels nested PerlinNoise3 objects. Each
21 // stacked Perlin object will have a scale of 1 /
22 // scale_factor times the previous object (so that it is
23 // higher-frequency, if scale_factor > 1), and an
24 // amplitude of amp_scale times the previous object (so
25 // that it is less important, if amp_scale < 1).
26 ////////////////////////////////////////////////////////////////////
28 StackedPerlinNoise3(double sx, double sy, double sz, int num_levels,
29  double scale_factor, double amp_scale,
30  int table_size, unsigned long seed) {
31  _noises.reserve(num_levels);
32  double amp = 1.0;
33  for (int i = 0; i < num_levels; ++i) {
34  PerlinNoise3 noise(sx, sy, sz, table_size, seed);
35  add_level(noise, amp);
36 
37  seed = noise.get_seed();
38  amp *= amp_scale;
39  sx /= scale_factor;
40  sy /= scale_factor;
41  sz /= scale_factor;
42  }
43 }
44 
45 ////////////////////////////////////////////////////////////////////
46 // Function: StackedPerlinNoise3::Copy Constructor
47 // Access: Published
48 // Description: Creates an exact duplicate of the existing
49 // StackedPerlinNoise3 object, including the random
50 // seed.
51 ////////////////////////////////////////////////////////////////////
54  _noises(copy._noises)
55 {
56 }
57 
58 ////////////////////////////////////////////////////////////////////
59 // Function: StackedPerlinNoise3::Copy Assignment Operator
60 // Access: Published
61 // Description: Creates an exact duplicate of the existing
62 // StackedPerlinNoise3 object, including the random
63 // seed.
64 ////////////////////////////////////////////////////////////////////
67  _noises = copy._noises;
68 }
69 
70 ////////////////////////////////////////////////////////////////////
71 // Function: StackedPerlinNoise3::add_level
72 // Access: Published
73 // Description: Adds an arbitrary PerlinNoise3 object, and an
74 // associated amplitude, to the stack.
75 ////////////////////////////////////////////////////////////////////
77 add_level(const PerlinNoise3 &level, double amp) {
78  _noises.push_back(Noise());
79  Noise &n = _noises.back();
80  n._noise = level;
81  n._amp = amp;
82 }
83 
84 ////////////////////////////////////////////////////////////////////
85 // Function: StackedPerlinNoise3::clear
86 // Access: Published
87 // Description: Removes all levels from the stack. You must call
88 // add_level() again to restore them.
89 ////////////////////////////////////////////////////////////////////
91 clear() {
92  _noises.clear();
93 }
94 
95 ////////////////////////////////////////////////////////////////////
96 // Function: StackedPerlinNoise3::noise
97 // Access: Published
98 // Description: Returns the noise function of the three inputs.
99 ////////////////////////////////////////////////////////////////////
101 noise(const LVecBase3d &value) {
102  double result = 0.0;
103 
104  Noises::iterator ni;
105  for (ni = _noises.begin(); ni != _noises.end(); ++ni) {
106  result += (*ni)._noise(value) * (*ni)._amp;
107  }
108 
109  return result;
110 }
void add_level(const PerlinNoise3 &level, double amp=1.0)
Adds an arbitrary PerlinNoise3 object, and an associated amplitude, to the stack. ...
This class provides an implementation of Perlin noise for 3 variables.
Definition: perlinNoise3.h:28
StackedPerlinNoise3()
Creates a StackedPerlinNoise3 object with no levels.
This is the base class for all three-component vectors and points.
Definition: lvecBase3.h:1471
unsigned long get_seed()
Returns a unique seed value based on the seed value passed to this PerlinNoise object (and on its cur...
Definition: perlinNoise.I:46
double noise(double x, double y, double z)
Returns the noise function of the three inputs.
Implements a multi-layer PerlinNoise, with one or more high-frequency noise functions added to a lowe...
void operator=(const StackedPerlinNoise3 &copy)
Creates an exact duplicate of the existing StackedPerlinNoise3 object, including the random seed...
void clear()
Removes all levels from the stack.