Skip to content

Commit

Permalink
feat: add degToRad, radToDeg in glsl with unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bhouston committed Jul 29, 2020
1 parent ff0e595 commit a6237eb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/lib/shaders/includes/math/math.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ const float EPSILON = 1e-6;

float saturate( const in float a ) { return clamp( a, 0., 1. ); }
vec3 saturate( const in vec3 a ) { return clamp( a, 0., 1. ); }
vec3 whiteComplement( const in vec3 a ) { return 1.0 - saturate(a); }
vec3 whiteComplement( const in vec3 a ) { return 1. - saturate(a); }
float pow2( const in float x ) { return x*x; }
float pow3( const in float x ) { return x*x*x; }
float pow4( const in float x ) { float x2 = x*x; return x2*x2; }
float average( const in vec3 color ) { return dot( color, vec3( 0.333333333333 ) ); }
float degToRad( const in float deg ) { return deg * PI / 180.; }
float radToDeg( const in float rad ) { return rad * 180. / PI; }

const float NAN = sqrt( 0. );
bool isnan( const float x ) {
Expand Down
12 changes: 12 additions & 0 deletions src/lib/shaders/includes/math/math.test.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,16 @@ void tests( inout TestSuite suite ) {
// should be undefined according to spec: https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/atan.xhtml
assert( suite, 126, atan( 1., 0. ) == PI_HALF );

assert( suite, 200, eqAbs( degToRad( 0. ), PI * 0., 0.000001 ) );
assert( suite, 201, eqAbs( degToRad( 90. ), PI * 0.5, 0.000001 ) );
assert( suite, 202, eqAbs( degToRad( 180. ), PI * 1., 0.000001 ) );
assert( suite, 203, eqAbs( degToRad( 270. ), PI * 1.5, 0.000001 ) );
assert( suite, 204, eqAbs( degToRad( 360. ), PI * 2., 0.000001 ) );

assert( suite, 205, eqAbs( radToDeg( degToRad( 0. ) ), 0., 0.000001 ) );
assert( suite, 206, eqAbs( radToDeg( degToRad( 90. ) ), 90., 0.000001 ) );
assert( suite, 207, eqAbs( radToDeg( degToRad( 180. ) ), 180., 0.000001 ) );
assert( suite, 208, eqAbs( radToDeg( degToRad( 270. ) ), 270., 0.000001 ) );
assert( suite, 209, eqAbs( radToDeg( degToRad( 360. ) ), 360., 0.000001 ) );

}

0 comments on commit a6237eb

Please sign in to comment.