stackedPerlinNoise2.cxx
00001 // Filename: stackedPerlinNoise2.cxx
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 #include "stackedPerlinNoise2.h"
00016 
00017 ////////////////////////////////////////////////////////////////////
00018 //     Function: StackedPerlinNoise2::Constructor
00019 //       Access: Published
00020 //  Description: Creates num_levels nested PerlinNoise2 objects.  Each
00021 //               stacked Perlin object will have a scale of 1 /
00022 //               scale_factor times the previous object (so that it is
00023 //               higher-frequency, if scale_factor > 1), and an
00024 //               amplitude of amp_scale times the previous object (so
00025 //               that it is less important, if amp_scale < 1).
00026 ////////////////////////////////////////////////////////////////////
00027 StackedPerlinNoise2::
00028 StackedPerlinNoise2(double sx, double sy, int num_levels,
00029                     double scale_factor, double amp_scale,
00030                     int table_size, unsigned long seed) {
00031   _noises.reserve(num_levels);
00032   double amp = 1.0;
00033   for (int i = 0; i < num_levels; ++i) {
00034     PerlinNoise2 noise(sx, sy, table_size, seed);
00035     add_level(noise, amp);
00036 
00037     seed = noise.get_seed();
00038     amp *= amp_scale;
00039     sx /= scale_factor;
00040     sy /= scale_factor;
00041   }
00042 }
00043 
00044 ////////////////////////////////////////////////////////////////////
00045 //     Function: StackedPerlinNoise2::Copy Constructor
00046 //       Access: Published
00047 //  Description: Creates an exact duplicate of the existing
00048 //               StackedPerlinNoise2 object, including the random
00049 //               seed.
00050 ////////////////////////////////////////////////////////////////////
00051 StackedPerlinNoise2::
00052 StackedPerlinNoise2(const StackedPerlinNoise2 &copy) :
00053   _noises(copy._noises)
00054 {
00055 }
00056 
00057 ////////////////////////////////////////////////////////////////////
00058 //     Function: StackedPerlinNoise2::Copy Assignment Operator
00059 //       Access: Published
00060 //  Description: Creates an exact duplicate of the existing
00061 //               StackedPerlinNoise2 object, including the random
00062 //               seed.
00063 ////////////////////////////////////////////////////////////////////
00064 void StackedPerlinNoise2::
00065 operator = (const StackedPerlinNoise2 &copy) {
00066   _noises = copy._noises;
00067 }
00068 
00069 ////////////////////////////////////////////////////////////////////
00070 //     Function: StackedPerlinNoise2::add_level
00071 //       Access: Published
00072 //  Description: Adds an arbitrary PerlinNoise2 object, and an
00073 //               associated amplitude, to the stack.
00074 ////////////////////////////////////////////////////////////////////
00075 void StackedPerlinNoise2::
00076 add_level(const PerlinNoise2 &level, double amp) {
00077   _noises.push_back(Noise());
00078   Noise &n = _noises.back();
00079   n._noise = level;
00080   n._amp = amp;
00081 }
00082 
00083 ////////////////////////////////////////////////////////////////////
00084 //     Function: StackedPerlinNoise2::clear
00085 //       Access: Published
00086 //  Description: Removes all levels from the stack.  You must call
00087 //               add_level() again to restore them.
00088 ////////////////////////////////////////////////////////////////////
00089 void StackedPerlinNoise2::
00090 clear() {
00091   _noises.clear();
00092 }
00093 
00094 ////////////////////////////////////////////////////////////////////
00095 //     Function: StackedPerlinNoise2::noise
00096 //       Access: Published
00097 //  Description: Returns the noise function of the three inputs.
00098 ////////////////////////////////////////////////////////////////////
00099 double StackedPerlinNoise2::
00100 noise(const LVecBase2d &value) {
00101   double result = 0.0;
00102 
00103   Noises::iterator ni;
00104   for (ni = _noises.begin(); ni != _noises.end(); ++ni) {
00105     result += (*ni)._noise(value) * (*ni)._amp;
00106   }
00107 
00108   return result;
00109 }