commit 8a57a50c346a19b12daf5b2c081c8d02127a06e3 parent 174a59a0271dda3140ab9ef9e96bda95640ff1c3 Author: Michael Savage <mikejsavage@gmail.com> Date: Wed May 10 03:03:41 +0300 Move lerp/bilerp into headers Diffstat:
array.h | | | 26 | ++++++++++++++++++++++++++ |
intrinsics.h | | | 6 | ++++++ |
terrain_manager.cc | | | 27 | --------------------------- |
diff --git a/array.h b/array.h @@ -299,3 +299,29 @@ inline array< T > file_get_array( const char * path ) { ASSERT( len % sizeof( T ) == 0 ); return array< T >( ( T * ) mem, len / sizeof( T ) ); } + +template< typename T > +static T bilerp( const array2d< T > arr, float x, float y ) { + size_t xi = ( size_t ) x; + size_t yi = ( size_t ) y; + size_t xi1 = min( xi + 1, arr.w - 1 ); + size_t yi1 = min( yi + 1, arr.h - 1 ); + + float xf = x - xi; + float yf = y - yi; + + T a = arr( xi, yi ); + T b = arr( xi1, yi ); + T c = arr( xi, yi1 ); + T d = arr( xi1, yi1 ); + + T ab = lerp( a, xf, b ); + T cd = lerp( c, xf, d ); + + return lerp( ab, yf, cd ); +} + +template< typename T > +static T bilerp01( const array2d< T > arr, float x, float y ) { + return bilerp( arr, x * arr.w, y * arr.h ); +} diff --git a/intrinsics.h b/intrinsics.h @@ -238,3 +238,9 @@ inline T * realloc_array( T * old, size_t count ) { return ( T * ) realloc( old, count * sizeof( T ) ); } #endif + +template< typename T > +static T lerp( T a, float t, T b ) { + ASSERT( t >= 0.0f && t <= 1.0f ); + return a * ( 1.0f - t ) + b * t; +} diff --git a/terrain_manager.cc b/terrain_manager.cc @@ -469,33 +469,6 @@ float terrain_height( const TerrainManager * tm, v3 position ) { return hm->bilerp_height( position.x - tx * TILE_SIZE, position.y - ty * TILE_SIZE ); } -template< typename T > -static T lerp( T a, float t, T b ) { - ASSERT( t >= 0.0f && t <= 1.0f ); - return a * ( 1.0f - t ) + b * t; -} - -template< typename T > -static T bilerp( const array2d< T > arr, float x, float y ) { - size_t xi = ( size_t ) x; - size_t yi = ( size_t ) y; - size_t xi1 = min( xi + 1, arr.w - 1 ); - size_t yi1 = min( yi + 1, arr.h - 1 ); - - float xf = x - xi; - float yf = y - yi; - - T a = arr( xi, yi ); - T b = arr( xi1, yi ); - T c = arr( xi, yi1 ); - T d = arr( xi1, yi1 ); - - T ab = lerp( a, xf, b ); - T cd = lerp( c, xf, d ); - - return lerp( ab, yf, cd ); -} - v3 terrain_normal( const TerrainManager * tm, v3 position ) { ASSERT( position.x >= 0 ); ASSERT( position.y >= 0 );