From feca07ded9a2ba680f1e6329769fa338cd90db5a Mon Sep 17 00:00:00 2001 From: treeform Date: Sat, 8 May 2021 11:09:22 -0700 Subject: [PATCH] Fix #34: ortho(), add tests. --- src/vmath.nim | 18 +++++++++--------- tests/test.nim | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/vmath.nim b/src/vmath.nim index 6457e75..8366d13 100644 --- a/src/vmath.nim +++ b/src/vmath.nim @@ -1116,28 +1116,28 @@ proc perspective*[T](fovy, aspect, near, far: T): GMat4[T] = proc ortho*[T](left, right, bottom, top, near, far: T): GMat4[T] = ## Create an orthographic matrix. let - rl = (right - left) - tb = (top - bottom) - fn = (far - near) + rl: T = (right - left) + tb: T = (top - bottom) + fn: T = (far - near) - result[0, 0] = 2 / rl + result[0, 0] = T(2 / rl) result[0, 1] = 0 result[0, 2] = 0 result[0, 3] = 0 result[1, 0] = 0 - result[1, 1] = 2 / tb + result[1, 1] = T(2 / tb) result[1, 2] = 0 result[1, 3] = 0 result[2, 0] = 0 result[2, 1] = 0 - result[2, 2] = -2 / fn + result[2, 2] = T(-2 / fn) result[2, 3] = 0 - result[3, 0] = -(left + right) / rl - result[3, 1] = -(top + bottom) / tb - result[3, 2] = -(far + near) / fn + result[3, 0] = T(-(left + right) / rl) + result[3, 1] = T(-(top + bottom) / tb) + result[3, 2] = T(-(far + near) / fn) result[3, 3] = 1 proc lookAt*[T](eye, center, up: GVec3[T]): GMat4[T] = diff --git a/tests/test.nim b/tests/test.nim index ab6c964..87c4566 100644 --- a/tests/test.nim +++ b/tests/test.nim @@ -578,6 +578,20 @@ block: ) doAssert lookAt(vec3(0, 0, 1), vec3(0, 0, 0)).quat ~= quat(0.0, 0.0, 0.0, 1.0) + doAssert ortho[float32](-1, 1, 1, -1, -1000, 1000) ~= mat4( + 1.0, 0.0, 0.0, 0.0, + 0.0, -1.0, 0.0, 0.0, + 0.0, 0.0, -0.001000000047497451, + 0.0, -0.0, 0.0, -0.0, 1.0 + ) + + doAssert perspective[float32](75, 1.666, 1, 1000) ~= mat4( + 0.7822480201721191, 0.0, 0.0, 0.0, + 0.0, 1.30322527885437, 0.0, 0.0, + 0.0, 0.0, -1.002002000808716, -1.0, + 0.0, 0.0, -2.002002000808716, 0.0 + ) + # Test super random quat test. for i in 0 ..< 1000: var m1 = rotate(