Skip to content

Commit

Permalink
feat: mat2 glsl helpers with tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
bhouston committed Jul 29, 2020
1 parent c864909 commit 978dde2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/lib/shaders/includes/math/mat2.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

mat2 mat2Identity() {
return mat2( 1., 0., 0., 1. );
}

// https://thebookofshaders.com/08/
mat2 mat2Rotate(float angle){
float c = cos(angle);
float s = sin(angle);

return mat2( c, -s, s, c );
}
32 changes: 32 additions & 0 deletions src/lib/shaders/includes/math/mat2.test.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma include <tests/fragment>
#pragma include "mat2"

void testMatEquals( inout TestSuite suite, int id, mat2 m, mat2 target ) {
for( int i = 0; i < 2; i ++ ) {
for( int j = 0; j < 2; j ++ ) {
assert( suite, id + i*2 + j, m[i][j] == target[i][j] );
}
}
}

void tests( inout TestSuite suite ) {

mat2 uninitialized;

mat2 zero = mat2( 0., 0., 0., 0. );
testMatEquals( suite, 10, uninitialized, zero );

mat2 reference = mat2( 0., 1., 2., 3. );

mat2 refByIdentity = reference * mat2Identity();
testMatEquals( suite, 30, refByIdentity, reference );

mat2 vec = mat2( vec2( 0., 1. ), vec2( 2., 3. ) );
testMatEquals( suite, 40, vec, reference );

mat2 manual;
manual[0] = vec2( 0., 1. );
manual[1].yx = vec2( 3., 2. );
testMatEquals( suite, 50, manual, reference );

}

0 comments on commit 978dde2

Please sign in to comment.