1 /*
2 ===========================================================================
3 Copyright (C) 1999 - 2005, Id Software, Inc.
4 Copyright (C) 2000 - 2013, Raven Software, Inc.
5 Copyright (C) 2001 - 2013, Activision, Inc.
6 Copyright (C) 2005 - 2015, ioquake3 contributors
7 Copyright (C) 2013 - 2015, OpenJK contributors
8 
9 This file is part of the OpenJK source code.
10 
11 OpenJK is free software; you can redistribute it and/or modify it
12 under the terms of the GNU General Public License version 2 as
13 published by the Free Software Foundation.
14 
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, see <http://www.gnu.org/licenses/>.
22 ===========================================================================
23 */
24 
25 // tr_noise.c
26 #include "tr_common.h"
27 
28 #define NOISE_SIZE 256
29 #define NOISE_MASK ( NOISE_SIZE - 1 )
30 
31 #define VAL( a ) s_noise_perm[ ( a ) & ( NOISE_MASK )]
32 #define INDEX( x, y, z, t ) VAL( x + VAL( y + VAL( z + VAL( t ) ) ) )
33 
34 static float s_noise_table[NOISE_SIZE];
35 static int s_noise_perm[NOISE_SIZE];
36 
37 #define LERP( a, b, w ) ( a * ( 1.0f - w ) + b * w )
38 
GetNoiseValue(int x,int y,int z,int t)39 static float GetNoiseValue( int x, int y, int z, int t )
40 {
41 	int index = INDEX( ( int ) x, ( int ) y, ( int ) z, ( int ) t );
42 
43 	return s_noise_table[index];
44 }
45 
GetNoiseTime(int t)46 float GetNoiseTime( int t )
47 {
48 	int index = VAL( t );
49 
50 	return (1 + s_noise_table[index]);
51 }
52 
R_NoiseInit(void)53 void R_NoiseInit( void )
54 {
55 	int i;
56 
57 	srand( 1001 );
58 
59 	for ( i = 0; i < NOISE_SIZE; i++ )
60 	{
61 		s_noise_table[i] = ( float ) ( ( ( rand() / ( float ) RAND_MAX ) * 2.0 - 1.0 ) );
62 		s_noise_perm[i] = ( unsigned char ) ( rand() / ( float ) RAND_MAX * 255 );
63 	}
64 }
65 
R_NoiseGet4f(float x,float y,float z,float t)66 float R_NoiseGet4f( float x, float y, float z, float t )
67 {
68 	int i;
69 	int ix, iy, iz, it;
70 	float fx, fy, fz, ft;
71 	float front[4];
72 	float back[4];
73 	float fvalue, bvalue, value[2], finalvalue;
74 
75 	ix = ( int ) floor( x );
76 	fx = x - ix;
77 	iy = ( int ) floor( y );
78 	fy = y - iy;
79 	iz = ( int ) floor( z );
80 	fz = z - iz;
81 	it = ( int ) floor( t );
82 	ft = t - it;
83 
84 	for ( i = 0; i < 2; i++ )
85 	{
86 		front[0] = GetNoiseValue( ix, iy, iz, it + i );
87 		front[1] = GetNoiseValue( ix+1, iy, iz, it + i );
88 		front[2] = GetNoiseValue( ix, iy+1, iz, it + i );
89 		front[3] = GetNoiseValue( ix+1, iy+1, iz, it + i );
90 
91 		back[0] = GetNoiseValue( ix, iy, iz + 1, it + i );
92 		back[1] = GetNoiseValue( ix+1, iy, iz + 1, it + i );
93 		back[2] = GetNoiseValue( ix, iy+1, iz + 1, it + i );
94 		back[3] = GetNoiseValue( ix+1, iy+1, iz + 1, it + i );
95 
96 		fvalue = LERP( LERP( front[0], front[1], fx ), LERP( front[2], front[3], fx ), fy );
97 		bvalue = LERP( LERP( back[0], back[1], fx ), LERP( back[2], back[3], fx ), fy );
98 
99 		value[i] = LERP( fvalue, bvalue, fz );
100 	}
101 
102 	finalvalue = LERP( value[0], value[1], ft );
103 
104 	return finalvalue;
105 }
106