diff --git a/src/lib/shaders/includes/math/mat2.glsl b/src/lib/shaders/includes/math/mat2.glsl new file mode 100644 index 00000000..92a30ccd --- /dev/null +++ b/src/lib/shaders/includes/math/mat2.glsl @@ -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 ); +} diff --git a/src/lib/shaders/includes/math/mat2.test.glsl b/src/lib/shaders/includes/math/mat2.test.glsl new file mode 100644 index 00000000..27ba3941 --- /dev/null +++ b/src/lib/shaders/includes/math/mat2.test.glsl @@ -0,0 +1,32 @@ +#pragma include +#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 ); + +}