Panda3D
perlinNoise2.I
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 perlinNoise2.I
10  * @author drose
11  * @date 2005-10-05
12  */
13 
14 /**
15  * Randomizes the tables to make a unique noise function. Uses a default
16  * scale (noise frequency), table size, and seed.
17  */
18 INLINE PerlinNoise2::
20  PerlinNoise(256, 0)
21 {
22  init_unscaled_xform();
23  _input_xform = _unscaled_xform;
24 }
25 
26 /**
27  * Randomizes the tables to make a unique noise function.
28  *
29  * If seed is nonzero, it is used to define the tables; if it is zero a random
30  * seed is generated.
31  */
32 INLINE PerlinNoise2::
33 PerlinNoise2(double sx, double sy,
34  int table_size, unsigned long seed) :
35  PerlinNoise(table_size, seed)
36 {
37  init_unscaled_xform();
38  set_scale(sx, sy);
39 }
40 
41 /**
42  * Makes an exact copy of the existing PerlinNoise object, including its
43  * random seed.
44  */
45 INLINE PerlinNoise2::
46 PerlinNoise2(const PerlinNoise2 &copy) :
47  PerlinNoise(copy),
48  _unscaled_xform(copy._unscaled_xform),
49  _input_xform(copy._input_xform)
50 {
51 }
52 
53 /**
54  * Makes an exact copy of the existing PerlinNoise object, including its
55  * random seed.
56  */
57 INLINE void PerlinNoise2::
58 operator = (const PerlinNoise2 &copy) {
59  PerlinNoise::operator = (copy);
60  _unscaled_xform = copy._unscaled_xform;
61  _input_xform = copy._input_xform;
62 }
63 
64 /**
65  * Changes the scale (frequency) of the noise.
66  */
67 INLINE void PerlinNoise2::
68 set_scale(double scale) {
69  set_scale(scale, scale);
70 }
71 
72 /**
73  * Changes the scale (frequency) of the noise.
74  */
75 INLINE void PerlinNoise2::
76 set_scale(double x, double y) {
77  set_scale(LVecBase2d(x, y));
78 }
79 
80 /**
81  * Changes the scale (frequency) of the noise.
82  */
83 INLINE void PerlinNoise2::
84 set_scale(const LVecBase2f &value) {
85  set_scale(value[0], value[1]);
86 }
87 
88 /**
89  * Changes the scale (frequency) of the noise.
90  */
91 INLINE void PerlinNoise2::
92 set_scale(const LVecBase2d &value) {
93  _input_xform = LMatrix3d::scale_mat(1.0f / value[0], 1.0f / value[1]) * _unscaled_xform;
94 }
95 
96 /**
97  * Returns the noise function of the three inputs.
98  */
99 INLINE double PerlinNoise2::
100 noise(double x, double y) const {
101  return noise(LVecBase2d(x, y));
102 }
103 
104 /**
105  * Returns the noise function of the three inputs.
106  */
107 INLINE float PerlinNoise2::
108 noise(const LVecBase2f &value) const {
109  return (float)noise(value[0], value[1]);
110 }
111 
112 /**
113  * Returns the noise function of the two inputs.
114  */
115 INLINE double PerlinNoise2::
116 operator ()(double x, double y) const {
117  return noise(x, y);
118 }
119 
120 /**
121  * Returns the noise function of the two inputs.
122  */
123 INLINE float PerlinNoise2::
124 operator ()(const LVecBase2f &value) const {
125  return noise(value);
126 }
127 
128 /**
129  * Returns the noise function of the two inputs.
130  */
131 INLINE double PerlinNoise2::
132 operator ()(const LVecBase2d &value) const {
133  return noise(value);
134 }
135 
136 /**
137  * Returns the dot product of a random gradient vector (determined by the hash
138  * code) with the indicated offset vector.
139  */
140 INLINE double PerlinNoise2::
141 grad(int hash, double x, double y) {
142  // Convert low 3 bits of hash code into 8 gradient directions.
143  switch (hash & 7) {
144  // Four corners.
145  case 0: return x + y;
146  case 1: return x - y;
147  case 2: return -x + y;
148  case 3: return -x - y;
149 
150  // Four edges. Here we scale by 1.707 to make all the vectors equal
151  // length, and to make their lengths consistent with PerlinNoise3.
152  case 4: return 1.707 * x;
153  case 5: return 1.707 * y;
154  case 6: return -1.707 * x;
155  case 7: return -1.707 * y;
156  }
157 
158  nassertr(false, 0);
159  return 0;
160 }
This is the base class for PerlinNoise2 and PerlinNoise3, different dimensions of Perlin noise implem...
Definition: perlinNoise.h:28
void set_scale(double scale)
Changes the scale (frequency) of the noise.
Definition: perlinNoise2.I:68
double operator()(double x, double y) const
Returns the noise function of the two inputs.
Definition: perlinNoise2.I:116
This class provides an implementation of Perlin noise for 2 variables.
Definition: perlinNoise2.h:25
void operator=(const PerlinNoise2 &copy)
Makes an exact copy of the existing PerlinNoise object, including its random seed.
Definition: perlinNoise2.I:58
double noise(double x, double y) const
Returns the noise function of the three inputs.
Definition: perlinNoise2.I:100
PerlinNoise2()
Randomizes the tables to make a unique noise function.
Definition: perlinNoise2.I:19