At some point while programming you are confronted with the term Perlin or Noise or Perlin-Noise and ask yourself what kind of function it is and what is it good for!Here it is explained nicely but dryly ► wikipedia.... Perlin-Noise . In short, they are pattern generation algorithms based on random numbers that are intended to simulate random-based but still orderly behavior! As we know from nature, all leaves are identical in some way but not the same. Or all birds are similar, but not all are chickens, ... etc. 1.) ... Example of a simple Perlin noise as a simple cloud!
|
(Picture-3) Perlin-Noise Wood! |
https://web.archive.org/web/20160604173317/http://freespace.virgin.net/hugo.elias/models/m_clouds.htm
Real-time Perlin noise coded to generate the clouds and all the rest of the graphics stuff, and Matt designed the overall program and wrote the all-important inner loop that actually makes the clouds as good as they are.
(Image-4) Clouds with Perlin noise as a function! |
2.) Why do I need this knowledge? I need to understand Perlin Noise!
Large game companies, film industries, ... can hire armies of artists and developers to create the impossible that make up the virtual world of the game or 3D graphics for movies. If you want to design a game or 3D task without such financial means and extensive resources to generate an attractive alternative for certain natural phenomena such as terrain, trees and atmospheric effects, then by using auto-generation you will achieve the same or even better. With the help of a simple random number generator, a high quality auto-generation system can produce remarkably realistic models and objects.
Other useful links:
► https://www.cs.umd.edu/class/fall2019/cmsc425/handouts/lect14-perlin.pdf
Even an Oscar:
► https://web.archive.org/web/20211208192020/https://mrl.cs.nyu.edu/~perlin/doc/oscar.html
Ken Perlin page:
► https://mrl.cs.nyu.edu/~perlin/
(Image-1) Ken Perlin and more features! |
Or to generate an entire city
► https://www.shadertoy.com/view/XtsSWs
(Image-2) Perlin noise and city with or from random numbers! |
3.) Simple Perlin-Noise example code in CPP
Your C++ code implements Perlin noise generation and uses the C++ standard library for some functions. Here is a commented version of your code in English:
//Here's the translated and explained C++ code for generating Perlin noise, including comments on the functionality: #include <iostream> #include <cmath> #include <vector> #include <conio.h> // For _getch to wait for a key press std::vector<int> permutation = std::vector<int>(512); class PerlinNoise { public: PerlinNoise() { int i = 0; // Initialize the permutation vector with values from 0 to 255 for (i = 0; i < 256; ++i) { permutation[i] = i; } // Shuffle the permutation vector using a custom random number generator int seed = 42; // You can change the seed for different patterns shufflePermutation(seed); // Duplicate the permutation vector to make wrapping easier for (i = 0; i < 256; ++i) { permutation[256 + i] = permutation[i]; } } double noise(double x, double y, double z) const { int X = static_cast<int>(floor(x)) & 255; int Y = static_cast<int>(floor(y)) & 255; int Z = static_cast<int>(floor(z)) & 255; x -= floor(x); y -= floor(y); z -= floor(z); double u = fade(x); double v = fade(y); double w = fade(z); int A = permutation[X] + Y; int AA = permutation[A] + Z; int AB = permutation[A + 1] + Z; int B = permutation[X + 1] + Y; int BA = permutation[B] + Z; int BB = permutation[B + 1] + Z; double p1 = grad(permutation[AA], x, y, z); double p2 = grad(permutation[BA], x - 1, y, z); double p3 = grad(permutation[AB], x, y - 1, z); double p4 = grad(permutation[BB], x - 1, y - 1, z); double p5 = grad(permutation[AA + 1], x, y, z - 1); double p6 = grad(permutation[BA + 1], x - 1, y, z - 1); double p7 = grad(permutation[AB + 1], x, y - 1, z - 1); double p8 = grad(permutation[BB + 1], x - 1, y - 1, z - 1); double x1 = lerp(u, p1, p2); double x2 = lerp(u, p3, p4); double x3 = lerp(u, p5, p6); double x4 = lerp(u, p7, p8); double y1 = lerp(v, x1, x2); double y2 = lerp(v, x3, x4); return lerp(w, y1, y2); } private: double fade(double t) const { return t * t * t * (t * (t * 6 - 15) + 10); } double lerp(double t, double a, double b) const { return a + t * (b - a); } double grad(int hash, double x, double y, double z) const { int h = hash & 15; double u = h < 8 ? x : y; double v = h < 4 ? y : (h == 12 || h == 14 ? x : z); return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v); } void shufflePermutation(int seed) { int size = permutation.size(); for (int i = size - 1; i > 0; --i) { int j = seed % (i + 1); std::swap(permutation[i], permutation[j]); } } }; int main() { PerlinNoise perlin; double x = 5.0, y = 10.0, z = 15.0; double noiseValue = perlin.noise(x, y, z); std::cout << "Perlin noise at (" << x << ", " << y << ", " << z << ") = " << noiseValue << std::endl; std::cout << "Press any key to continue...\n"; // Wait for a key press _getch(); return 0; }
FAQ 35: Updated on: 4 September 2024 13:03