Counter Strike : Global Offensive Source Code
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.

147 lines
4.4 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include <stdafx.h>
  8. #include <math.h>
  9. #define OCTAVE_COUNT 4
  10. #define OCTAVE_COUNT_MINUS_ONE OCTAVE_COUNT - 1
  11. //-----------------------------------------------------------------------------
  12. // quick random number generator - returns a value -1.0f to 1.0
  13. //-----------------------------------------------------------------------------
  14. float RandomNoise2D( int x, int y )
  15. {
  16. int n = x + y * 57;
  17. n = ( n << 13 ) ^ n;
  18. float value ( 1.0f - ( float )( ( n * ( n * n * 15731 + 789221 ) + 1376312589 ) & 0x7fffffff ) / 1073741824.0f );
  19. return value;
  20. }
  21. //-----------------------------------------------------------------------------
  22. // Purpose:
  23. //-----------------------------------------------------------------------------
  24. float SmoothNoise2D( int x, int y )
  25. {
  26. float corners = ( RandomNoise2D( x-1, y-1 ) + RandomNoise2D( x+1, y-1 ) +
  27. RandomNoise2D( x-1, y+1 ) + RandomNoise2D( x+1, y+1 ) ) / 16.0f;
  28. float sides = ( RandomNoise2D( x-1, y ) + RandomNoise2D( x+1, y ) +
  29. RandomNoise2D( x, y-1 ) + RandomNoise2D( x, y+1 ) ) / 8.0f;
  30. float center = RandomNoise2D( x, y ) / 4.0f;
  31. return corners + sides + center;
  32. }
  33. //-----------------------------------------------------------------------------
  34. // Purpose:
  35. //-----------------------------------------------------------------------------
  36. inline float InterpLinear( float a, float b, float x )
  37. {
  38. return ( a * ( 1.0f - x ) + b * x );
  39. }
  40. //-----------------------------------------------------------------------------
  41. // Purpose:
  42. //-----------------------------------------------------------------------------
  43. inline float InterpCos( float a, float b, float x )
  44. {
  45. float alpha = x * 3.1415927f; /* PI */
  46. float frac = 1.0f - ( float )cos( alpha ) * 0.5f;
  47. return ( a * ( 1.0f - frac ) + b * frac );
  48. }
  49. //-----------------------------------------------------------------------------
  50. // Purpose:
  51. //-----------------------------------------------------------------------------
  52. inline float InterpCubic( float a, float b, float c, float d, float x )
  53. {
  54. float P = ( d - c ) - ( a - b );
  55. float Q = ( a - b ) - P;
  56. float R = ( c - a );
  57. float S = b;
  58. return ( ( P * x * 3.0f ) + ( Q * x * 2.0f ) + ( R * x ) + S );
  59. }
  60. //-----------------------------------------------------------------------------
  61. // Purpose:
  62. //-----------------------------------------------------------------------------
  63. float InterpNoise2D( float x, float y )
  64. {
  65. int iX = ( int )x;
  66. float fracX = x - iX;
  67. int iY = ( int )y;
  68. float fracY = y - iY;
  69. float n[4];
  70. n[0] = SmoothNoise2D( iX, iY );
  71. n[1] = SmoothNoise2D( iX+1, iY );
  72. n[2] = SmoothNoise2D( iX, iY+1 );
  73. n[3] = SmoothNoise2D( iX+1, iY+1 );
  74. float interpX[2];
  75. interpX[0] = InterpLinear( n[0], n[1], fracX );
  76. interpX[1] = InterpLinear( n[2], n[3], fracX );
  77. return InterpLinear( interpX[0], interpX[1], fracY );
  78. }
  79. //-----------------------------------------------------------------------------
  80. // Purpose:
  81. // Input: x -
  82. // y -
  83. // rockiness - 0.0f - 1.0f (0.0 = smooth, 1.0f = jagged)
  84. // Output:
  85. //-----------------------------------------------------------------------------
  86. float PerlinNoise2D( float x, float y, float rockiness )
  87. {
  88. float total = 0.0;
  89. float persistence = rockiness;
  90. for( int ndxOctave = 0; ndxOctave < OCTAVE_COUNT_MINUS_ONE; ndxOctave++ )
  91. {
  92. float frequency = ( float )pow( 2.f, ndxOctave );
  93. float amplitude = ( float )pow( persistence, ndxOctave );
  94. total += InterpNoise2D( x * frequency, y * frequency ) * amplitude;
  95. }
  96. return total;
  97. }
  98. //-----------------------------------------------------------------------------
  99. // Purpose:
  100. // Input: x -
  101. // y -
  102. // rockiness - 0.0f - 1.0f (0.0 = smooth, 1.0f = jagged)
  103. // Output: is between -1.0f and 1.0f
  104. //-----------------------------------------------------------------------------
  105. float PerlinNoise2DScaled( float x, float y, float rockiness )
  106. {
  107. float total = 0.0;
  108. float persistence = rockiness;
  109. for( int ndxOctave = 0; ndxOctave < OCTAVE_COUNT_MINUS_ONE; ndxOctave++ )
  110. {
  111. float frequency = ( float )pow( 2.f, ndxOctave );
  112. float amplitude = ( float )pow( persistence, ndxOctave );
  113. total += InterpNoise2D( x * frequency, y * frequency ) * amplitude;
  114. }
  115. total /= ( float )OCTAVE_COUNT;
  116. return total;
  117. }