noise2d.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _NOISE2D_H_
  23. #define _NOISE2D_H_
  24. #ifndef _PLATFORM_H_
  25. #include "platform/platform.h"
  26. #endif
  27. #ifndef _MMATH_H_
  28. #include "math/mMath.h"
  29. #endif
  30. #ifndef _MRANDOM_H_
  31. #include "math/mRandom.h"
  32. #endif
  33. class Noise2D
  34. {
  35. private:
  36. enum Constants {
  37. SIZE = 0x100,
  38. SIZE_MASK = 0x0ff
  39. };
  40. S32 mPermutation[SIZE + SIZE + 2];
  41. F32 mGradient[SIZE + SIZE + 2][2];
  42. U32 mSeed;
  43. MRandom mRandom;
  44. F32 curve(F32 t);
  45. void setup(F32 t, S32 &b0, S32 &b1, F32 &r0, F32 &r1);
  46. F32 dot(const F32 *q, F32 rx, F32 ry);
  47. void normalize(F32 v[2]);
  48. public:
  49. Noise2D();
  50. ~Noise2D();
  51. void setSeed(U32 seed);
  52. U32 getSeed();
  53. F32 getValue(F32 u, F32 v, S32 interval);
  54. /// @name Noise
  55. /// These functions actually generate
  56. /// noise values into the passed in destination
  57. /// array.
  58. ///
  59. /// Note that the output values of these functions
  60. /// are from -1.0 to 1.0.
  61. ///
  62. /// fBm - Fractal Brownian Motion - A simple noise generation
  63. /// algorithm, it tends to generate either flowing rounded
  64. /// hills or rounded mountainous shapes.
  65. /// @{
  66. void fBm( Vector<F32> *dst, U32 size, U32 interval, F32 h, F32 octave=5.0f);
  67. /// rigidMultiFractal
  68. /// Creates ridged mountains with a high frequency detail.
  69. void rigidMultiFractal( Vector<F32> *dst, Vector<F32> *signal, U32 size, U32 interval, F32 h, F32 octave=5.0f);
  70. /// @}
  71. bool erodeHydraulic(Vector<F32> *src, Vector<F32> *dst, U32 iterations, U32 size );
  72. bool erodeThermal(Vector<F32> *src, Vector<F32> *dst, F32 slope, F32 materialLoss, U32 iterations, U32 size, U32 squareSize, F32 maxHeight );
  73. F32 turbulence(F32 x, F32 y, F32 freq);
  74. void getMinMax( Vector<F32> *src, F32 *maxNoise, F32 *minNoise, U32 size );
  75. };
  76. #endif // _NOISE2D_H_