summaryrefslogtreecommitdiff
path: root/demo/Vector.lua
blob: 89fd26aa5bed31a1ed5efffe044790143fadc43e (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
local Vec3 = {}

Vec3.tostring = function(vec3)
    local str = '['..
        tostring(honey.cglm.get_value(vec3, 0))..','..
        tostring(honey.cglm.get_value(vec3, 1))..','..
        tostring(honey.cglm.get_value(vec3, 2))..']'
    return str
end

Vec3.new = function(tbl)
    if tbl == nil then
        return honey.cglm.new_array_zero(3)
    end
    
    if #tbl ~= 3 then
        error('3-vectors require exactly three elements!')
    end
    
    local vec3 = honey.cglm.new_array_zero(3)
    for i = 0,2 do
        honey.cglm.set_value(vec3, i, tbl[i+1])
    end

    return vec3
end

Vec3.ZERO   = Vec3.new()
Vec3.X_UNIT = Vec3.new{1, 0, 0}
Vec3.Y_UNIT = Vec3.new{0, 1, 0}
Vec3.Z_UNIT = Vec3.new{0, 0, 1}

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

local Vec4 = {}

Vec4.tostring = function(vec4)
    local str = '['..
        tostring(honey.cglm.get_value(vec4, 0))..','..
        tostring(honey.cglm.get_value(vec4, 1))..','..
        tostring(honey.cglm.get_value(vec4, 2))..','..
        tostring(honey.cglm.get_value(vec4, 3))..']'
    return str
end

Vec4.new = function(tbl)
    if tbl == nil then
        return honey.cglm.new_array_zero(4)
    end
    
    if #tbl ~= 4 then
        error('4-vectors require exactly four elements!')
    end
    
    local vec4 = honey.cglm.new_array_zero(4)
    for i = 0,3 do
        honey.cglm.set_value(vec4, i, tbl[i+1])
    end

    return vec4
end


return function() return Vec3, Vec4 end