Panda3D

perlinNoise.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 perlinNoise.cxx
10 * @author drose
11 * @date 2005-10-05
12 */
13
14 #include "perlinNoise.h"
15
16 /**
17 * Randomizes the tables to make a unique noise function.
18 *
19 * If seed is nonzero, it is used to define the tables; if it is zero a random
20 * seed is generated.
21 */
22 PerlinNoise::
23 PerlinNoise(int table_size, unsigned long seed) :
24 _table_size(table_size),
25 _table_size_mask(table_size - 1),
26 _randomizer(seed)
27 {
28 // It is necessary for _table_size to be a power of 2.
29 #ifndef NDEBUG
30 if (_table_size != 0) {
31 bool table_size_power_2 = ((_table_size ^ _table_size_mask) == (_table_size + _table_size_mask));
32 nassertd(table_size_power_2) {
33 _table_size = 0;
34 _table_size_mask = 0;
35 }
36 }
37 #endif // NDEBUG
38
39 // The _index table is just a randomly shuffled index table.
40 _index.reserve(_table_size * 2);
41 int i;
42 for (i = 0; i < _table_size; ++i) {
43 _index.push_back(i);
44 }
45 for (i = 0; i < _table_size; ++i) {
46 int j = _randomizer.random_int(_table_size);
47 nassertv(j >= 0 && j < _table_size);
48 int t = _index[i];
49 _index[i] = _index[j];
50 _index[j] = t;
51 }
52
53 // We double up _index so we don't need to perform modulo arithmetic.
54 for (i = 0; i < _table_size; ++i) {
55 _index.push_back(_index[i]);
56 }
57 }
58
59 /**
60 * Makes an exact copy of the existing PerlinNoise object, including its
61 * random seed.
62 */
63 PerlinNoise::
64 PerlinNoise(const PerlinNoise &copy) :
65 _table_size(copy._table_size),
66 _table_size_mask(copy._table_size_mask),
67 _randomizer(copy._randomizer),
68 _index(copy._index)
69 {
70 }
71
72 /**
73 * Makes an exact copy of the existing PerlinNoise object, including its
74 * random seed.
75 */
76 void PerlinNoise::
77 operator = (const PerlinNoise &copy) {
78 _table_size = copy._table_size;
79 _table_size_mask = copy._table_size_mask;
80 _randomizer = copy._randomizer;
81 _index = copy._index;
82 }
PerlinNoise
This is the base class for PerlinNoise2 and PerlinNoise3, different dimensions of Perlin noise implem...
Definition: perlinNoise.h:28
perlinNoise.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.