|
1060 | 1060 | class perlin {
|
1061 | 1061 | public:
|
1062 | 1062 | 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(); |
1064 | 1071 | }
|
1065 | 1072 |
|
1066 | 1073 | ~perlin() {
|
|
1087 | 1094 | int* perm_y;
|
1088 | 1095 | int* perm_z;
|
1089 | 1096 |
|
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 |
| - |
1101 | 1097 | static int* perlin_generate_perm() {
|
1102 | 1098 | auto p = new int[point_count];
|
1103 | 1099 |
|
|
1300 | 1296 | This is still a bit grid blocky looking, probably because the min and max of the pattern always
|
1301 | 1297 | lands exactly on the integer x/y/z. Ken Perlin’s very clever trick was to instead put random unit
|
1302 | 1298 | 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: |
1304 | 1301 |
|
1305 | 1302 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
1306 | 1303 | class perlin {
|
1307 | 1304 | 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 | + |
1309 | 1317 | ~perlin() {
|
1310 | 1318 | delete[] ranvec;
|
1311 | 1319 | delete[] perm_x;
|
|
1325 | 1333 | </div>
|
1326 | 1334 |
|
1327 | 1335 | <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: |
1355 | 1337 |
|
1356 | 1338 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
1357 | 1339 | class perlin {
|
|
1380 | 1362 | ...
|
1381 | 1363 | }
|
1382 | 1364 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
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] |
1384 | 1366 | </div>
|
1385 | 1367 |
|
1386 | 1368 | <div class='together'>
|
|
0 commit comments