Panda3D
 All Classes Functions Variables Enumerations
perlinNoise2.I
1 // Filename: perlinNoise2.I
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 
16 ////////////////////////////////////////////////////////////////////
17 // Function: PerlinNoise2::Default Constructor
18 // Access: Published
19 // Description: Randomizes the tables to make a unique noise
20 // function. Uses a default scale (noise frequency),
21 // table size, and seed.
22 ////////////////////////////////////////////////////////////////////
23 INLINE PerlinNoise2::
25  PerlinNoise(256, 0)
26 {
27  init_unscaled_xform();
28  _input_xform = _unscaled_xform;
29 }
30 
31 ////////////////////////////////////////////////////////////////////
32 // Function: PerlinNoise2::Constructor
33 // Access: Published
34 // Description: Randomizes the tables to make a unique noise
35 // function.
36 //
37 // If seed is nonzero, it is used to define the tables;
38 // if it is zero a random seed is generated.
39 ////////////////////////////////////////////////////////////////////
40 INLINE PerlinNoise2::
41 PerlinNoise2(double sx, double sy,
42  int table_size, unsigned long seed) :
43  PerlinNoise(table_size, seed)
44 {
45  init_unscaled_xform();
46  set_scale(sx, sy);
47 }
48 
49 ////////////////////////////////////////////////////////////////////
50 // Function: PerlinNoise2::Copy Constructor
51 // Access: Published
52 // Description: Makes an exact copy of the existing PerlinNoise
53 // object, including its random seed.
54 ////////////////////////////////////////////////////////////////////
55 INLINE PerlinNoise2::
56 PerlinNoise2(const PerlinNoise2 &copy) :
57  PerlinNoise(copy),
58  _unscaled_xform(copy._unscaled_xform),
59  _input_xform(copy._input_xform)
60 {
61 }
62 
63 ////////////////////////////////////////////////////////////////////
64 // Function: PerlinNoise2::Copy Assignment Operator
65 // Access: Published
66 // Description: Makes an exact copy of the existing PerlinNoise
67 // object, including its random seed.
68 ////////////////////////////////////////////////////////////////////
69 INLINE void PerlinNoise2::
70 operator = (const PerlinNoise2 &copy) {
71  PerlinNoise::operator = (copy);
72  _unscaled_xform = copy._unscaled_xform;
73  _input_xform = copy._input_xform;
74 }
75 
76 ////////////////////////////////////////////////////////////////////
77 // Function: PerlinNoise2::set_scale
78 // Access: Published
79 // Description: Changes the scale (frequency) of the noise.
80 ////////////////////////////////////////////////////////////////////
81 INLINE void PerlinNoise2::
82 set_scale(double scale) {
83  set_scale(scale, scale);
84 }
85 
86 ////////////////////////////////////////////////////////////////////
87 // Function: PerlinNoise2::set_scale
88 // Access: Published
89 // Description: Changes the scale (frequency) of the noise.
90 ////////////////////////////////////////////////////////////////////
91 INLINE void PerlinNoise2::
92 set_scale(double x, double y) {
93  set_scale(LVecBase2d(x, y));
94 }
95 
96 ////////////////////////////////////////////////////////////////////
97 // Function: PerlinNoise2::set_scale
98 // Access: Published
99 // Description: Changes the scale (frequency) of the noise.
100 ////////////////////////////////////////////////////////////////////
101 INLINE void PerlinNoise2::
102 set_scale(const LVecBase2f &value) {
103  set_scale(value[0], value[1]);
104 }
105 
106 ////////////////////////////////////////////////////////////////////
107 // Function: PerlinNoise2::set_scale
108 // Access: Published
109 // Description: Changes the scale (frequency) of the noise.
110 ////////////////////////////////////////////////////////////////////
111 INLINE void PerlinNoise2::
112 set_scale(const LVecBase2d &value) {
113  _input_xform = LMatrix3d::scale_mat(1.0f / value[0], 1.0f / value[1]) * _unscaled_xform;
114 }
115 
116 ////////////////////////////////////////////////////////////////////
117 // Function: PerlinNoise2::noise
118 // Access: Published
119 // Description: Returns the noise function of the three inputs.
120 ////////////////////////////////////////////////////////////////////
121 INLINE double PerlinNoise2::
122 noise(double x, double y) const {
123  return noise(LVecBase2d(x, y));
124 }
125 
126 ////////////////////////////////////////////////////////////////////
127 // Function: PerlinNoise2::noise
128 // Access: Published
129 // Description: Returns the noise function of the three inputs.
130 ////////////////////////////////////////////////////////////////////
131 INLINE float PerlinNoise2::
132 noise(const LVecBase2f &value) const {
133  return (float)noise(value[0], value[1]);
134 }
135 
136 ////////////////////////////////////////////////////////////////////
137 // Function: PerlinNoise2::operator ()
138 // Access: Published
139 // Description: Returns the noise function of the two inputs.
140 ////////////////////////////////////////////////////////////////////
141 INLINE double PerlinNoise2::
142 operator ()(double x, double y) const {
143  return noise(x, y);
144 }
145 
146 ////////////////////////////////////////////////////////////////////
147 // Function: PerlinNoise2::noise
148 // Access: Published
149 // Description: Returns the noise function of the two inputs.
150 ////////////////////////////////////////////////////////////////////
151 INLINE float PerlinNoise2::
152 operator ()(const LVecBase2f &value) const {
153  return noise(value);
154 }
155 
156 ////////////////////////////////////////////////////////////////////
157 // Function: PerlinNoise2::noise
158 // Access: Published
159 // Description: Returns the noise function of the two inputs.
160 ////////////////////////////////////////////////////////////////////
161 INLINE double PerlinNoise2::
162 operator ()(const LVecBase2d &value) const {
163  return noise(value);
164 }
165 
166 ////////////////////////////////////////////////////////////////////
167 // Function: PerlinNoise2::grad
168 // Access: Private, Static
169 // Description: Returns the dot product of a random gradient vector
170 // (determined by the hash code) with the indicated
171 // offset vector.
172 ////////////////////////////////////////////////////////////////////
173 INLINE double PerlinNoise2::
174 grad(int hash, double x, double y) {
175  // Convert low 3 bits of hash code into 8 gradient directions.
176  switch (hash & 7) {
177  // Four corners.
178  case 0: return x + y;
179  case 1: return x - y;
180  case 2: return -x + y;
181  case 3: return -x - y;
182 
183  // Four edges. Here we scale by 1.707 to make all the vectors equal
184  // length, and to make their lengths consistent with PerlinNoise3.
185  case 4: return 1.707 * x;
186  case 5: return 1.707 * y;
187  case 6: return -1.707 * x;
188  case 7: return -1.707 * y;
189  }
190 
191  nassertr(false, 0);
192  return 0;
193 }
This is the base class for PerlinNoise2 and PerlinNoise3, different dimensions of Perlin noise implem...
Definition: perlinNoise.h:31
This is the base class for all two-component vectors and points.
Definition: lvecBase2.h:1241
void set_scale(double scale)
Changes the scale (frequency) of the noise.
Definition: perlinNoise2.I:82
double noise(double x, double y) const
Returns the noise function of the three inputs.
Definition: perlinNoise2.I:122
This class provides an implementation of Perlin noise for 2 variables.
Definition: perlinNoise2.h:28
This is the base class for all two-component vectors and points.
Definition: lvecBase2.h:105
void operator=(const PerlinNoise2 &copy)
Makes an exact copy of the existing PerlinNoise object, including its random seed.
Definition: perlinNoise2.I:70
static LMatrix3d scale_mat(const LVecBase2d &scale)
Returns a matrix that applies the indicated scale in each of the two axes.
Definition: lmatrix.h:8359
PerlinNoise2()
Randomizes the tables to make a unique noise function.
Definition: perlinNoise2.I:24
double operator()(double x, double y) const
Returns the noise function of the two inputs.
Definition: perlinNoise2.I:142