Quake-III-Arena

Quake III Arena GPL Source Release
Log | Files | Refs

tr_noise.c (2824B)


      1 /*
      2 ===========================================================================
      3 Copyright (C) 1999-2005 Id Software, Inc.
      4 
      5 This file is part of Quake III Arena source code.
      6 
      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.
     11 
     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.
     16 
     17 You should have received a copy of the GNU General Public License
     18 along with Foobar; if not, write to the Free Software
     19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20 ===========================================================================
     21 */
     22 // tr_noise.c
     23 #include "tr_local.h"
     24 
     25 #define NOISE_SIZE 256
     26 #define NOISE_MASK ( NOISE_SIZE - 1 )
     27 
     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 ) ) ) )
     30 
     31 static float s_noise_table[NOISE_SIZE];
     32 static int s_noise_perm[NOISE_SIZE];
     33 
     34 #define LERP( a, b, w ) ( a * ( 1.0f - w ) + b * w )
     35 
     36 static float GetNoiseValue( int x, int y, int z, int t )
     37 {
     38 	int index = INDEX( ( int ) x, ( int ) y, ( int ) z, ( int ) t );
     39 
     40 	return s_noise_table[index];
     41 }
     42 
     43 void R_NoiseInit( void )
     44 {
     45 	int i;
     46 
     47 	srand( 1001 );
     48 
     49 	for ( i = 0; i < NOISE_SIZE; i++ )
     50 	{
     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 );
     53 	}
     54 }
     55 
     56 float R_NoiseGet4f( float x, float y, float z, float t )
     57 {
     58 	int i;
     59 	int ix, iy, iz, it;
     60 	float fx, fy, fz, ft;
     61 	float front[4];
     62 	float back[4];
     63 	float fvalue, bvalue, value[2], finalvalue;
     64 
     65 	ix = ( int ) floor( x );
     66 	fx = x - ix;
     67 	iy = ( int ) floor( y );
     68 	fy = y - iy;
     69 	iz = ( int ) floor( z );
     70 	fz = z - iz;
     71 	it = ( int ) floor( t );
     72 	ft = t - it;
     73 
     74 	for ( i = 0; i < 2; i++ )
     75 	{
     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 );
     80 
     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 );
     85 
     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 );
     88 
     89 		value[i] = LERP( fvalue, bvalue, fz );
     90 	}
     91 
     92 	finalvalue = LERP( value[0], value[1], ft );
     93 
     94 	return finalvalue;
     95 }