Panda3D

stackedPerlinNoise3.cxx

00001 // Filename: stackedPerlinNoise3.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 "stackedPerlinNoise3.h"
00016 
00017 ////////////////////////////////////////////////////////////////////
00018 //     Function: StackedPerlinNoise3::Constructor
00019 //       Access: Published
00020 //  Description: Creates num_levels nested PerlinNoise3 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 StackedPerlinNoise3::
00028 StackedPerlinNoise3(double sx, double sy, double sz, 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     PerlinNoise3 noise(sx, sy, sz, 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     sz /= scale_factor;
00042   }
00043 }
00044 
00045 ////////////////////////////////////////////////////////////////////
00046 //     Function: StackedPerlinNoise3::Copy Constructor
00047 //       Access: Published
00048 //  Description: Creates an exact duplicate of the existing
00049 //               StackedPerlinNoise3 object, including the random
00050 //               seed.
00051 ////////////////////////////////////////////////////////////////////
00052 StackedPerlinNoise3::
00053 StackedPerlinNoise3(const StackedPerlinNoise3 &copy) :
00054   _noises(copy._noises)
00055 {
00056 }
00057 
00058 ////////////////////////////////////////////////////////////////////
00059 //     Function: StackedPerlinNoise3::Copy Assignment Operator
00060 //       Access: Published
00061 //  Description: Creates an exact duplicate of the existing
00062 //               StackedPerlinNoise3 object, including the random
00063 //               seed.
00064 ////////////////////////////////////////////////////////////////////
00065 void StackedPerlinNoise3::
00066 operator = (const StackedPerlinNoise3 &copy) {
00067   _noises = copy._noises;
00068 }
00069 
00070 ////////////////////////////////////////////////////////////////////
00071 //     Function: StackedPerlinNoise3::add_level
00072 //       Access: Published
00073 //  Description: Adds an arbitrary PerlinNoise3 object, and an
00074 //               associated amplitude, to the stack.
00075 ////////////////////////////////////////////////////////////////////
00076 void StackedPerlinNoise3::
00077 add_level(const PerlinNoise3 &level, double amp) {
00078   _noises.push_back(Noise());
00079   Noise &n = _noises.back();
00080   n._noise = level;
00081   n._amp = amp;
00082 }
00083 
00084 ////////////////////////////////////////////////////////////////////
00085 //     Function: StackedPerlinNoise3::clear
00086 //       Access: Published
00087 //  Description: Removes all levels from the stack.  You must call
00088 //               add_level() again to restore them.
00089 ////////////////////////////////////////////////////////////////////
00090 void StackedPerlinNoise3::
00091 clear() {
00092   _noises.clear();
00093 }
00094 
00095 ////////////////////////////////////////////////////////////////////
00096 //     Function: StackedPerlinNoise3::noise
00097 //       Access: Published
00098 //  Description: Returns the noise function of the three inputs.
00099 ////////////////////////////////////////////////////////////////////
00100 double StackedPerlinNoise3::
00101 noise(const LVecBase3d &value) {
00102   double result = 0.0;
00103 
00104   Noises::iterator ni;
00105   for (ni = _noises.begin(); ni != _noises.end(); ++ni) {
00106     result += (*ni)._noise(value) * (*ni)._amp;
00107   }
00108 
00109   return result;
00110 }
 All Classes Functions Variables Enumerations