Skip to content

Commit

Permalink
Added to_frustum test
Browse files Browse the repository at this point in the history
  • Loading branch information
karai17 committed Jul 24, 2016
1 parent 375f748 commit 2cb5d09
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 42 deletions.
11 changes: 6 additions & 5 deletions modules/mat4.lua
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ function mat4.to_frustum(a, infinite)
local frustum = {}

-- Extract the LEFT plane
frustum.left = {}
frustum.left.a = a[4] + a[1]
frustum.left.b = a[8] + a[5]
frustum.left.c = a[12] + a[9]
Expand All @@ -562,7 +563,7 @@ function mat4.to_frustum(a, infinite)
frustum.left.d = frustum.left.d / t

-- Extract the RIGHT plane
frustum.right = {}
frustum.right = {}
frustum.right.a = a[4] - a[1]
frustum.right.b = a[8] - a[5]
frustum.right.c = a[12] - a[9]
Expand All @@ -576,7 +577,7 @@ function mat4.to_frustum(a, infinite)
frustum.right.d = frustum.right.d / t

-- Extract the BOTTOM plane
frustum.bottom = {}
frustum.bottom = {}
frustum.bottom.a = a[4] + a[2]
frustum.bottom.b = a[8] + a[6]
frustum.bottom.c = a[12] + a[10]
Expand All @@ -590,7 +591,7 @@ function mat4.to_frustum(a, infinite)
frustum.bottom.d = frustum.bottom.d / t

-- Extract the TOP plane
frustum.top = {}
frustum.top = {}
frustum.top.a = a[4] - a[2]
frustum.top.b = a[8] - a[6]
frustum.top.c = a[12] - a[10]
Expand All @@ -604,7 +605,7 @@ function mat4.to_frustum(a, infinite)
frustum.top.d = frustum.top.d / t

-- Extract the NEAR plane
frustum.near = {}
frustum.near = {}
frustum.near.a = a[4] + a[3]
frustum.near.b = a[8] + a[7]
frustum.near.c = a[12] + a[11]
Expand All @@ -619,7 +620,7 @@ function mat4.to_frustum(a, infinite)

if not infinite then
-- Extract the FAR plane
frustum.far = {}
frustum.far = {}
frustum.far.a = a[4] - a[3]
frustum.far.b = a[8] - a[7]
frustum.far.c = a[12] - a[11]
Expand Down
120 changes: 83 additions & 37 deletions spec/mat4_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ describe("mat4:", function()
end)

it("tests multiplication", function()
-- 4x4 * 4x4
do
local a = mat4 {
1, 2, 3, 4,
Expand All @@ -117,23 +118,21 @@ describe("mat4:", function()
assert.is.equal(c[2], 70)
assert.is.equal(c[3], 110)
assert.is.equal(c[4], 150)

assert.is.equal(c[5], 70)
assert.is.equal(c[6], 174)
assert.is.equal(c[7], 278)
assert.is.equal(c[8], 382)

assert.is.equal(c[9], 110)
assert.is.equal(c[10], 278)
assert.is.equal(c[11], 446)
assert.is.equal(c[12], 614)

assert.is.equal(c[13], 150)
assert.is.equal(c[14], 382)
assert.is.equal(c[15], 614)
assert.is.equal(c[16], 846)
end

-- 4x4 * 4x1
do
local a = mat4 {
1, 2, 3, 4,
Expand All @@ -153,13 +152,15 @@ describe("mat4:", function()
it("tests transforms", function()
local a = mat4()

-- Scale matrix
do
local b = mat4():scale(a, vec3(5, 5, 5))
assert.is.equal(b[1], 5)
assert.is.equal(b[6], 5)
assert.is.equal(b[11], 5)
end

-- Rotate matrix
do
local b = mat4():rotate(a, math.rad(45), vec3.unit_z)
assert.is_true(utils.tolerance( 0.7071-b[1], 0.001))
Expand All @@ -168,6 +169,7 @@ describe("mat4:", function()
assert.is_true(utils.tolerance( 0.7071-b[6], 0.001))
end

-- Translate matrix
do
local b = mat4():translate(a, vec3(5, 5, 5))
assert.is.equal(b[13], 5)
Expand Down Expand Up @@ -240,11 +242,13 @@ describe("mat4:", function()
local v = vec3(0, 0, 10)
local vp = { 0, 0, 400, 400 }

-- Project to pixel space
local c = mat4.project(v, a, b, vp)
assert.is.equal(c.x, 200)
assert.is.equal(c.y, 200)
assert.is_true(utils.tolerance(1.0101-c.z, 0.001))

-- Unproject to vector space
local d = mat4.unproject(c, a, b, vp)
assert.is_true(utils.tolerance(v.x-d.x, 0.001))
assert.is_true(utils.tolerance(v.y-d.y, 0.001))
Expand All @@ -253,40 +257,83 @@ describe("mat4:", function()

it("tests convertions", function()
local a = mat4()
local v = a:to_vec4s()
assert.is_true(type(v) == "table")
assert.is_true(type(v[1]) == "table")
assert.is_true(type(v[2]) == "table")
assert.is_true(type(v[3]) == "table")
assert.is_true(type(v[4]) == "table")
assert.is.equal(v[1][1], 1)
assert.is.equal(v[1][2], 0)
assert.is.equal(v[1][3], 0)
assert.is.equal(v[1][4], 0)
assert.is.equal(v[2][1], 0)
assert.is.equal(v[2][2], 1)
assert.is.equal(v[2][3], 0)
assert.is.equal(v[2][4], 0)
assert.is.equal(v[3][1], 0)
assert.is.equal(v[3][2], 0)
assert.is.equal(v[3][3], 1)
assert.is.equal(v[3][4], 0)
assert.is.equal(v[4][1], 0)
assert.is.equal(v[4][2], 0)
assert.is.equal(v[4][3], 0)
assert.is.equal(v[4][4], 1)

local b = mat4 {
0, 0, 1, 0,
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 0, 0
}
local q = b:to_quat()
assert.is.equal(q.x, -0.5)
assert.is.equal(q.y, -0.5)
assert.is.equal(q.z, -0.5)
assert.is.equal(q.w, 0.5)
-- Convert to vec4s
do
local v = a:to_vec4s()
assert.is_true(type(v) == "table")
assert.is_true(type(v[1]) == "table")
assert.is_true(type(v[2]) == "table")
assert.is_true(type(v[3]) == "table")
assert.is_true(type(v[4]) == "table")
assert.is.equal(v[1][1], 1)
assert.is.equal(v[1][2], 0)
assert.is.equal(v[1][3], 0)
assert.is.equal(v[1][4], 0)
assert.is.equal(v[2][1], 0)
assert.is.equal(v[2][2], 1)
assert.is.equal(v[2][3], 0)
assert.is.equal(v[2][4], 0)
assert.is.equal(v[3][1], 0)
assert.is.equal(v[3][2], 0)
assert.is.equal(v[3][3], 1)
assert.is.equal(v[3][4], 0)
assert.is.equal(v[4][1], 0)
assert.is.equal(v[4][2], 0)
assert.is.equal(v[4][3], 0)
assert.is.equal(v[4][4], 1)
end

-- Convert to quaternion
do
local b = mat4 {
0, 0, 1, 0,
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 0, 0
}
local q = b:to_quat()
assert.is.equal(q.x, -0.5)
assert.is.equal(q.y, -0.5)
assert.is.equal(q.z, -0.5)
assert.is.equal(q.w, 0.5)
end

-- Convert to frustum
do
local b = mat4.from_perspective(45, 1, 0.1, 1000)
local f = mat4():mul(b, a):to_frustum()

assert.is_true(utils.tolerance( 0.9239-f.left.a, 0.001))
assert.is_true(utils.tolerance( 0 -f.left.b, 0.001))
assert.is_true(utils.tolerance(-0.3827-f.left.c, 0.001))
assert.is_true(utils.tolerance( 0 -f.left.d, 0.001))

assert.is_true(utils.tolerance(-0.9239-f.right.a, 0.001))
assert.is_true(utils.tolerance( 0 -f.right.b, 0.001))
assert.is_true(utils.tolerance(-0.3827-f.right.c, 0.001))
assert.is_true(utils.tolerance( 0 -f.right.d, 0.001))

assert.is_true(utils.tolerance( 0 -f.bottom.a, 0.001))
assert.is_true(utils.tolerance( 0.9239-f.bottom.b, 0.001))
assert.is_true(utils.tolerance(-0.3827-f.bottom.c, 0.001))
assert.is_true(utils.tolerance( 0 -f.bottom.d, 0.001))

assert.is_true(utils.tolerance( 0 -f.top.a, 0.001))
assert.is_true(utils.tolerance(-0.9239-f.top.b, 0.001))
assert.is_true(utils.tolerance(-0.3827-f.top.c, 0.001))
assert.is_true(utils.tolerance( 0 -f.top.d, 0.001))

assert.is_true(utils.tolerance( 0 -f.near.a, 0.001))
assert.is_true(utils.tolerance( 0 -f.near.b, 0.001))
assert.is_true(utils.tolerance(-1 -f.near.c, 0.001))
assert.is_true(utils.tolerance(-0.1-f.near.d, 0.001))

assert.is_true(utils.tolerance( 0 -f.far.a, 0.001))
assert.is_true(utils.tolerance( 0 -f.far.b, 0.001))
assert.is_true(utils.tolerance( 1 -f.far.c, 0.001))
assert.is_true(utils.tolerance( 1000-f.far.d, 0.001))
end
end)
end)

Expand All @@ -298,5 +345,4 @@ end)
from_perspective
from_hmd_perspective
look_at
to_frustum
--]]

0 comments on commit 2cb5d09

Please sign in to comment.