tr_noise.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1999-2005 Id Software, Inc.
  4. This file is part of Quake III Arena source code.
  5. Quake III Arena source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake III Arena source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Foobar; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. // tr_noise.c
  19. #include "tr_local.h"
  20. #define NOISE_SIZE 256
  21. #define NOISE_MASK ( NOISE_SIZE - 1 )
  22. #define VAL( a ) s_noise_perm[ ( a ) & ( NOISE_MASK )]
  23. #define INDEX( x, y, z, t ) VAL( x + VAL( y + VAL( z + VAL( t ) ) ) )
  24. static float s_noise_table[NOISE_SIZE];
  25. static int s_noise_perm[NOISE_SIZE];
  26. #define LERP( a, b, w ) ( a * ( 1.0f - w ) + b * w )
  27. static float GetNoiseValue( int x, int y, int z, int t )
  28. {
  29. int index = INDEX( ( int ) x, ( int ) y, ( int ) z, ( int ) t );
  30. return s_noise_table[index];
  31. }
  32. void R_NoiseInit( void )
  33. {
  34. int i;
  35. srand( 1001 );
  36. for ( i = 0; i < NOISE_SIZE; i++ )
  37. {
  38. s_noise_table[i] = ( float ) ( ( ( rand() / ( float ) RAND_MAX ) * 2.0 - 1.0 ) );
  39. s_noise_perm[i] = ( unsigned char ) ( rand() / ( float ) RAND_MAX * 255 );
  40. }
  41. }
  42. float R_NoiseGet4f( float x, float y, float z, float t )
  43. {
  44. int i;
  45. int ix, iy, iz, it;
  46. float fx, fy, fz, ft;
  47. float front[4];
  48. float back[4];
  49. float fvalue, bvalue, value[2], finalvalue;
  50. ix = ( int ) floor( x );
  51. fx = x - ix;
  52. iy = ( int ) floor( y );
  53. fy = y - iy;
  54. iz = ( int ) floor( z );
  55. fz = z - iz;
  56. it = ( int ) floor( t );
  57. ft = t - it;
  58. for ( i = 0; i < 2; i++ )
  59. {
  60. front[0] = GetNoiseValue( ix, iy, iz, it + i );
  61. front[1] = GetNoiseValue( ix+1, iy, iz, it + i );
  62. front[2] = GetNoiseValue( ix, iy+1, iz, it + i );
  63. front[3] = GetNoiseValue( ix+1, iy+1, iz, it + i );
  64. back[0] = GetNoiseValue( ix, iy, iz + 1, it + i );
  65. back[1] = GetNoiseValue( ix+1, iy, iz + 1, it + i );
  66. back[2] = GetNoiseValue( ix, iy+1, iz + 1, it + i );
  67. back[3] = GetNoiseValue( ix+1, iy+1, iz + 1, it + i );
  68. fvalue = LERP( LERP( front[0], front[1], fx ), LERP( front[2], front[3], fx ), fy );
  69. bvalue = LERP( LERP( back[0], back[1], fx ), LERP( back[2], back[3], fx ), fy );
  70. value[i] = LERP( fvalue, bvalue, fz );
  71. }
  72. finalvalue = LERP( value[0], value[1], ft );
  73. return finalvalue;
  74. }