Panda3D
stackedPerlinNoise2.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file stackedPerlinNoise2.cxx
10  * @author drose
11  * @date 2005-10-05
12  */
13 
14 #include "stackedPerlinNoise2.h"
15 
16 /**
17  * Creates num_levels nested PerlinNoise2 objects. Each stacked Perlin object
18  * will have a scale of 1 scale_factor times the previous object (so that it
19  * is higher-frequency, if scale_factor > 1), and an amplitude of amp_scale
20  * times the previous object (so that it is less important, if amp_scale < 1).
21  */
23 StackedPerlinNoise2(double sx, double sy, int num_levels,
24  double scale_factor, double amp_scale,
25  int table_size, unsigned long seed) {
26  _noises.reserve(num_levels);
27  double amp = 1.0;
28  for (int i = 0; i < num_levels; ++i) {
29  PerlinNoise2 noise(sx, sy, table_size, seed);
30  add_level(noise, amp);
31 
32  seed = noise.get_seed();
33  amp *= amp_scale;
34  sx /= scale_factor;
35  sy /= scale_factor;
36  }
37 }
38 
39 /**
40  * Creates an exact duplicate of the existing StackedPerlinNoise2 object,
41  * including the random seed.
42  */
45  _noises(copy._noises)
46 {
47 }
48 
49 /**
50  * Creates an exact duplicate of the existing StackedPerlinNoise2 object,
51  * including the random seed.
52  */
55  _noises = copy._noises;
56 }
57 
58 /**
59  * Adds an arbitrary PerlinNoise2 object, and an associated amplitude, to the
60  * stack.
61  */
63 add_level(const PerlinNoise2 &level, double amp) {
64  _noises.push_back(Noise());
65  Noise &n = _noises.back();
66  n._noise = level;
67  n._amp = amp;
68 }
69 
70 /**
71  * Removes all levels from the stack. You must call add_level() again to
72  * restore them.
73  */
75 clear() {
76  _noises.clear();
77 }
78 
79 /**
80  * Returns the noise function of the three inputs.
81  */
83 noise(const LVecBase2d &value) {
84  double result = 0.0;
85 
86  Noises::iterator ni;
87  for (ni = _noises.begin(); ni != _noises.end(); ++ni) {
88  result += (*ni)._noise(value) * (*ni)._amp;
89  }
90 
91  return result;
92 }
double noise(double x, double y)
Returns the noise function of the three inputs.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void add_level(const PerlinNoise2 &level, double amp=1.0)
Adds an arbitrary PerlinNoise2 object, and an associated amplitude, to the stack.
This class provides an implementation of Perlin noise for 2 variables.
Definition: perlinNoise2.h:25
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.