Panda3D
perlinNoise.cxx
1 // Filename: perlinNoise.cxx
2 // Created by: drose (05Oct05)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "perlinNoise.h"
16 
17 ////////////////////////////////////////////////////////////////////
18 // Function: PerlinNoise::Constructor
19 // Access: Protected
20 // Description: Randomizes the tables to make a unique noise
21 // function.
22 //
23 // If seed is nonzero, it is used to define the tables;
24 // if it is zero a random seed is generated.
25 ////////////////////////////////////////////////////////////////////
26 PerlinNoise::
27 PerlinNoise(int table_size, unsigned long seed) :
28  _table_size(table_size),
29  _table_size_mask(table_size - 1),
30  _randomizer(seed)
31 {
32  // It is necessary for _table_size to be a power of 2.
33 #ifndef NDEBUG
34  if (_table_size != 0) {
35  bool table_size_power_2 = ((_table_size ^ _table_size_mask) == (_table_size + _table_size_mask));
36  nassertd(table_size_power_2) {
37  _table_size = 0;
38  _table_size_mask = 0;
39  }
40  }
41 #endif // NDEBUG
42 
43  // The _index table is just a randomly shuffled index
44  // table.
45  _index.reserve(_table_size * 2);
46  int i;
47  for (i = 0; i < _table_size; ++i) {
48  _index.push_back(i);
49  }
50  for (i = 0; i < _table_size; ++i) {
51  int j = _randomizer.random_int(_table_size);
52  nassertv(j >= 0 && j < _table_size);
53  int t = _index[i];
54  _index[i] = _index[j];
55  _index[j] = t;
56  }
57 
58  // We double up _index so we don't need to perform modulo
59  // arithmetic.
60  for (i = 0; i < _table_size; ++i) {
61  _index.push_back(_index[i]);
62  }
63 }
64 
65 ////////////////////////////////////////////////////////////////////
66 // Function: PerlinNoise::Copy Constructor
67 // Access: Protected
68 // Description: Makes an exact copy of the existing PerlinNoise
69 // object, including its random seed.
70 ////////////////////////////////////////////////////////////////////
71 PerlinNoise::
72 PerlinNoise(const PerlinNoise &copy) :
73  _table_size(copy._table_size),
74  _table_size_mask(copy._table_size_mask),
75  _randomizer(copy._randomizer),
76  _index(copy._index)
77 {
78 }
79 
80 ////////////////////////////////////////////////////////////////////
81 // Function: PerlinNoise::Copy Assignment Operator
82 // Access: Protected
83 // Description: Makes an exact copy of the existing PerlinNoise
84 // object, including its random seed.
85 ////////////////////////////////////////////////////////////////////
86 void PerlinNoise::
87 operator = (const PerlinNoise &copy) {
88  _table_size = copy._table_size;
89  _table_size_mask = copy._table_size_mask;
90  _randomizer = copy._randomizer;
91  _index = copy._index;
92 }
This is the base class for PerlinNoise2 and PerlinNoise3, different dimensions of Perlin noise implem...
Definition: perlinNoise.h:31