2
===========================================================================
3
Copyright (C) 1999-2005 Id Software, Inc.
5
This file is part of Quake III Arena source code.
7
Quake III Arena source code is free software; you can redistribute it
8
and/or modify it under the terms of the GNU General Public License as
9
published by the Free Software Foundation; either version 2 of the License,
10
or (at your option) any later version.
12
Quake III Arena source code is distributed in the hope that it will be
13
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU General Public License for more details.
17
You should have received a copy of the GNU General Public License
18
along with Quake III Arena source code; if not, write to the Free Software
19
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
===========================================================================
25
#define NOISE_SIZE 256
26
#define NOISE_MASK ( NOISE_SIZE - 1 )
28
#define VAL( a ) s_noise_perm[ ( a ) & ( NOISE_MASK )]
29
#define INDEX( x, y, z, t ) VAL( x + VAL( y + VAL( z + VAL( t ) ) ) )
31
static float s_noise_table[NOISE_SIZE];
32
static int s_noise_perm[NOISE_SIZE];
34
#define LERP( a, b, w ) ( a * ( 1.0f - w ) + b * w )
36
static float GetNoiseValue( int x, int y, int z, int t )
38
int index = INDEX( ( int ) x, ( int ) y, ( int ) z, ( int ) t );
40
return s_noise_table[index];
43
void R_NoiseInit( void )
49
for ( i = 0; i < NOISE_SIZE; i++ )
51
s_noise_table[i] = ( float ) ( ( ( rand() / ( float ) RAND_MAX ) * 2.0 - 1.0 ) );
52
s_noise_perm[i] = ( unsigned char ) ( rand() / ( float ) RAND_MAX * 255 );
56
float R_NoiseGet4f( float x, float y, float z, float t )
63
float fvalue, bvalue, value[2], finalvalue;
65
ix = ( int ) floor( x );
67
iy = ( int ) floor( y );
69
iz = ( int ) floor( z );
71
it = ( int ) floor( t );
74
for ( i = 0; i < 2; i++ )
76
front[0] = GetNoiseValue( ix, iy, iz, it + i );
77
front[1] = GetNoiseValue( ix+1, iy, iz, it + i );
78
front[2] = GetNoiseValue( ix, iy+1, iz, it + i );
79
front[3] = GetNoiseValue( ix+1, iy+1, iz, it + i );
81
back[0] = GetNoiseValue( ix, iy, iz + 1, it + i );
82
back[1] = GetNoiseValue( ix+1, iy, iz + 1, it + i );
83
back[2] = GetNoiseValue( ix, iy+1, iz + 1, it + i );
84
back[3] = GetNoiseValue( ix+1, iy+1, iz + 1, it + i );
86
fvalue = LERP( LERP( front[0], front[1], fx ), LERP( front[2], front[3], fx ), fy );
87
bvalue = LERP( LERP( back[0], back[1], fx ), LERP( back[2], back[3], fx ), fy );
89
value[i] = LERP( fvalue, bvalue, fz );
92
finalvalue = LERP( value[0], value[1], ft );