00001 // Filename: perlinNoise3.I 00002 // Created by: drose (05Oct05) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 00016 //////////////////////////////////////////////////////////////////// 00017 // Function: PerlinNoise3::Default Constructor 00018 // Access: Published 00019 // Description: Randomizes the tables to make a unique noise 00020 // function. Uses a default scale (noise frequency), 00021 // table size, and seed. 00022 //////////////////////////////////////////////////////////////////// 00023 INLINE PerlinNoise3:: 00024 PerlinNoise3() : 00025 PerlinNoise(256, 0) 00026 { 00027 init_unscaled_xform(); 00028 _input_xform = _unscaled_xform; 00029 } 00030 00031 //////////////////////////////////////////////////////////////////// 00032 // Function: PerlinNoise3::Constructor 00033 // Access: Published 00034 // Description: Randomizes the tables to make a unique noise 00035 // function. 00036 // 00037 // If seed is nonzero, it is used to define the tables; 00038 // if it is zero a random seed is generated. 00039 //////////////////////////////////////////////////////////////////// 00040 INLINE PerlinNoise3:: 00041 PerlinNoise3(double sx, double sy, double sz, 00042 int table_size, unsigned long seed) : 00043 PerlinNoise(table_size, seed) 00044 { 00045 init_unscaled_xform(); 00046 set_scale(sx, sy, sz); 00047 } 00048 00049 //////////////////////////////////////////////////////////////////// 00050 // Function: PerlinNoise3::Copy Constructor 00051 // Access: Published 00052 // Description: Makes an exact copy of the existing PerlinNoise 00053 // object, including its random seed. 00054 //////////////////////////////////////////////////////////////////// 00055 INLINE PerlinNoise3:: 00056 PerlinNoise3(const PerlinNoise3 ©) : 00057 PerlinNoise(copy), 00058 _unscaled_xform(copy._unscaled_xform), 00059 _input_xform(copy._input_xform) 00060 { 00061 } 00062 00063 //////////////////////////////////////////////////////////////////// 00064 // Function: PerlinNoise3::Copy Assignment Operator 00065 // Access: Published 00066 // Description: Makes an exact copy of the existing PerlinNoise 00067 // object, including its random seed. 00068 //////////////////////////////////////////////////////////////////// 00069 INLINE void PerlinNoise3:: 00070 operator = (const PerlinNoise3 ©) { 00071 PerlinNoise::operator = (copy); 00072 _unscaled_xform = copy._unscaled_xform; 00073 _input_xform = copy._input_xform; 00074 } 00075 00076 //////////////////////////////////////////////////////////////////// 00077 // Function: PerlinNoise3::set_scale 00078 // Access: Published 00079 // Description: Changes the scale (frequency) of the noise. 00080 //////////////////////////////////////////////////////////////////// 00081 INLINE void PerlinNoise3:: 00082 set_scale(double scale) { 00083 set_scale(scale, scale, scale); 00084 } 00085 00086 //////////////////////////////////////////////////////////////////// 00087 // Function: PerlinNoise3::set_scale 00088 // Access: Published 00089 // Description: Changes the scale (frequency) of the noise. 00090 //////////////////////////////////////////////////////////////////// 00091 INLINE void PerlinNoise3:: 00092 set_scale(double x, double y, double z) { 00093 set_scale(LVecBase3d(x, y, z)); 00094 } 00095 00096 //////////////////////////////////////////////////////////////////// 00097 // Function: PerlinNoise3::set_scale 00098 // Access: Published 00099 // Description: Changes the scale (frequency) of the noise. 00100 //////////////////////////////////////////////////////////////////// 00101 INLINE void PerlinNoise3:: 00102 set_scale(const LVecBase3f &value) { 00103 set_scale(value[0], value[1], value[2]); 00104 } 00105 00106 //////////////////////////////////////////////////////////////////// 00107 // Function: PerlinNoise3::set_scale 00108 // Access: Published 00109 // Description: Changes the scale (frequency) of the noise. 00110 //////////////////////////////////////////////////////////////////// 00111 INLINE void PerlinNoise3:: 00112 set_scale(const LVecBase3d &value) { 00113 _input_xform = LMatrix4d::scale_mat(1.0f / value[0], 1.0f / value[1], 1.0f / value[2]) * _unscaled_xform; 00114 } 00115 00116 //////////////////////////////////////////////////////////////////// 00117 // Function: PerlinNoise3::noise 00118 // Access: Published 00119 // Description: Returns the noise function of the three inputs. 00120 //////////////////////////////////////////////////////////////////// 00121 INLINE double PerlinNoise3:: 00122 noise(double x, double y, double z) const { 00123 return noise(LVecBase3d(x, y, z)); 00124 } 00125 00126 //////////////////////////////////////////////////////////////////// 00127 // Function: PerlinNoise3::noise 00128 // Access: Published 00129 // Description: Returns the noise function of the three inputs. 00130 //////////////////////////////////////////////////////////////////// 00131 INLINE float PerlinNoise3:: 00132 noise(const LVecBase3f &value) const { 00133 return (float)noise(value[0], value[1], value[2]); 00134 } 00135 00136 //////////////////////////////////////////////////////////////////// 00137 // Function: PerlinNoise3::operator () 00138 // Access: Published 00139 // Description: Returns the noise function of the three inputs. 00140 //////////////////////////////////////////////////////////////////// 00141 INLINE double PerlinNoise3:: 00142 operator ()(double x, double y, double z) const { 00143 return noise(x, y, z); 00144 } 00145 00146 //////////////////////////////////////////////////////////////////// 00147 // Function: PerlinNoise3::noise 00148 // Access: Published 00149 // Description: Returns the noise function of the three inputs. 00150 //////////////////////////////////////////////////////////////////// 00151 INLINE float PerlinNoise3:: 00152 operator ()(const LVecBase3f &value) const { 00153 return noise(value); 00154 } 00155 00156 //////////////////////////////////////////////////////////////////// 00157 // Function: PerlinNoise3::noise 00158 // Access: Published 00159 // Description: Returns the noise function of the three inputs. 00160 //////////////////////////////////////////////////////////////////// 00161 INLINE double PerlinNoise3:: 00162 operator ()(const LVecBase3d &value) const { 00163 return noise(value); 00164 } 00165 00166 //////////////////////////////////////////////////////////////////// 00167 // Function: PerlinNoise3::grad 00168 // Access: Private, Static 00169 // Description: Returns the dot product of a random gradient vector 00170 // (determined by the hash code) with the indicated 00171 // offset vector. 00172 //////////////////////////////////////////////////////////////////// 00173 INLINE double PerlinNoise3:: 00174 grad(int hash, double x, double y, double z) { 00175 // Convert low 4 bits of hash code into 12 gradient directions. 00176 /* 00177 This is Perlin's reference code, but the switch statement below is 00178 slightly faster and produces exactly the same results. 00179 int h = hash & 15; 00180 double u = (h < 8) ? x : y; 00181 double v = (h < 4) ? y : ((h == 12 || h == 14) ? x : z); 00182 return ((h & 1) ? -u : u) + ((h & 2) ? -v : v); 00183 */ 00184 00185 switch (hash & 15) { 00186 case 0: return x + y; 00187 case 1: return -x + y; 00188 case 2: return x - y; 00189 case 3: return -x - y; 00190 00191 case 4: return x + z; 00192 case 5: return -x + z; 00193 case 6: return x - z; 00194 case 7: return -x - z; 00195 00196 case 8: return y + z; 00197 case 9: return -y + z; 00198 case 10: return y - z; 00199 case 11: return -y - z; 00200 00201 case 12: return x + y; 00202 case 13: return -y + z; 00203 case 14: return -x + y; 00204 case 15: return -y - z; 00205 } 00206 00207 nassertr(false, 0); 00208 return 0; 00209 }