Panda3D
 All Classes Functions Variables Enumerations
perlinNoise2.cxx
00001 // Filename: perlinNoise2.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 "perlinNoise2.h"
00016 #include "cmath.h"
00017 
00018 ////////////////////////////////////////////////////////////////////
00019 //     Function: PerlinNoise2::noise
00020 //       Access: Published
00021 //  Description: Returns the noise function of the three inputs.
00022 ////////////////////////////////////////////////////////////////////
00023 double PerlinNoise2::
00024 noise(const LVecBase2d &value) const {
00025   // Convert the vector to our local coordinate space.
00026   LVecBase2d vec = _input_xform.xform_point(value);
00027 
00028   double x = vec[0];
00029   double y = vec[1];
00030 
00031   // Find unit square that contains point.
00032   double xf = cfloor(x);
00033   double yf = cfloor(y);
00034 
00035   int X = ((int)xf) & _table_size_mask;
00036   int Y = ((int)yf) & _table_size_mask;
00037 
00038   // Find relative x,y of point in square.
00039   x -= xf;
00040   y -= yf;        
00041 
00042   // Compute fade curves for each of x,y.
00043   double u = fade(x);
00044   double v = fade(y);
00045 
00046   // Hash coordinates of the 4 square corners (A, B, A + 1, and B + 1)
00047   int A = _index[X] + Y;
00048   int B = _index[X + 1] + Y;
00049   
00050   // and add blended results from 4 corners of square.
00051   double result =
00052     lerp(v, lerp(u, grad(_index[A], x, y), 
00053                  grad(_index[B], x - 1, y)), 
00054          lerp(u, grad(_index[A + 1], x, y - 1), 
00055               grad(_index[B + 1], x - 1, y - 1)));
00056 
00057   return result;
00058 }
00059 
00060 ////////////////////////////////////////////////////////////////////
00061 //     Function: PerlinNoise2::init_unscaled_xform
00062 //       Access: Private
00063 //  Description: Come up with a random rotation to apply to the input
00064 //               coordinates. This will reduce the problem of the
00065 //               singularities on the axes, by sending the axes in
00066 //               some crazy direction.
00067 ////////////////////////////////////////////////////////////////////
00068 void PerlinNoise2::
00069 init_unscaled_xform() {
00070   double rot = _randomizer.random_real(360.0f);
00071   _unscaled_xform = LMatrix3d::rotate_mat(rot);
00072 
00073   // And come up with a random translation too, just so the
00074   // singularity at (0, 0) is also unpredicatable.
00075   _unscaled_xform.set_row(2, LVecBase2d(_randomizer.random_real_unit(),
00076                                         _randomizer.random_real_unit()));
00077 }
 All Classes Functions Variables Enumerations