RandNoise.cpp 904 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include "RandNoise.h"
  2. #include <Random.h>
  3. #include <stdlib.h>
  4. #include "Constants.h"
  5. #include "FastNoiseLite.h"
  6. #include "FastNoiseWrapper.h"
  7. #ifndef RAND_MAX
  8. # define RAND_MAX 0x7FFF
  9. #endif
  10. RandNoise::RandNoise(int seed)
  11. : Noise(),
  12. seed(seed)
  13. {
  14. srand(seed);
  15. table = new unsigned short[RandNoiseTableSize];
  16. for (int i = 0; i < RandNoiseTableSize; i++)
  17. {
  18. int r = rand();
  19. table[i] = (unsigned short)(r % RAND_MAX);
  20. }
  21. }
  22. RandNoise::~RandNoise()
  23. {
  24. delete[] table;
  25. }
  26. int RandNoise::getSeed() const
  27. {
  28. return seed;
  29. }
  30. double RandNoise::getNoise(double x, double y, double z)
  31. {
  32. int index = seed % RandNoiseTableSize;
  33. index = abs(table[index] + (int)x) % RandNoiseTableSize;
  34. index = abs(table[index] + (int)y) % RandNoiseTableSize;
  35. index = abs(table[index] + (int)z) % RandNoiseTableSize;
  36. return (double)table[index] / RAND_MAX;
  37. }