llnoise.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * @file llnoise.cpp
  3. * @brief Perlin noise routines for procedural textures, etc
  4. *
  5. * $LicenseInfo:firstyear=2000&license=viewergpl$
  6. *
  7. * Copyright (c) 2000-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #include "llnoise.h"
  34. #include "llrand.h"
  35. //static
  36. F32 LOGHALFFACTOR = 1.f / logf(0.5f);
  37. // static
  38. #define B 0x100
  39. S32 p[B + B + 2];
  40. F32 g3[B + B + 2][3];
  41. F32 g2[B + B + 2][2];
  42. F32 g1[B + B + 2];
  43. S32 gNoiseStart = 1;
  44. F32 noise2(F32* vec)
  45. {
  46. U8 bx0, bx1, by0, by1;
  47. U32 b00, b10, b01, b11;
  48. F32 rx0, rx1, ry0, ry1, *q, sx, sy, a, b, u, v;
  49. S32 i, j;
  50. if (gNoiseStart)
  51. {
  52. gNoiseStart = 0;
  53. init();
  54. }
  55. fast_setup(*vec, bx0, bx1, rx0, rx1);
  56. fast_setup(*(vec + 1), by0, by1, ry0, ry1);
  57. i = *(p + bx0);
  58. j = *(p + bx1);
  59. b00 = *(p + i + by0);
  60. b10 = *(p + j + by0);
  61. b01 = *(p + i + by1);
  62. b11 = *(p + j + by1);
  63. sx = s_curve(rx0);
  64. sy = s_curve(ry0);
  65. q = *(g2 + b00);
  66. u = fast_at2(rx0, ry0, q);
  67. q = *(g2 + b10);
  68. v = fast_at2(rx1, ry0, q);
  69. a = lerp_m(sx, u, v);
  70. q = *(g2 + b01);
  71. u = fast_at2(rx0,ry1,q);
  72. q = *(g2 + b11);
  73. v = fast_at2(rx1,ry1,q);
  74. b = lerp_m(sx, u, v);
  75. return lerp_m(sy, a, b);
  76. }