Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
6.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: noise() primitives.
  4. //
  5. //=====================================================================================//
  6. #include <math.h>
  7. #include "basetypes.h"
  8. #include <memory.h>
  9. #include "tier0/dbg.h"
  10. #include "mathlib/mathlib.h"
  11. #include "mathlib/vector.h"
  12. #include "mathlib/noise.h"
  13. // memdbgon must be the last include file in a .cpp file!!!
  14. #include "tier0/memdbgon.h"
  15. // generate high quality noise based upon "sparse convolution". HIgher quality than perlin noise,
  16. // and no direcitonal artifacts.
  17. #include "noisedata.h"
  18. #define N_IMPULSES_PER_CELL 5
  19. #define NORMALIZING_FACTOR 1.0
  20. //(0.5/N_IMPULSES_PER_CELL)
  21. static inline int LatticeCoord(float x)
  22. {
  23. return ((int) floor(x)) & 0xff;
  24. }
  25. static inline int Hash4D(int ix, int iy, int iz, int idx)
  26. {
  27. int ret=perm_a[ix];
  28. ret=perm_b[(ret+iy) & 0xff];
  29. ret=perm_c[(ret+iz) & 0xff];
  30. ret=perm_d[(ret+idx) & 0xff];
  31. return ret;
  32. }
  33. #define SQ(x) ((x)*(x))
  34. static float CellNoise( int ix, int iy, int iz, float xfrac, float yfrac, float zfrac,
  35. float (*pNoiseShapeFunction)(float) )
  36. {
  37. float ret=0;
  38. for(int idx=0;idx<N_IMPULSES_PER_CELL;idx++)
  39. {
  40. int coord_idx=Hash4D( ix, iy, iz, idx );
  41. float dsq=SQ(impulse_xcoords[coord_idx]-xfrac)+
  42. SQ(impulse_ycoords[coord_idx]-yfrac)+
  43. SQ(impulse_zcoords[coord_idx]-zfrac);
  44. dsq = sqrt( dsq );
  45. if (dsq < 1.0 )
  46. {
  47. ret += (*pNoiseShapeFunction)( 1-dsq );
  48. }
  49. }
  50. return ret;
  51. }
  52. float SparseConvolutionNoise( Vector const &pnt )
  53. {
  54. return SparseConvolutionNoise( pnt, QuinticInterpolatingPolynomial );
  55. }
  56. float FractalNoise( Vector const &pnt, int n_octaves)
  57. {
  58. float scale=1.0;
  59. float iscale=1.0;
  60. float ret=0;
  61. float sumscale=0;
  62. for(int o=0;o<n_octaves;o++)
  63. {
  64. Vector p1=pnt;
  65. p1 *= scale;
  66. ret+=iscale * SparseConvolutionNoise( p1 );
  67. sumscale += iscale;
  68. scale *= 2.0;
  69. iscale *= 0.5;
  70. }
  71. return ret * ( 1.0/sumscale );
  72. }
  73. float Turbulence( Vector const &pnt, int n_octaves)
  74. {
  75. float scale=1.0;
  76. float iscale=1.0;
  77. float ret=0;
  78. float sumscale=0;
  79. for(int o=0;o<n_octaves;o++)
  80. {
  81. Vector p1=pnt;
  82. p1 *= scale;
  83. ret+=iscale * fabs ( 2.0*( SparseConvolutionNoise( p1 )-.5 ) );
  84. sumscale += iscale;
  85. scale *= 2.0;
  86. iscale *= 0.5;
  87. }
  88. return ret * ( 1.0/sumscale );
  89. }
  90. #ifdef MEASURE_RANGE
  91. float fmin1=10000000.0;
  92. float fmax1=-1000000.0;
  93. #endif
  94. float SparseConvolutionNoise(Vector const &pnt, float (*pNoiseShapeFunction)(float) )
  95. {
  96. // computer integer lattice point
  97. int ix=LatticeCoord(pnt.x);
  98. int iy=LatticeCoord(pnt.y);
  99. int iz=LatticeCoord(pnt.z);
  100. // compute offsets within unit cube
  101. float xfrac=pnt.x-floor(pnt.x);
  102. float yfrac=pnt.y-floor(pnt.y);
  103. float zfrac=pnt.z-floor(pnt.z);
  104. float sum_out=0.;
  105. for(int ox=-1; ox<=1; ox++)
  106. for(int oy=-1; oy<=1; oy++)
  107. for(int oz=-1; oz<=1; oz++)
  108. {
  109. sum_out += CellNoise( ix+ox, iy+oy, iz+oz,
  110. xfrac-ox, yfrac-oy, zfrac-oz,
  111. pNoiseShapeFunction );
  112. }
  113. #ifdef MEASURE_RANGE
  114. fmin1=min(sum_out,fmin1);
  115. fmax1=max(sum_out,fmax1);
  116. #endif
  117. return RemapValClamped( sum_out, .544487, 9.219176, 0.0, 1.0 );
  118. }
  119. // Improved Perlin Noise
  120. // The following code is the c-ification of Ken Perlin's new noise algorithm
  121. // "JAVA REFERENCE IMPLEMENTATION OF IMPROVED NOISE - COPYRIGHT 2002 KEN PERLIN"
  122. // as available here: http://mrl.nyu.edu/~perlin/noise/
  123. float NoiseGradient(int hash, float x, float y, float z)
  124. {
  125. int h = hash & 15; // CONVERT LO 4 BITS OF HASH CODE
  126. float u = h<8 ? x : y; // INTO 12 GRADIENT DIRECTIONS.
  127. float v = h<4 ? y : (h==12||h==14 ? x : z);
  128. return ((h&1) == 0 ? u : -u) + ((h&2) == 0 ? v : -v);
  129. }
  130. int NoiseHashIndex( int i )
  131. {
  132. static int s_permutation[] =
  133. {
  134. 151,160,137,91,90,15,
  135. 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
  136. 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
  137. 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
  138. 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
  139. 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
  140. 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
  141. 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
  142. 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
  143. 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
  144. 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
  145. 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
  146. 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180
  147. };
  148. return s_permutation[ i & 0xff ];
  149. }
  150. float ImprovedPerlinNoise( Vector const &pnt )
  151. {
  152. float fx = floor(pnt.x);
  153. float fy = floor(pnt.y);
  154. float fz = floor(pnt.z);
  155. int X = (int)fx & 255; // FIND UNIT CUBE THAT
  156. int Y = (int)fy & 255; // CONTAINS POINT.
  157. int Z = (int)fz & 255;
  158. float x = pnt.x - fx; // FIND RELATIVE X,Y,Z
  159. float y = pnt.y - fy; // OF POINT IN CUBE.
  160. float z = pnt.z - fz;
  161. float u = QuinticInterpolatingPolynomial(x); // COMPUTE FADE CURVES
  162. float v = QuinticInterpolatingPolynomial(y); // FOR EACH OF X,Y,Z.
  163. float w = QuinticInterpolatingPolynomial(z);
  164. int A = NoiseHashIndex( X ) + Y; // HASH COORDINATES OF
  165. int AA = NoiseHashIndex( A ) + Z; // THE 8 CUBE CORNERS,
  166. int AB = NoiseHashIndex( A + 1 ) + Z;
  167. int B = NoiseHashIndex( X + 1 ) + Y;
  168. int BA = NoiseHashIndex( B ) + Z;
  169. int BB = NoiseHashIndex( B + 1 ) + Z;
  170. float g0 = NoiseGradient(NoiseHashIndex(AA ), x , y , z );
  171. float g1 = NoiseGradient(NoiseHashIndex(BA ), x-1, y , z );
  172. float g2 = NoiseGradient(NoiseHashIndex(AB ), x , y-1, z );
  173. float g3 = NoiseGradient(NoiseHashIndex(BB ), x-1, y-1, z );
  174. float g4 = NoiseGradient(NoiseHashIndex(AA+1), x , y , z-1 );
  175. float g5 = NoiseGradient(NoiseHashIndex(BA+1), x-1, y , z-1 );
  176. float g6 = NoiseGradient(NoiseHashIndex(AB+1), x , y-1, z-1 );
  177. float g7 = NoiseGradient(NoiseHashIndex(BB+1), x-1, y-1, z-1 );
  178. // AND ADD BLENDED RESULTS FROM 8 CORNERS OF CUBE
  179. float g01 = Lerp( u, g0, g1 );
  180. float g23 = Lerp( u, g2, g3 );
  181. float g45 = Lerp( u, g4, g5 );
  182. float g67 = Lerp( u, g6, g7 );
  183. float g0123 = Lerp( v, g01, g23 );
  184. float g4567 = Lerp( v, g45, g67 );
  185. return Lerp( w, g0123,g4567 );
  186. }