Panda3D
 All Classes Functions Variables Enumerations
stackedPerlinNoise2.cxx
1 // Filename: stackedPerlinNoise2.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 "stackedPerlinNoise2.h"
16 
17 ////////////////////////////////////////////////////////////////////
18 // Function: StackedPerlinNoise2::Constructor
19 // Access: Published
20 // Description: Creates num_levels nested PerlinNoise2 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 StackedPerlinNoise2(double sx, double sy, 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  PerlinNoise2 noise(sx, sy, 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  }
42 }
43 
44 ////////////////////////////////////////////////////////////////////
45 // Function: StackedPerlinNoise2::Copy Constructor
46 // Access: Published
47 // Description: Creates an exact duplicate of the existing
48 // StackedPerlinNoise2 object, including the random
49 // seed.
50 ////////////////////////////////////////////////////////////////////
53  _noises(copy._noises)
54 {
55 }
56 
57 ////////////////////////////////////////////////////////////////////
58 // Function: StackedPerlinNoise2::Copy Assignment Operator
59 // Access: Published
60 // Description: Creates an exact duplicate of the existing
61 // StackedPerlinNoise2 object, including the random
62 // seed.
63 ////////////////////////////////////////////////////////////////////
66  _noises = copy._noises;
67 }
68 
69 ////////////////////////////////////////////////////////////////////
70 // Function: StackedPerlinNoise2::add_level
71 // Access: Published
72 // Description: Adds an arbitrary PerlinNoise2 object, and an
73 // associated amplitude, to the stack.
74 ////////////////////////////////////////////////////////////////////
76 add_level(const PerlinNoise2 &level, double amp) {
77  _noises.push_back(Noise());
78  Noise &n = _noises.back();
79  n._noise = level;
80  n._amp = amp;
81 }
82 
83 ////////////////////////////////////////////////////////////////////
84 // Function: StackedPerlinNoise2::clear
85 // Access: Published
86 // Description: Removes all levels from the stack. You must call
87 // add_level() again to restore them.
88 ////////////////////////////////////////////////////////////////////
90 clear() {
91  _noises.clear();
92 }
93 
94 ////////////////////////////////////////////////////////////////////
95 // Function: StackedPerlinNoise2::noise
96 // Access: Published
97 // Description: Returns the noise function of the three inputs.
98 ////////////////////////////////////////////////////////////////////
100 noise(const LVecBase2d &value) {
101  double result = 0.0;
102 
103  Noises::iterator ni;
104  for (ni = _noises.begin(); ni != _noises.end(); ++ni) {
105  result += (*ni)._noise(value) * (*ni)._amp;
106  }
107 
108  return result;
109 }
double noise(double x, double y)
Returns the noise function of the three inputs.
void add_level(const PerlinNoise2 &level, double amp=1.0)
Adds an arbitrary PerlinNoise2 object, and an associated amplitude, to the stack. ...
This is the base class for all two-component vectors and points.
Definition: lvecBase2.h:1241
This class provides an implementation of Perlin noise for 2 variables.
Definition: perlinNoise2.h:28
void clear()
Removes all levels from the stack.
Implements a multi-layer PerlinNoise, with one or more high-frequency noise functions added to a lowe...
StackedPerlinNoise2()
Creates a StackedPerlinNoise2 object with no levels.
void operator=(const StackedPerlinNoise2 &copy)
Creates an exact duplicate of the existing StackedPerlinNoise2 object, including the random seed...
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