Wed, 31 Dec 2014 13:27:57 +0100
Ignore runtime configuration files generated during quality assurance.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | "use strict"; |
michael@0 | 4 | |
michael@0 | 5 | function test() { |
michael@0 | 6 | let v1 = vec3.create(); |
michael@0 | 7 | |
michael@0 | 8 | ok(v1, "Should have created a vector with vec3.create()."); |
michael@0 | 9 | is(v1.length, 3, "A vec3 should have 3 elements."); |
michael@0 | 10 | |
michael@0 | 11 | ok(isApproxVec(v1, [0, 0, 0]), |
michael@0 | 12 | "When created, a vec3 should have the values default to 0."); |
michael@0 | 13 | |
michael@0 | 14 | vec3.set([1, 2, 3], v1); |
michael@0 | 15 | ok(isApproxVec(v1, [1, 2, 3]), |
michael@0 | 16 | "The vec3.set() function didn't set the values correctly."); |
michael@0 | 17 | |
michael@0 | 18 | vec3.zero(v1); |
michael@0 | 19 | ok(isApproxVec(v1, [0, 0, 0]), |
michael@0 | 20 | "The vec3.zero() function didn't set the values correctly."); |
michael@0 | 21 | |
michael@0 | 22 | let v2 = vec3.create([4, 5, 6]); |
michael@0 | 23 | ok(isApproxVec(v2, [4, 5, 6]), |
michael@0 | 24 | "When cloning arrays, a vec3 should have the values copied."); |
michael@0 | 25 | |
michael@0 | 26 | let v3 = vec3.create(v2); |
michael@0 | 27 | ok(isApproxVec(v3, [4, 5, 6]), |
michael@0 | 28 | "When cloning vectors, a vec3 should have the values copied."); |
michael@0 | 29 | |
michael@0 | 30 | vec3.add(v2, v3); |
michael@0 | 31 | ok(isApproxVec(v2, [8, 10, 12]), |
michael@0 | 32 | "The vec3.add() function didn't set the x value correctly."); |
michael@0 | 33 | |
michael@0 | 34 | vec3.subtract(v2, v3); |
michael@0 | 35 | ok(isApproxVec(v2, [4, 5, 6]), |
michael@0 | 36 | "The vec3.subtract() function didn't set the values correctly."); |
michael@0 | 37 | |
michael@0 | 38 | vec3.negate(v2); |
michael@0 | 39 | ok(isApproxVec(v2, [-4, -5, -6]), |
michael@0 | 40 | "The vec3.negate() function didn't set the values correctly."); |
michael@0 | 41 | |
michael@0 | 42 | vec3.scale(v2, -2); |
michael@0 | 43 | ok(isApproxVec(v2, [8, 10, 12]), |
michael@0 | 44 | "The vec3.scale() function didn't set the values correctly."); |
michael@0 | 45 | |
michael@0 | 46 | vec3.normalize(v1); |
michael@0 | 47 | ok(isApproxVec(v1, [0, 0, 0]), |
michael@0 | 48 | "Normalizing a vector with zero length should return [0, 0, 0]."); |
michael@0 | 49 | |
michael@0 | 50 | vec3.normalize(v2); |
michael@0 | 51 | ok(isApproxVec(v2, [ |
michael@0 | 52 | 0.4558423161506653, 0.5698028802871704, 0.6837634444236755 |
michael@0 | 53 | ]), "The vec3.normalize() function didn't set the values correctly."); |
michael@0 | 54 | |
michael@0 | 55 | vec3.cross(v2, v3); |
michael@0 | 56 | ok(isApproxVec(v2, [ |
michael@0 | 57 | 5.960464477539063e-8, -1.1920928955078125e-7, 5.960464477539063e-8 |
michael@0 | 58 | ]), "The vec3.cross() function didn't set the values correctly."); |
michael@0 | 59 | |
michael@0 | 60 | vec3.dot(v2, v3); |
michael@0 | 61 | ok(isApproxVec(v2, [ |
michael@0 | 62 | 5.960464477539063e-8, -1.1920928955078125e-7, 5.960464477539063e-8 |
michael@0 | 63 | ]), "The vec3.dot() function didn't set the values correctly."); |
michael@0 | 64 | |
michael@0 | 65 | ok(isApproxVec([vec3.length(v2)], [1.4600096599955427e-7]), |
michael@0 | 66 | "The vec3.length() function didn't calculate the value correctly."); |
michael@0 | 67 | |
michael@0 | 68 | vec3.direction(v2, v3); |
michael@0 | 69 | ok(isApproxVec(v2, [ |
michael@0 | 70 | -0.4558422863483429, -0.5698028802871704, -0.6837634444236755 |
michael@0 | 71 | ]), "The vec3.direction() function didn't set the values correctly."); |
michael@0 | 72 | |
michael@0 | 73 | vec3.lerp(v2, v3, 0.5); |
michael@0 | 74 | ok(isApproxVec(v2, [ |
michael@0 | 75 | 1.7720788717269897, 2.2150986194610596, 2.65811824798584 |
michael@0 | 76 | ]), "The vec3.lerp() function didn't set the values correctly."); |
michael@0 | 77 | |
michael@0 | 78 | |
michael@0 | 79 | vec3.project([100, 100, 10], [0, 0, 100, 100], |
michael@0 | 80 | mat4.create(), mat4.perspective(45, 1, 0.1, 100), v1); |
michael@0 | 81 | ok(isApproxVec(v1, [-1157.10693359375, 1257.10693359375, 0]), |
michael@0 | 82 | "The vec3.project() function didn't set the values correctly."); |
michael@0 | 83 | |
michael@0 | 84 | vec3.unproject([100, 100, 1], [0, 0, 100, 100], |
michael@0 | 85 | mat4.create(), mat4.perspective(45, 1, 0.1, 100), v1); |
michael@0 | 86 | ok(isApproxVec(v1, [ |
michael@0 | 87 | 41.420406341552734, -41.420406341552734, -99.99771118164062 |
michael@0 | 88 | ]), "The vec3.project() function didn't set the values correctly."); |
michael@0 | 89 | |
michael@0 | 90 | |
michael@0 | 91 | let ray = vec3.createRay([10, 10, 0], [100, 100, 1], [0, 0, 100, 100], |
michael@0 | 92 | mat4.create(), mat4.perspective(45, 1, 0.1, 100)); |
michael@0 | 93 | |
michael@0 | 94 | ok(isApproxVec(ray.origin, [ |
michael@0 | 95 | -0.03313708305358887, 0.03313708305358887, -0.1000000014901161 |
michael@0 | 96 | ]), "The vec3.createRay() function didn't create the position correctly."); |
michael@0 | 97 | ok(isApproxVec(ray.direction, [ |
michael@0 | 98 | 0.35788586614428364, -0.35788586614428364, -0.862458934459091 |
michael@0 | 99 | ]), "The vec3.createRay() function didn't create the direction correctly."); |
michael@0 | 100 | |
michael@0 | 101 | |
michael@0 | 102 | is(vec3.str([0, 0, 0]), "[0, 0, 0]", |
michael@0 | 103 | "The vec3.str() function didn't work properly."); |
michael@0 | 104 | } |