Fix #34: ortho(), add tests.

This commit is contained in:
treeform 2021-05-08 11:09:22 -07:00
parent 8619b9aea4
commit feca07ded9
2 changed files with 23 additions and 9 deletions

View file

@ -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] =

View file

@ -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(