Browse Source

add set_scale()

David Rose 20 năm trước cách đây
mục cha
commit
c0c9347983

+ 66 - 9
panda/src/mathutil/perlinNoise2.I

@@ -20,19 +20,34 @@
 ////////////////////////////////////////////////////////////////////
 //     Function: PerlinNoise2::Default Constructor
 //       Access: Published
-//  Description: The default constructor makes an invalid PerlinNoise2
-//               object.  You must at least pass in a scale of each
-//               dimension.  
-//
-//               This constructor exists only so you can create a
-//               temporary placeholder PerlinNoise2 object, and later
-//               fill it in with the assignment operator.
+//  Description: Randomizes the tables to make a unique noise
+//               function.  Uses a default scale (noise frequency),
+//               table size, and seed.
 ////////////////////////////////////////////////////////////////////
 INLINE PerlinNoise2::
 PerlinNoise2() :
-  PerlinNoise(0, 1)
+  PerlinNoise(256, 0)
+{
+  init_unscaled_xform();
+  _input_xform = _unscaled_xform;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: PerlinNoise2::Constructor
+//       Access: Published
+//  Description: Randomizes the tables to make a unique noise
+//               function.
+//
+//               If seed is nonzero, it is used to define the tables;
+//               if it is zero a random seed is generated.
+////////////////////////////////////////////////////////////////////
+INLINE PerlinNoise2::
+PerlinNoise2(double sx, double sy,
+	     int table_size, unsigned long seed) :
+  PerlinNoise(table_size, seed)
 {
-  _input_xform.fill(0.0f);
+  init_unscaled_xform();
+  set_scale(sx, sy);
 }
 
 ////////////////////////////////////////////////////////////////////
@@ -44,6 +59,7 @@ PerlinNoise2() :
 INLINE PerlinNoise2::
 PerlinNoise2(const PerlinNoise2 &copy) :
   PerlinNoise(copy),
+  _unscaled_xform(copy._unscaled_xform),
   _input_xform(copy._input_xform)
 {
 }
@@ -57,9 +73,50 @@ PerlinNoise2(const PerlinNoise2 &copy) :
 INLINE void PerlinNoise2::
 operator = (const PerlinNoise2 &copy) {
   PerlinNoise::operator = (copy);
+  _unscaled_xform = copy._unscaled_xform;
   _input_xform = copy._input_xform;
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: PerlinNoise2::set_scale
+//       Access: Published
+//  Description: Changes the scale (frequency) of the noise.
+////////////////////////////////////////////////////////////////////
+INLINE void PerlinNoise2::
+set_scale(double scale) {
+  set_scale(scale, scale);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: PerlinNoise2::set_scale
+//       Access: Published
+//  Description: Changes the scale (frequency) of the noise.
+////////////////////////////////////////////////////////////////////
+INLINE void PerlinNoise2::
+set_scale(double x, double y) {
+  set_scale(LVecBase2d(x, y));
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: PerlinNoise2::set_scale
+//       Access: Published
+//  Description: Changes the scale (frequency) of the noise.
+////////////////////////////////////////////////////////////////////
+INLINE void PerlinNoise2::
+set_scale(const LVecBase2f &value) {
+  set_scale(value[0], value[1]);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: PerlinNoise2::set_scale
+//       Access: Published
+//  Description: Changes the scale (frequency) of the noise.
+////////////////////////////////////////////////////////////////////
+INLINE void PerlinNoise2::
+set_scale(const LVecBase2d &value) {
+  _input_xform = LMatrix3d::scale_mat(1.0f / value[0], 1.0f / value[1]) * _unscaled_xform;
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: PerlinNoise2::noise
 //       Access: Published

+ 19 - 29
panda/src/mathutil/perlinNoise2.cxx

@@ -19,35 +19,6 @@
 #include "perlinNoise2.h"
 #include "cmath.h"
 
-////////////////////////////////////////////////////////////////////
-//     Function: PerlinNoise2::Constructor
-//       Access: Published
-//  Description: Randomizes the tables to make a unique noise
-//               function.
-//
-//               If seed is nonzero, it is used to define the tables;
-//               if it is zero a random seed is generated.
-////////////////////////////////////////////////////////////////////
-PerlinNoise2::
-PerlinNoise2(double sx, double sy,
-	     int table_size, unsigned long seed) :
-  PerlinNoise(table_size, seed)
-{
-  // Come up with a random rotation to apply to the input coordinates.
-  // This will reduce the problem of the singularities on the axes, by
-  // sending the axes in some crazy direction.
-  double rot = random_real(360.0f);
-  _input_xform = LMatrix3d::rotate_mat(rot);
-
-  // And come up with a random translation too, just so the
-  // singularity at (0, 0) is also unpredicatable.
-  _input_xform.set_row(2, LVecBase2d(random_real_unit(),
-				     random_real_unit()));
-
-  // Finally, apply the user's input scale.
-  _input_xform = LMatrix3d::scale_mat(1.0f / sx, 1.0f / sy) * _input_xform;
-}
-
 ////////////////////////////////////////////////////////////////////
 //     Function: PerlinNoise2::noise
 //       Access: Published
@@ -89,3 +60,22 @@ noise(const LVecBase2d &value) {
 
   return result;
 }
+
+////////////////////////////////////////////////////////////////////
+//     Function: PerlinNoise2::init_unscaled_xform
+//       Access: Private
+//  Description: Come up with a random rotation to apply to the input
+//               coordinates. This will reduce the problem of the
+//               singularities on the axes, by sending the axes in
+//               some crazy direction.
+////////////////////////////////////////////////////////////////////
+void PerlinNoise2::
+init_unscaled_xform() {
+  double rot = random_real(360.0f);
+  _unscaled_xform = LMatrix3d::rotate_mat(rot);
+
+  // And come up with a random translation too, just so the
+  // singularity at (0, 0) is also unpredicatable.
+  _unscaled_xform.set_row(2, LVecBase2d(random_real_unit(),
+					random_real_unit()));
+}

+ 10 - 3
panda/src/mathutil/perlinNoise2.h

@@ -32,12 +32,17 @@
 class EXPCL_PANDA PerlinNoise2 : public PerlinNoise {
 PUBLISHED:
   INLINE PerlinNoise2();
-  PerlinNoise2(double sx, double sy,
-	       int table_size = 256,
-	       unsigned long seed = 0);
+  INLINE PerlinNoise2(double sx, double sy,
+		      int table_size = 256,
+		      unsigned long seed = 0);
   INLINE PerlinNoise2(const PerlinNoise2 &copy);
   INLINE void operator = (const PerlinNoise2 &copy);
 
+  INLINE void set_scale(double scale);
+  INLINE void set_scale(double sx, double sy);
+  INLINE void set_scale(const LVecBase2f &scale);
+  void set_scale(const LVecBase2d &scale);
+
   INLINE double noise(double x, double y);
   INLINE float noise(const LVecBase2f &value);
   double noise(const LVecBase2d &value);
@@ -47,9 +52,11 @@ PUBLISHED:
   INLINE double operator ()(const LVecBase2d &value);
   
 private:
+  void init_unscaled_xform();
   INLINE static double grad(int hash, double x, double y);
 
 private:
+  LMatrix3d _unscaled_xform;
   LMatrix3d _input_xform;
 };
 

+ 66 - 9
panda/src/mathutil/perlinNoise3.I

@@ -20,19 +20,34 @@
 ////////////////////////////////////////////////////////////////////
 //     Function: PerlinNoise3::Default Constructor
 //       Access: Published
-//  Description: The default constructor makes an invalid PerlinNoise3
-//               object.  You must at least pass in a scale of each
-//               dimension.  
-//
-//               This constructor exists only so you can create a
-//               temporary placeholder PerlinNoise3 object, and later
-//               fill it in with the assignment operator.
+//  Description: Randomizes the tables to make a unique noise
+//               function.  Uses a default scale (noise frequency),
+//               table size, and seed.
 ////////////////////////////////////////////////////////////////////
 INLINE PerlinNoise3::
 PerlinNoise3() :
-  PerlinNoise(0, 1)
+  PerlinNoise(256, 0)
+{
+  init_unscaled_xform();
+  _input_xform = _unscaled_xform;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: PerlinNoise3::Constructor
+//       Access: Published
+//  Description: Randomizes the tables to make a unique noise
+//               function.
+//
+//               If seed is nonzero, it is used to define the tables;
+//               if it is zero a random seed is generated.
+////////////////////////////////////////////////////////////////////
+INLINE PerlinNoise3::
+PerlinNoise3(double sx, double sy, double sz,
+	     int table_size, unsigned long seed) :
+  PerlinNoise(table_size, seed)
 {
-  _input_xform.fill(0.0f);
+  init_unscaled_xform();
+  set_scale(sx, sy, sz);
 }
 
 ////////////////////////////////////////////////////////////////////
@@ -44,6 +59,7 @@ PerlinNoise3() :
 INLINE PerlinNoise3::
 PerlinNoise3(const PerlinNoise3 &copy) :
   PerlinNoise(copy),
+  _unscaled_xform(copy._unscaled_xform),
   _input_xform(copy._input_xform)
 {
 }
@@ -57,9 +73,50 @@ PerlinNoise3(const PerlinNoise3 &copy) :
 INLINE void PerlinNoise3::
 operator = (const PerlinNoise3 &copy) {
   PerlinNoise::operator = (copy);
+  _unscaled_xform = copy._unscaled_xform;
   _input_xform = copy._input_xform;
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: PerlinNoise3::set_scale
+//       Access: Published
+//  Description: Changes the scale (frequency) of the noise.
+////////////////////////////////////////////////////////////////////
+INLINE void PerlinNoise3::
+set_scale(double scale) {
+  set_scale(scale, scale, scale);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: PerlinNoise3::set_scale
+//       Access: Published
+//  Description: Changes the scale (frequency) of the noise.
+////////////////////////////////////////////////////////////////////
+INLINE void PerlinNoise3::
+set_scale(double x, double y, double z) {
+  set_scale(LVecBase3d(x, y, z));
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: PerlinNoise3::set_scale
+//       Access: Published
+//  Description: Changes the scale (frequency) of the noise.
+////////////////////////////////////////////////////////////////////
+INLINE void PerlinNoise3::
+set_scale(const LVecBase3f &value) {
+  set_scale(value[0], value[1], value[2]);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: PerlinNoise3::set_scale
+//       Access: Published
+//  Description: Changes the scale (frequency) of the noise.
+////////////////////////////////////////////////////////////////////
+INLINE void PerlinNoise3::
+set_scale(const LVecBase3d &value) {
+  _input_xform = LMatrix4d::scale_mat(1.0f / value[0], 1.0f / value[1], 1.0f / value[2]) * _unscaled_xform;
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: PerlinNoise3::noise
 //       Access: Published

+ 24 - 34
panda/src/mathutil/perlinNoise3.cxx

@@ -19,40 +19,6 @@
 #include "perlinNoise3.h"
 #include "cmath.h"
 
-////////////////////////////////////////////////////////////////////
-//     Function: PerlinNoise3::Constructor
-//       Access: Published
-//  Description: Randomizes the tables to make a unique noise
-//               function.
-//
-//               If seed is nonzero, it is used to define the tables;
-//               if it is zero a random seed is generated.
-////////////////////////////////////////////////////////////////////
-PerlinNoise3::
-PerlinNoise3(double sx, double sy, double sz,
-	     int table_size, unsigned long seed) :
-  PerlinNoise(table_size, seed)
-{
-  // Come up with a random rotation to apply to the input coordinates.
-  // This will reduce the problem of the singularities on the axes, by
-  // sending the axes in some crazy direction.
-  LRotationd rot(random_real_unit(),
-		 random_real_unit(),
-		 random_real_unit(),
-		 random_real_unit());
-  rot.normalize();
-  rot.extract_to_matrix(_input_xform);
-
-  // And come up with a random translation too, just so the
-  // singularity at (0, 0, 0) is also unpredicatable.
-  _input_xform.set_row(3, LVecBase3d(random_real_unit(),
-				     random_real_unit(),
-				     random_real_unit()));
-
-  // Finally, apply the user's input scale.
-  _input_xform = LMatrix4d::scale_mat(1.0f / sx, 1.0f / sy, 1.0f / sz) * _input_xform;
-}
-
 ////////////////////////////////////////////////////////////////////
 //     Function: PerlinNoise3::noise
 //       Access: Published
@@ -108,3 +74,27 @@ noise(const LVecBase3d &value) {
 
   return result;
 }
+
+////////////////////////////////////////////////////////////////////
+//     Function: PerlinNoise3::init_unscaled_xform
+//       Access: Private
+//  Description: Come up with a random rotation to apply to the input
+//               coordinates. This will reduce the problem of the
+//               singularities on the axes, by sending the axes in
+//               some crazy direction.
+////////////////////////////////////////////////////////////////////
+void PerlinNoise3::
+init_unscaled_xform() {
+  LRotationd rot(random_real_unit(),
+		 random_real_unit(),
+		 random_real_unit(),
+		 random_real_unit());
+  rot.normalize();
+  rot.extract_to_matrix(_unscaled_xform);
+
+  // And come up with a random translation too, just so the
+  // singularity at (0, 0, 0) is also unpredicatable.
+  _unscaled_xform.set_row(3, LVecBase3d(random_real_unit(),
+					random_real_unit(),
+					random_real_unit()));
+}

+ 9 - 3
panda/src/mathutil/perlinNoise3.h

@@ -32,12 +32,16 @@
 class EXPCL_PANDA PerlinNoise3 : public PerlinNoise {
 PUBLISHED:
   INLINE PerlinNoise3();
-  PerlinNoise3(double sx, double sy, double sz,
-	       int table_size = 256,
-	       unsigned long seed = 0);
+  INLINE PerlinNoise3(double sx, double sy, double sz,
+		      int table_size = 256, unsigned long seed = 0);
   INLINE PerlinNoise3(const PerlinNoise3 &copy);
   INLINE void operator = (const PerlinNoise3 &copy);
 
+  INLINE void set_scale(double scale);
+  INLINE void set_scale(double sx, double sy, double sz);
+  INLINE void set_scale(const LVecBase3f &scale);
+  INLINE void set_scale(const LVecBase3d &scale);
+
   INLINE double noise(double x, double y, double z);
   INLINE float noise(const LVecBase3f &value);
   double noise(const LVecBase3d &value);
@@ -47,9 +51,11 @@ PUBLISHED:
   INLINE double operator ()(const LVecBase3d &value);
   
 private:
+  void init_unscaled_xform();
   INLINE static double grad(int hash, double x, double y, double z);
 
 private:
+  LMatrix4d _unscaled_xform;
   LMatrix4d _input_xform;
 };