1 /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
3 Redistribution and use in source and binary forms, with or without modification,
4 are permitted provided that the following conditions are met:
6 * Redistributions of source code must retain the above copyright notice, this
7 list of conditions and the following disclaimer.
8 * Redistributions in binary form must reproduce the above copyright notice,
9 this list of conditions and the following disclaimer in the documentation
10 and/or other materials provided with the distribution.
12 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
13 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
16 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
19 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
23 describe("mat4", function() {
24 var out, matA, matB, identity, result;
26 beforeEach(function() {
27 // Attempting to portray a semi-realistic transform matrix
43 identity = [1, 0, 0, 0,
49 describe("create", function() {
50 beforeEach(function() { result = mat4.create(); });
51 it("should return a 16 element array initialized to a 4x4 identity matrix", function() { expect(result).toBeEqualish(identity); });
54 describe("clone", function() {
55 beforeEach(function() { result = mat4.clone(matA); });
56 it("should return a 16 element array initialized to the values in matA", function() { expect(result).toBeEqualish(matA); });
59 describe("copy", function() {
60 beforeEach(function() { result = mat4.copy(out, matA); });
61 it("should place values into out", function() { expect(out).toBeEqualish(matA); });
62 it("should return out", function() { expect(result).toBe(out); });
65 describe("identity", function() {
66 beforeEach(function() { result = mat4.identity(out); });
67 it("should place values into out", function() { expect(result).toBeEqualish(identity); });
68 it("should return out", function() { expect(result).toBe(out); });
71 describe("transpose", function() {
72 describe("with a separate output matrix", function() {
73 beforeEach(function() { result = mat4.transpose(out, matA); });
75 it("should place values into out", function() {
76 expect(out).toBeEqualish([
83 it("should return out", function() { expect(result).toBe(out); });
84 it("should not modify matA", function() {
85 expect(matA).toBeEqualish([
94 describe("when matA is the output matrix", function() {
95 beforeEach(function() { result = mat4.transpose(matA, matA); });
97 it("should place values into matA", function() {
98 expect(matA).toBeEqualish([
105 it("should return matA", function() { expect(result).toBe(matA); });
109 describe("invert", function() {
110 describe("with a separate output matrix", function() {
111 beforeEach(function() { result = mat4.invert(out, matA); });
113 it("should place values into out", function() {
114 expect(out).toBeEqualish([
121 it("should return out", function() { expect(result).toBe(out); });
122 it("should not modify matA", function() {
123 expect(matA).toBeEqualish([
132 describe("when matA is the output matrix", function() {
133 beforeEach(function() { result = mat4.invert(matA, matA); });
135 it("should place values into matA", function() {
136 expect(matA).toBeEqualish([
143 it("should return matA", function() { expect(result).toBe(matA); });
147 describe("adjoint", function() {
148 describe("with a separate output matrix", function() {
149 beforeEach(function() { result = mat4.adjoint(out, matA); });
151 it("should place values into out", function() {
152 expect(out).toBeEqualish([
159 it("should return out", function() { expect(result).toBe(out); });
160 it("should not modify matA", function() {
161 expect(matA).toBeEqualish([
170 describe("when matA is the output matrix", function() {
171 beforeEach(function() { result = mat4.adjoint(matA, matA); });
173 it("should place values into matA", function() {
174 expect(matA).toBeEqualish([
181 it("should return matA", function() { expect(result).toBe(matA); });
185 describe("determinant", function() {
186 beforeEach(function() { result = mat4.determinant(matA); });
188 it("should return the determinant", function() { expect(result).toEqual(1); });
191 describe("multiply", function() {
192 it("should have an alias called 'mul'", function() { expect(mat4.mul).toEqual(mat4.multiply); });
194 describe("with a separate output matrix", function() {
195 beforeEach(function() { result = mat4.multiply(out, matA, matB); });
197 it("should place values into out", function() {
198 expect(out).toBeEqualish([
205 it("should return out", function() { expect(result).toBe(out); });
206 it("should not modify matA", function() {
207 expect(matA).toBeEqualish([
214 it("should not modify matB", function() {
215 expect(matB).toBeEqualish([
224 describe("when matA is the output matrix", function() {
225 beforeEach(function() { result = mat4.multiply(matA, matA, matB); });
227 it("should place values into matA", function() {
228 expect(matA).toBeEqualish([
235 it("should return matA", function() { expect(result).toBe(matA); });
236 it("should not modify matB", function() {
237 expect(matB).toBeEqualish([
246 describe("when matB is the output matrix", function() {
247 beforeEach(function() { result = mat4.multiply(matB, matA, matB); });
249 it("should place values into matB", function() {
250 expect(matB).toBeEqualish([
257 it("should return matB", function() { expect(result).toBe(matB); });
258 it("should not modify matA", function() {
259 expect(matA).toBeEqualish([
269 describe("translate", function() {
270 describe("with a separate output matrix", function() {
271 beforeEach(function() { result = mat4.translate(out, matA, [4, 5, 6]); });
273 it("should place values into out", function() {
274 expect(out).toBeEqualish([
281 it("should return out", function() { expect(result).toBe(out); });
282 it("should not modify matA", function() {
283 expect(matA).toBeEqualish([
292 describe("when matA is the output matrix", function() {
293 beforeEach(function() { result = mat4.translate(matA, matA, [4, 5, 6]); });
295 it("should place values into matA", function() {
296 expect(matA).toBeEqualish([
303 it("should return matA", function() { expect(result).toBe(matA); });
307 describe("scale", function() {
308 describe("with a separate output matrix", function() {
309 beforeEach(function() { result = mat4.scale(out, matA, [4, 5, 6]); });
311 it("should place values into out", function() {
312 expect(out).toBeEqualish([
319 it("should return out", function() { expect(result).toBe(out); });
320 it("should not modify matA", function() {
321 expect(matA).toBeEqualish([
330 describe("when matA is the output matrix", function() {
331 beforeEach(function() { result = mat4.scale(matA, matA, [4, 5, 6]); });
333 it("should place values into matA", function() {
334 expect(matA).toBeEqualish([
341 it("should return matA", function() { expect(result).toBe(matA); });
345 describe("rotate", function() {
346 var rad = Math.PI * 0.5;
347 var axis = [1, 0, 0];
349 describe("with a separate output matrix", function() {
350 beforeEach(function() { result = mat4.rotate(out, matA, rad, axis); });
352 it("should place values into out", function() {
353 expect(out).toBeEqualish([
355 0, Math.cos(rad), Math.sin(rad), 0,
356 0, -Math.sin(rad), Math.cos(rad), 0,
360 it("should return out", function() { expect(result).toBe(out); });
361 it("should not modify matA", function() {
362 expect(matA).toBeEqualish([
371 describe("when matA is the output matrix", function() {
372 beforeEach(function() { result = mat4.rotate(matA, matA, rad, axis); });
374 it("should place values into matA", function() {
375 expect(matA).toBeEqualish([
377 0, Math.cos(rad), Math.sin(rad), 0,
378 0, -Math.sin(rad), Math.cos(rad), 0,
382 it("should return matA", function() { expect(result).toBe(matA); });
386 describe("rotateX", function() {
387 var rad = Math.PI * 0.5;
389 describe("with a separate output matrix", function() {
390 beforeEach(function() { result = mat4.rotateX(out, matA, rad); });
392 it("should place values into out", function() {
393 expect(out).toBeEqualish([
395 0, Math.cos(rad), Math.sin(rad), 0,
396 0, -Math.sin(rad), Math.cos(rad), 0,
400 it("should return out", function() { expect(result).toBe(out); });
401 it("should not modify matA", function() {
402 expect(matA).toBeEqualish([
411 describe("when matA is the output matrix", function() {
412 beforeEach(function() { result = mat4.rotateX(matA, matA, rad); });
414 it("should place values into matA", function() {
415 expect(matA).toBeEqualish([
417 0, Math.cos(rad), Math.sin(rad), 0,
418 0, -Math.sin(rad), Math.cos(rad), 0,
422 it("should return matA", function() { expect(result).toBe(matA); });
426 describe("rotateY", function() {
427 var rad = Math.PI * 0.5;
429 describe("with a separate output matrix", function() {
430 beforeEach(function() { result = mat4.rotateY(out, matA, rad); });
432 it("should place values into out", function() {
433 expect(out).toBeEqualish([
434 Math.cos(rad), 0, -Math.sin(rad), 0,
436 Math.sin(rad), 0, Math.cos(rad), 0,
440 it("should return out", function() { expect(result).toBe(out); });
441 it("should not modify matA", function() {
442 expect(matA).toBeEqualish([
451 describe("when matA is the output matrix", function() {
452 beforeEach(function() { result = mat4.rotateY(matA, matA, rad); });
454 it("should place values into matA", function() {
455 expect(matA).toBeEqualish([
456 Math.cos(rad), 0, -Math.sin(rad), 0,
458 Math.sin(rad), 0, Math.cos(rad), 0,
462 it("should return matA", function() { expect(result).toBe(matA); });
466 describe("rotateZ", function() {
467 var rad = Math.PI * 0.5;
469 describe("with a separate output matrix", function() {
470 beforeEach(function() { result = mat4.rotateZ(out, matA, rad); });
472 it("should place values into out", function() {
473 expect(out).toBeEqualish([
474 Math.cos(rad), Math.sin(rad), 0, 0,
475 -Math.sin(rad), Math.cos(rad), 0, 0,
480 it("should return out", function() { expect(result).toBe(out); });
481 it("should not modify matA", function() {
482 expect(matA).toBeEqualish([
491 describe("when matA is the output matrix", function() {
492 beforeEach(function() { result = mat4.rotateZ(matA, matA, rad); });
494 it("should place values into matA", function() {
495 expect(matA).toBeEqualish([
496 Math.cos(rad), Math.sin(rad), 0, 0,
497 -Math.sin(rad), Math.cos(rad), 0, 0,
502 it("should return matA", function() { expect(result).toBe(matA); });
506 // TODO: fromRotationTranslation
508 describe("frustum", function() {
509 beforeEach(function() { result = mat4.frustum(out, -1, 1, -1, 1, -1, 1); });
510 it("should place values into out", function() { expect(result).toBeEqualish([
517 it("should return out", function() { expect(result).toBe(out); });
520 describe("perspective", function() {
521 var fovy = Math.PI * 0.5;
522 beforeEach(function() { result = mat4.perspective(out, fovy, 1, 0, 1); });
523 it("should place values into out", function() { expect(result).toBeEqualish([
530 it("should return out", function() { expect(result).toBe(out); });
533 describe("ortho", function() {
534 beforeEach(function() { result = mat4.ortho(out, -1, 1, -1, 1, -1, 1); });
535 it("should place values into out", function() { expect(result).toBeEqualish([
542 it("should return out", function() { expect(result).toBe(out); });
545 describe("lookAt", function() {
547 var center = [0, 0, -1];
550 beforeEach(function() { result = mat4.lookAt(out, eye, center, up); });
551 it("should place values into out", function() { expect(result).toBeEqualish([
558 it("should return out", function() { expect(result).toBe(out); });
561 describe("str", function() {
562 beforeEach(function() { result = mat4.str(matA); });
564 it("should return a string representation of the matrix", function() { expect(result).toEqual("mat4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 2, 3, 1)"); });