Skip to content

Commit 70ae85b

Browse files
committed
Improve Perlin class initialization
The prior form defined a private `init()` method to initialize the Perlin class. This had several drawbacks. First, there really wasn't a need for the indirection of calling a separate method from the constructor. Second, because the `init()` method did not delete any existing allocations, it was not safe to call more than once. Given all this, there was really no good reason to not just perform the initialization in the constructor.
1 parent d216fee commit 70ae85b

File tree

2 files changed

+32
-54
lines changed

2 files changed

+32
-54
lines changed

books/RayTracingTheNextWeek.html

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,14 @@
10601060
class perlin {
10611061
public:
10621062
perlin() {
1063-
init();
1063+
ranfloat = new double[point_count];
1064+
for (int i = 0; i < point_count; ++i) {
1065+
ranfloat[i] = random_double();
1066+
}
1067+
1068+
perm_x = perlin_generate_perm();
1069+
perm_y = perlin_generate_perm();
1070+
perm_z = perlin_generate_perm();
10641071
}
10651072

10661073
~perlin() {
@@ -1087,17 +1094,6 @@
10871094
int* perm_y;
10881095
int* perm_z;
10891096

1090-
void init() {
1091-
ranfloat = new double[point_count];
1092-
for (int i = 0; i < point_count; ++i) {
1093-
ranfloat[i] = random_double();
1094-
}
1095-
1096-
perm_x = perlin_generate_perm();
1097-
perm_y = perlin_generate_perm();
1098-
perm_z = perlin_generate_perm();
1099-
}
1100-
11011097
static int* perlin_generate_perm() {
11021098
auto p = new int[point_count];
11031099

@@ -1300,12 +1296,24 @@
13001296
This is still a bit grid blocky looking, probably because the min and max of the pattern always
13011297
lands exactly on the integer x/y/z. Ken Perlin’s very clever trick was to instead put random unit
13021298
vectors (instead of just floats) on the lattice points, and use a dot product to move the min and
1303-
max off the lattice. So, first we need to change the random floats to random vectors:
1299+
max off the lattice. So, first we need to change the random floats to random vectors. These vectors
1300+
are any reasonable set of irregular directions, and I won't bother to make them exactly uniform:
13041301

13051302
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
13061303
class perlin {
13071304
public:
1308-
...
1305+
perlin() {
1306+
ranvec = new vec3[point_count];
1307+
1308+
for (int i = 0; i < point_count; ++i) {
1309+
ranvec[i] = unit_vector(vec3::random(-1,1));
1310+
}
1311+
1312+
perm_x = perlin_generate_perm();
1313+
perm_y = perlin_generate_perm();
1314+
perm_z = perlin_generate_perm();
1315+
}
1316+
13091317
~perlin() {
13101318
delete[] ranvec;
13111319
delete[] perm_x;
@@ -1325,33 +1333,7 @@
13251333
</div>
13261334

13271335
<div class='together'>
1328-
These vectors are any reasonable set of irregular directions, and I won't bother to make them
1329-
exactly uniform:
1330-
1331-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1332-
class perlin {
1333-
...
1334-
private:
1335-
...
1336-
void init() {
1337-
ranvec = new vec3[point_count];
1338-
1339-
for (int i = 0; i < point_count; ++i) {
1340-
ranvec[i] = unit_vector(vec3::random(-1,1));
1341-
}
1342-
1343-
perm_x = perlin_generate_perm();
1344-
perm_y = perlin_generate_perm();
1345-
perm_z = perlin_generate_perm();
1346-
}
1347-
...
1348-
}
1349-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1350-
[Listing [perlin-gen-2]: <kbd>[perlin.h]</kbd> New perlin_generate()]
1351-
</div>
1352-
1353-
<div class='together'>
1354-
The Perlin class is now:
1336+
The Perlin class `noise()` method is now:
13551337

13561338
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
13571339
class perlin {
@@ -1380,7 +1362,7 @@
13801362
...
13811363
}
13821364
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1383-
[Listing [perlin-2]: <kbd>[perlin.h]</kbd> The perlin class so far]
1365+
[Listing [perlin-2]: <kbd>[perlin.h]</kbd> Perlin class with new noise() method]
13841366
</div>
13851367

13861368
<div class='together'>

src/common/perlin.h

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@
1818
class perlin {
1919
public:
2020
perlin() {
21-
init();
21+
ranvec = new vec3[point_count];
22+
for (int i = 0; i < point_count; ++i) {
23+
ranvec[i] = unit_vector(vec3::random(-1,1));
24+
}
25+
26+
perm_x = perlin_generate_perm();
27+
perm_y = perlin_generate_perm();
28+
perm_z = perlin_generate_perm();
2229
}
2330

2431
~perlin() {
@@ -70,17 +77,6 @@ class perlin {
7077
int* perm_y;
7178
int* perm_z;
7279

73-
void init() {
74-
ranvec = new vec3[point_count];
75-
for (int i = 0; i < point_count; ++i) {
76-
ranvec[i] = unit_vector(vec3::random(-1,1));
77-
}
78-
79-
perm_x = perlin_generate_perm();
80-
perm_y = perlin_generate_perm();
81-
perm_z = perlin_generate_perm();
82-
}
83-
8480
static int* perlin_generate_perm() {
8581
auto p = new int[point_count];
8682

0 commit comments

Comments
 (0)