-1

I have found this code regarding 3D perlin noise: https://blog.kazade.co.uk/2014/05/a-public-domain-c11-1d2d3d-perlin-noise.html

I created a noise.h file from the first chunk of code. Then I added the second chunk to my C++ project, included the noise.h header file, and added it to my project via the solution explorer.

Everything is fine and I have no errors with the inserted code. The problem is that I don't really understand how to use it. He simply states:

It's pretty straightforward to use, just instantiate a Perlin, or PerlinOctave instance, and call noise(x, y, z); Simples.

I am not overly experienced with C++ so I don't know what he means by instantiating. But my attempts were:

float n = noise(x,y,z); (Where x,y,z are my float variables).

I also tried:

float n = PerlinOctave::noise(x,y,z); (Where x,y,z are my float variables).

Visual Studio reports an error saying: "A namespace name is not allowed"

He also doesn't give any instructions on how to use the octave function, which is separate from the noise function.

Does anyone have a better understanding of how to use this code?

Rich95
  • 199
  • 1
  • 3
  • 18
  • Note all the functions return doubles, so your `n` should probably be a double too. I am a big believer in understanding what you use before you use it ;) So have a look here to learn a bit more about classes and instances : https://www.learncpp.com/cpp-tutorial/classes-and-class-members/. – Pepijn Kramer Oct 26 '22 at 05:26
  • 1
    *"so I don't know what he means by instantiating"* -- this is a basis for a much better title than "I need help" (which is [not a question](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question)). Look at the example titles in [ask]. Then compare a title like "I need help getting something to work" to "What does it mean to 'instantiate' a class?" Which is more informative about your precise question? – JaMiT Oct 26 '22 at 05:31
  • Hi @JaMiT - thank you for the suggestion. You are right. My title wasn't very clear or helpful to my problem. I've renamed it to your suggestion and I'll take more care in the future to be more descriptive. – Rich95 Oct 27 '22 at 15:50

1 Answers1

4

Instantiation means creating an object. The author means you create a Perlin object as follows:

uint32_t seed = 42;
noise::Perlin perlin(seed);

And then you can call the noise methods:

for (double x = 0.0; x < 1.0; x += 0.1)
{
    std::cout << perlin.noise(x) << "\n";
}

Similarly for the PerlinOctave class.

It might pay to take a step back and learn the basics of C++ if you're going to continue with the language. Otherwise, you should prepare for a whole world of pain.

paddy
  • 60,864
  • 6
  • 61
  • 103
  • 1
    Note `seed` need not be 42. The algorithm will be at its most random if you provide a random number for the seed, but for testing purposes or any other time you want a reproducible result (testing against random numbers is a pain in the ass), use a fixed seed. – user4581301 Oct 26 '22 at 05:16
  • Thank you @paddy - that did indeed fix the issue. It looks like I'll need to brush up on my C++ understanding. I very much appreciate your explanation. – Rich95 Oct 27 '22 at 15:51