Panda3D
perlinNoise2.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file perlinNoise2.cxx
10  * @author drose
11  * @date 2005-10-05
12  */
13 
14 #include "perlinNoise2.h"
15 #include "cmath.h"
16 
17 /**
18  * Returns the noise function of the three inputs.
19  */
20 double PerlinNoise2::
21 noise(const LVecBase2d &value) const {
22  // Convert the vector to our local coordinate space.
23  LVecBase2d vec = _input_xform.xform_point(value);
24 
25  double x = vec[0];
26  double y = vec[1];
27 
28  // Find unit square that contains point.
29  double xf = cfloor(x);
30  double yf = cfloor(y);
31 
32  int X = ((int)xf) & _table_size_mask;
33  int Y = ((int)yf) & _table_size_mask;
34 
35  // Find relative x,y of point in square.
36  x -= xf;
37  y -= yf;
38 
39  // Compute fade curves for each of x,y.
40  double u = fade(x);
41  double v = fade(y);
42 
43  // Hash coordinates of the 4 square corners (A, B, A + 1, and B + 1)
44  int A = _index[X] + Y;
45  int B = _index[X + 1] + Y;
46 
47  // and add blended results from 4 corners of square.
48  double result =
49  lerp(v, lerp(u, grad(_index[A], x, y),
50  grad(_index[B], x - 1, y)),
51  lerp(u, grad(_index[A + 1], x, y - 1),
52  grad(_index[B + 1], x - 1, y - 1)));
53 
54  return result;
55 }
56 
57 /**
58  * Come up with a random rotation to apply to the input coordinates. This
59  * will reduce the problem of the singularities on the axes, by sending the
60  * axes in some crazy direction.
61  */
62 void PerlinNoise2::
63 init_unscaled_xform() {
64  double rot = _randomizer.random_real(360.0f);
65  _unscaled_xform = LMatrix3d::rotate_mat(rot);
66 
67  // And come up with a random translation too, just so the singularity at (0,
68  // 0) is also unpredicatable.
69  _unscaled_xform.set_row(2, LVecBase2d(_randomizer.random_real_unit(),
70  _randomizer.random_real_unit()));
71 }
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
double noise(double x, double y) const
Returns the noise function of the three inputs.
Definition: perlinNoise2.I:100