summaryrefslogtreecommitdiff
path: root/demo/Matrix.lua
blob: 11b704c9ea91277cf99f0dfbe56de4c38d740bf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
local Mat3 = {}

Mat3.index = function(i, j)
    return (i-1) + 3*(j-1)
end

Mat3.tostring = function(mat)
    local str = '[ ['..
        tostring(honey.cglm.get_value(mat, 0))..', '..
        tostring(honey.cglm.get_value(mat, 3))..', '..
        tostring(honey.cglm.get_value(mat, 6))..']\n  ['..
        tostring(honey.cglm.get_value(mat, 1))..', '..
        tostring(honey.cglm.get_value(mat, 4))..', '..
        tostring(honey.cglm.get_value(mat, 7))..']\n  ['..
        tostring(honey.cglm.get_value(mat, 2))..', '..
        tostring(honey.cglm.get_value(mat, 5))..', '..
        tostring(honey.cglm.get_value(mat, 8))..'] ]'
    return str
end

Mat3.new = function(tbl)
    if tbl == nil then
        return honey.cglm.new_array_zero(9)
    end
    
    if #tbl ~= 9 then
        error('3x3 matrices require exactly nine elements!')
    end
    
    local mat3 = honey.cglm.new_array_zero(9)
    for i = 0,8 do
        honey.cglm.set_value(mat3, i, tbl[i+1])
    end

    return mat3
end

-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

local Mat4 = {}

Mat4.index = function(i, j)
    return (i-1) + 4*(j-1)
end

Mat4.tostring = function(matrix)
    str = ''
    for i = 1,4 do
        for j = 1,4 do
            str = str..tostring(honey.cglm.get_value(matrix, Mat4.index(i,j)))..', '
        end
        str = str..'\n'
    end
    return str
end

Mat4.new = function(tbl)
    if tbl == nil then
        return honey.cglm.new_array_zero(16)
    end
    
    if #tbl ~= 16 then
        error('4x4 matrices require exactly 16 elements!')
    end
    
    local mat4 = honey.cglm.new_array_zero(16)
    honey.cglm.set_value(mat4, 0, tbl[1])
    honey.cglm.set_value(mat4, 1, tbl[5])
    honey.cglm.set_value(mat4, 2, tbl[9])
    honey.cglm.set_value(mat4, 3, tbl[13])
    honey.cglm.set_value(mat4, 4, tbl[2])
    honey.cglm.set_value(mat4, 5, tbl[6])
    honey.cglm.set_value(mat4, 6, tbl[10])
    honey.cglm.set_value(mat4, 7, tbl[14])
    honey.cglm.set_value(mat4, 8, tbl[3])
    honey.cglm.set_value(mat4, 9, tbl[7])
    honey.cglm.set_value(mat4, 10, tbl[11])
    honey.cglm.set_value(mat4, 11, tbl[15])
    honey.cglm.set_value(mat4, 12, tbl[4])
    honey.cglm.set_value(mat4, 13, tbl[8])
    honey.cglm.set_value(mat4, 14, tbl[12])
    honey.cglm.set_value(mat4, 15, tbl[16])

    setmetatable(mat4, Mat4.mt)

    return mat4
end


return function() return Mat3, Mat4 end