Panda3D
perlinNoise3.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 perlinNoise3.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 PerlinNoise3::
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 PerlinNoise3::
33 PerlinNoise3(double sx, double sy, double sz,
34  int table_size, unsigned long seed) :
35  PerlinNoise(table_size, seed)
36 {
37  init_unscaled_xform();
38  set_scale(sx, sy, sz);
39 }
40 
41 /**
42  * Makes an exact copy of the existing PerlinNoise object, including its
43  * random seed.
44  */
45 INLINE PerlinNoise3::
46 PerlinNoise3(const PerlinNoise3 &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 PerlinNoise3::
58 operator = (const PerlinNoise3 &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 PerlinNoise3::
68 set_scale(double scale) {
69  set_scale(scale, scale, scale);
70 }
71 
72 /**
73  * Changes the scale (frequency) of the noise.
74  */
75 INLINE void PerlinNoise3::
76 set_scale(double x, double y, double z) {
77  set_scale(LVecBase3d(x, y, z));
78 }
79 
80 /**
81  * Changes the scale (frequency) of the noise.
82  */
83 INLINE void PerlinNoise3::
84 set_scale(const LVecBase3f &value) {
85  set_scale(value[0], value[1], value[2]);
86 }
87 
88 /**
89  * Changes the scale (frequency) of the noise.
90  */
91 INLINE void PerlinNoise3::
92 set_scale(const LVecBase3d &value) {
93  _input_xform = LMatrix4d::scale_mat(1.0f / value[0], 1.0f / value[1], 1.0f / value[2]) * _unscaled_xform;
94 }
95 
96 /**
97  * Returns the noise function of the three inputs.
98  */
99 INLINE double PerlinNoise3::
100 noise(double x, double y, double z) const {
101  return noise(LVecBase3d(x, y, z));
102 }
103 
104 /**
105  * Returns the noise function of the three inputs.
106  */
107 INLINE float PerlinNoise3::
108 noise(const LVecBase3f &value) const {
109  return (float)noise(value[0], value[1], value[2]);
110 }
111 
112 /**
113  * Returns the noise function of the three inputs.
114  */
115 INLINE double PerlinNoise3::
116 operator ()(double x, double y, double z) const {
117  return noise(x, y, z);
118 }
119 
120 /**
121  * Returns the noise function of the three inputs.
122  */
123 INLINE float PerlinNoise3::
124 operator ()(const LVecBase3f &value) const {
125  return noise(value);
126 }
127 
128 /**
129  * Returns the noise function of the three inputs.
130  */
131 INLINE double PerlinNoise3::
132 operator ()(const LVecBase3d &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 PerlinNoise3::
141 grad(int hash, double x, double y, double z) {
142  // Convert low 4 bits of hash code into 12 gradient directions.
143  /*
144  This is Perlin's reference code, but the switch statement below is
145  slightly faster and produces exactly the same results.
146  int h = hash & 15;
147  double u = (h < 8) ? x : y;
148  double v = (h < 4) ? y : ((h == 12 || h == 14) ? x : z);
149  return ((h & 1) ? -u : u) + ((h & 2) ? -v : v);
150  */
151 
152  switch (hash & 15) {
153  case 0: return x + y;
154  case 1: return -x + y;
155  case 2: return x - y;
156  case 3: return -x - y;
157 
158  case 4: return x + z;
159  case 5: return -x + z;
160  case 6: return x - z;
161  case 7: return -x - z;
162 
163  case 8: return y + z;
164  case 9: return -y + z;
165  case 10: return y - z;
166  case 11: return -y - z;
167 
168  case 12: return x + y;
169  case 13: return -y + z;
170  case 14: return -x + y;
171  case 15: return -y - z;
172  }
173 
174  nassertr(false, 0);
175  return 0;
176 }
This is the base class for PerlinNoise2 and PerlinNoise3, different dimensions of Perlin noise implem...
Definition: perlinNoise.h:28
void operator=(const PerlinNoise3 &copy)
Makes an exact copy of the existing PerlinNoise object, including its random seed.
Definition: perlinNoise3.I:58
PerlinNoise3()
Randomizes the tables to make a unique noise function.
Definition: perlinNoise3.I:19
This class provides an implementation of Perlin noise for 3 variables.
Definition: perlinNoise3.h:25
void set_scale(double scale)
Changes the scale (frequency) of the noise.
Definition: perlinNoise3.I:68
double operator()(double x, double y, double z) const
Returns the noise function of the three inputs.
Definition: perlinNoise3.I:116
double noise(double x, double y, double z) const
Returns the noise function of the three inputs.
Definition: perlinNoise3.I:100