PerlinNoise 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* -*-c++-*-
  2. *
  3. * OpenSceneGraph example, osgshaders.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. */
  20. /************************************************************************
  21. * *
  22. * Copyright (C) 2002 3Dlabs Inc. Ltd. *
  23. * *
  24. ***********************************************************************/
  25. /* Adapted from osgshaders example by Robert Osfield for use as part of osgUtil.*/
  26. #ifndef PERLINENOISE_H
  27. #define PERLINENOISE_H
  28. #include <osg/Texture3D>
  29. #include <osgUtil/Export>
  30. namespace osgUtil
  31. {
  32. class OSGUTIL_EXPORT PerlinNoise
  33. {
  34. public:
  35. PerlinNoise();
  36. void SetNoiseFrequency(int frequency);
  37. double noise1(double arg);
  38. double noise2(double vec[2]);
  39. double noise3(double vec[3]);
  40. void normalize2(double vec[2]);
  41. void normalize3(double vec[3]);
  42. /*
  43. In what follows "alpha" is the weight when the sum is formed.
  44. Typically it is 2, As this approaches 1 the function is noisier.
  45. "beta" is the harmonic scaling/spacing, typically 2.
  46. */
  47. double PerlinNoise1D(double x,double alpha, double beta, int n);
  48. double PerlinNoise2D(double x,double y,double alpha, double beta, int n);
  49. double PerlinNoise3D(double x,double y,double z,double alpha, double beta, int n);
  50. osg::Image* create3DNoiseImage(int texSize);
  51. osg::Texture3D* create3DNoiseTexture(int texSize );
  52. protected:
  53. void initNoise(void);
  54. enum { MAXB = 0x100 };
  55. int p[MAXB + MAXB + 2];
  56. double g3[MAXB + MAXB + 2][3];
  57. double g2[MAXB + MAXB + 2][2];
  58. double g1[MAXB + MAXB + 2];
  59. int start;
  60. int B;
  61. int BM;
  62. };
  63. inline osg::Image* create3DNoiseImage(int texSize)
  64. {
  65. PerlinNoise pn;
  66. return pn.create3DNoiseImage(texSize);
  67. }
  68. inline osg::Texture3D* create3DNoiseTexture(int texSize )
  69. {
  70. PerlinNoise pn;
  71. return pn.create3DNoiseTexture(texSize);
  72. }
  73. }
  74. #endif // PERLINENOISE_H