PolyPerlin.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyString.h"
  21. #include "PolyGlobals.h"
  22. #define SAMPLE_SIZE 1024
  23. namespace Polycode {
  24. /**
  25. * 2D Perlin noise.
  26. */
  27. class _PolyExport Perlin : public PolyBase
  28. {
  29. public:
  30. /**
  31. * Constructs perlin noise.
  32. * @param octaves Number of noise octaves.
  33. * @param freq Noise frequency.
  34. * @param amp Noise amplitude.
  35. * @param seed Noise seed.
  36. */
  37. Perlin(int octaves,Number freq,Number amp,int seed);
  38. Number Get2DTiledX(Number x, Number y, Number t) {
  39. return ( (t - x) * Get2D(x, y) + (x) * Get2D(x - t, y) ) / (t);
  40. }
  41. /**
  42. * Returns noise value at the specified coordinate.
  43. * @param x Horizontal coordinate.
  44. * @param y Vertical coordinate.
  45. */
  46. Number Get(Number x,Number y) {
  47. return Get2D(x,y);
  48. };
  49. Number Get2D(Number x,Number y) {
  50. Number vec[2];
  51. vec[0] = x;
  52. vec[1] = y;
  53. return perlin_noise_2D(vec);
  54. };
  55. Number Get3D(Number x,Number y, Number z) {
  56. Number vec[3];
  57. vec[0] = x;
  58. vec[1] = y;
  59. vec[2] = z;
  60. return perlin_noise_3D(vec);
  61. };
  62. protected:
  63. void init_perlin(int n,Number p);
  64. Number perlin_noise_2D(Number vec[2]);
  65. Number perlin_noise_3D(Number vec[2]);
  66. Number noise1(Number arg);
  67. Number noise2(Number vec[2]);
  68. Number noise3(Number vec[3]);
  69. void normalize2(Number v[2]);
  70. void normalize3(Number v[3]);
  71. void init(void);
  72. int mOctaves;
  73. Number mFrequency;
  74. Number mAmplitude;
  75. int mSeed;
  76. int p[SAMPLE_SIZE + SAMPLE_SIZE + 2];
  77. Number g3[SAMPLE_SIZE + SAMPLE_SIZE + 2][3];
  78. Number g2[SAMPLE_SIZE + SAMPLE_SIZE + 2][2];
  79. Number g1[SAMPLE_SIZE + SAMPLE_SIZE + 2];
  80. bool mStart;
  81. };
  82. }