Skip to content

Commit

Permalink
feat: add mat4 to glsl unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
bhouston committed Jul 29, 2020
1 parent 4d023bc commit 1403c01
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 10 deletions.
1 change: 0 additions & 1 deletion src/lib/shaders/includes/math/mat3.test.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,4 @@ void tests( inout TestSuite suite ) {
manual[2][2] = 8.;
testMatEquals( suite, 120, manual, reference );

mat3 rot90 = mat3RotateZ( degToRad( 90. ) );
}
18 changes: 9 additions & 9 deletions src/lib/shaders/includes/math/mat4.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@ mat4 mat4Identity() {
0., 0., 0., 1. );
}

mat4 mat4RotateZDirection( vec2 dir ){
mat4 mat4RotateXDirection( vec2 dir ){
return mat4(
vec4( 1., 0., 0., 0. ),
vec4( 0., dir.x, -dir.y, 0., 0. ),
vec4( 0., dir.y, dir.x, 0., 0. ),
vec4( 0., 0., 0., 1. )
vec4( 0., dir.x, -dir.y, 0. ),
vec4( 0., dir.y, dir.x, 0. ),
vec4( 0., 0., 0., 1. ) );
}

mat4 mat4RotateYDirection( vec2 dir ){
return mat4(
vec4( dir.x, 0., -dir.y, 0. ),
vec4( dir.x, 0., dir.y, 0. ),
vec4( 0., 1., 0., 0. ),
vec4( dir.y, 0., dir.x, 0. ),
vec4( 0., 0., 0., 1. )
vec4( -dir.y, 0., dir.x, 0. ),
vec4( 0., 0., 0., 1. ) );
}

mat4 mat4RotateZDirection( vec2 dir ){
return mat4(
vec4( dir.x, -dir.y, 0., 0. ),
vec4( dir.y, dir.x, 0., 0. ),
vec4( 0., 0., 1., 0. ),
vec4( 0., 0., 0., 1. )
vec4( 0., 0., 0., 1. ) );
}

mat4 mat4RotateX( float angle ){
Expand Down Expand Up @@ -65,5 +65,5 @@ vec3 mat4TransformDirection( in mat4 m, in vec3 dir ) {
vec3 mat4UntransformDirection( in mat4 m, in vec3 dir ) {
// dir can be either a direction vector or a normal vector
// upper-left 3x3 of matrix is assumed to be orthogonal
return normalize( ( vec4( dir, 0. ) * matrix ).xyz );
return normalize( ( vec4( dir, 0. ) * m ).xyz );
}
49 changes: 49 additions & 0 deletions src/lib/shaders/includes/math/mat4.test.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#pragma include <tests/fragment>
#pragma include "mat4"
#pragma include <math/math>

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

void tests( inout TestSuite suite ) {

mat4 uninitialized;

mat4 zero = mat4(
0., 0., 0., 0.,
0., 0., 0., 0.,
0., 0., 0., 0.,
0., 0., 0., 0. );
testMatEquals( suite, 40, uninitialized, zero );

mat4 reference = mat4(
0., 1., 2., 3.,
4., 5., 6., 7.,
8., 9., 10., 11.,
12., 13., 14., 15. );

mat4 refByIdentity = reference * mat4Identity();
testMatEquals( suite, 80, refByIdentity, reference );

mat4 vec = mat4(
vec4( 0., 1., 2., 3. ),
vec4( 4., 5., 6., 7. ),
vec4( 8., 9., 10., 11. ),
vec4( 12., 13., 14., 15. ) );
testMatEquals( suite, 100, vec, reference );

mat4 manual;
manual[0] = vec4( 0., 1., 2., 3. );
manual[1].zyxw = vec4( 6., 5., 4., 7. );
manual[2].x = 8.;
manual[2][1] = 9.;
manual[2][2] = 10.;
manual[2][3] = 11.;
manual[3] = vec4( 12., 13., 14., 15. );
testMatEquals( suite, 120, manual, reference );
}
5 changes: 5 additions & 0 deletions src/lib/shaders/testSuites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import cubeFacesTests from "./includes/cubemaps/cubeFaces.test.glsl";
import latLongTests from "./includes/cubemaps/latLong.test.glsl";
import mat2Tests from "./includes/math/mat2.test.glsl";
import mat3Tests from "./includes/math/mat3.test.glsl";
import mat4Tests from "./includes/math/mat4.test.glsl";
import mathTests from "./includes/math/math.test.glsl";
import unitIntervalPackingTests from "./includes/math/unitIntervalPacking.test.glsl";
import normalPackingTests from "./includes/normals/normalPacking.test.glsl";
Expand Down Expand Up @@ -52,6 +53,10 @@ export const glslTestSuites: Array<GLSLTestSuite> = [
name: "mat3",
source: mat3Tests,
},
{
name: "mat4",
source: mat4Tests,
},
{
name: "normalPacking",
source: normalPackingTests,
Expand Down

0 comments on commit 1403c01

Please sign in to comment.