Panda3D

perlinNoise2.I

00001 // Filename: perlinNoise2.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: PerlinNoise2::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 PerlinNoise2::
00024 PerlinNoise2() :
00025   PerlinNoise(256, 0)
00026 {
00027   init_unscaled_xform();
00028   _input_xform = _unscaled_xform;
00029 }
00030 
00031 ////////////////////////////////////////////////////////////////////
00032 //     Function: PerlinNoise2::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 PerlinNoise2::
00041 PerlinNoise2(double sx, double sy,
00042              int table_size, unsigned long seed) :
00043   PerlinNoise(table_size, seed)
00044 {
00045   init_unscaled_xform();
00046   set_scale(sx, sy);
00047 }
00048 
00049 ////////////////////////////////////////////////////////////////////
00050 //     Function: PerlinNoise2::Copy Constructor
00051 //       Access: Published
00052 //  Description: Makes an exact copy of the existing PerlinNoise
00053 //               object, including its random seed.
00054 ////////////////////////////////////////////////////////////////////
00055 INLINE PerlinNoise2::
00056 PerlinNoise2(const PerlinNoise2 &copy) :
00057   PerlinNoise(copy),
00058   _unscaled_xform(copy._unscaled_xform),
00059   _input_xform(copy._input_xform)
00060 {
00061 }
00062 
00063 ////////////////////////////////////////////////////////////////////
00064 //     Function: PerlinNoise2::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 PerlinNoise2::
00070 operator = (const PerlinNoise2 &copy) {
00071   PerlinNoise::operator = (copy);
00072   _unscaled_xform = copy._unscaled_xform;
00073   _input_xform = copy._input_xform;
00074 }
00075 
00076 ////////////////////////////////////////////////////////////////////
00077 //     Function: PerlinNoise2::set_scale
00078 //       Access: Published
00079 //  Description: Changes the scale (frequency) of the noise.
00080 ////////////////////////////////////////////////////////////////////
00081 INLINE void PerlinNoise2::
00082 set_scale(double scale) {
00083   set_scale(scale, scale);
00084 }
00085 
00086 ////////////////////////////////////////////////////////////////////
00087 //     Function: PerlinNoise2::set_scale
00088 //       Access: Published
00089 //  Description: Changes the scale (frequency) of the noise.
00090 ////////////////////////////////////////////////////////////////////
00091 INLINE void PerlinNoise2::
00092 set_scale(double x, double y) {
00093   set_scale(LVecBase2d(x, y));
00094 }
00095 
00096 ////////////////////////////////////////////////////////////////////
00097 //     Function: PerlinNoise2::set_scale
00098 //       Access: Published
00099 //  Description: Changes the scale (frequency) of the noise.
00100 ////////////////////////////////////////////////////////////////////
00101 INLINE void PerlinNoise2::
00102 set_scale(const LVecBase2f &value) {
00103   set_scale(value[0], value[1]);
00104 }
00105 
00106 ////////////////////////////////////////////////////////////////////
00107 //     Function: PerlinNoise2::set_scale
00108 //       Access: Published
00109 //  Description: Changes the scale (frequency) of the noise.
00110 ////////////////////////////////////////////////////////////////////
00111 INLINE void PerlinNoise2::
00112 set_scale(const LVecBase2d &value) {
00113   _input_xform = LMatrix3d::scale_mat(1.0f / value[0], 1.0f / value[1]) * _unscaled_xform;
00114 }
00115 
00116 ////////////////////////////////////////////////////////////////////
00117 //     Function: PerlinNoise2::noise
00118 //       Access: Published
00119 //  Description: Returns the noise function of the three inputs.
00120 ////////////////////////////////////////////////////////////////////
00121 INLINE double PerlinNoise2::
00122 noise(double x, double y) const {
00123   return noise(LVecBase2d(x, y));
00124 }
00125 
00126 ////////////////////////////////////////////////////////////////////
00127 //     Function: PerlinNoise2::noise
00128 //       Access: Published
00129 //  Description: Returns the noise function of the three inputs.
00130 ////////////////////////////////////////////////////////////////////
00131 INLINE float PerlinNoise2::
00132 noise(const LVecBase2f &value) const {
00133   return (float)noise(value[0], value[1]);
00134 }
00135 
00136 ////////////////////////////////////////////////////////////////////
00137 //     Function: PerlinNoise2::operator ()
00138 //       Access: Published
00139 //  Description: Returns the noise function of the two inputs.
00140 ////////////////////////////////////////////////////////////////////
00141 INLINE double PerlinNoise2::
00142 operator ()(double x, double y) const {
00143   return noise(x, y);
00144 }
00145 
00146 ////////////////////////////////////////////////////////////////////
00147 //     Function: PerlinNoise2::noise
00148 //       Access: Published
00149 //  Description: Returns the noise function of the two inputs.
00150 ////////////////////////////////////////////////////////////////////
00151 INLINE float PerlinNoise2::
00152 operator ()(const LVecBase2f &value) const {
00153   return noise(value);
00154 }
00155 
00156 ////////////////////////////////////////////////////////////////////
00157 //     Function: PerlinNoise2::noise
00158 //       Access: Published
00159 //  Description: Returns the noise function of the two inputs.
00160 ////////////////////////////////////////////////////////////////////
00161 INLINE double PerlinNoise2::
00162 operator ()(const LVecBase2d &value) const {
00163   return noise(value);
00164 }
00165 
00166 ////////////////////////////////////////////////////////////////////
00167 //     Function: PerlinNoise2::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 PerlinNoise2::
00174 grad(int hash, double x, double y) {
00175   // Convert low 3 bits of hash code into 8 gradient directions.
00176   switch (hash & 7) {
00177     // Four corners.
00178   case 0: return x + y;
00179   case 1: return x - y;
00180   case 2: return -x + y;
00181   case 3: return -x - y;
00182 
00183     // Four edges.  Here we scale by 1.707 to make all the vectors equal
00184     // length, and to make their lengths consistent with PerlinNoise3.
00185   case 4: return 1.707 * x;
00186   case 5: return 1.707 * y;
00187   case 6: return -1.707 * x;
00188   case 7: return -1.707 * y;
00189   }
00190 
00191   nassertr(false, 0);
00192   return 0;
00193 }
 All Classes Functions Variables Enumerations