summaryrefslogtreecommitdiff
path: root/honey/vec3.lua
diff options
context:
space:
mode:
Diffstat (limited to 'honey/vec3.lua')
-rw-r--r--honey/vec3.lua170
1 files changed, 170 insertions, 0 deletions
diff --git a/honey/vec3.lua b/honey/vec3.lua
new file mode 100644
index 0000000..af9ac4b
--- /dev/null
+++ b/honey/vec3.lua
@@ -0,0 +1,170 @@
+local glm = honey.glm
+
+local vec3 = {}
+setmetatable(vec3, {__index=_G})
+setfenv(1, vec3)
+
+
+Vec3 = {}
+
+function Vec3.new(_, values)
+ local self = {}
+ self.data = glm.vec3_create()
+ setmetatable(self, Vec3)
+ if values then
+ self[1] = values[1]
+ self[2] = values[2]
+ self[3] = values[3]
+ end
+ return self
+end
+setmetatable(Vec3, {__call=Vec3.new})
+
+
+function Vec3.__index(self, key)
+ if type(key) == 'number' then
+ return glm.vec3_get(self.data, key-1)
+ else
+ return Vec3[key]
+ end
+end
+
+
+function Vec3.__newindex(self, key, value)
+ glm.vec3_set(self.data, key-1, value)
+end
+
+
+function Vec3.__tostring(self)
+ return string.format("[%.4f, %.4f, %.4f]", self[1], self[2], self[3])
+end
+
+
+--===== arithmetic =====--
+
+local function swapIfNumber(self, other)
+ if type(self) == "number" and type(other) == "table" then
+ return other, self
+ else
+ return self, other
+ end
+end
+
+
+function Vec3.__add(self, other)
+ local self, other = swapIfNumber(self, other)
+
+ local dest = Vec3()
+ if type(other) == "number" then
+ glm.vec3_adds(self.data, other, dest.data)
+ elseif type(other) == "table" then
+ glm.vec3_add(self.data, other.data, dest.data)
+ else
+ error(string.format("cannot add %s to Vec3", type(other)))
+ end
+ return dest
+end
+
+
+function Vec3.__sub(self, other)
+ local dest = Vec3()
+ if type(other) == "number" then
+ glm.vec3_subs(self.data, other, dest.data)
+ elseif type(other) == "table" then
+ glm.vec3_sub(self.data, other.data, dest.data)
+ else
+ error(string.format("cannot subtract %s from Vec3", type(other)))
+ end
+ return dest
+end
+
+
+function Vec3.__mul(self, other)
+ local self, other = swapIfNumber(self, other)
+ local dest = Vec3()
+ if type(other) == "number" then
+ glm.vec3_scale(self.data, other, dest.data)
+ elseif type(other) == "table" then
+ glm.vec3_mul(self.data, other.data, dest.data)
+ else
+ error(string.format("cannot multiply %s and Vec3", type(other)))
+ end
+ return dest
+end
+
+
+function Vec3.__div(self, other)
+ local dest = Vec3()
+ if type(other) == "number" then
+ glm.vec3_divs(self.data, other, dest.data)
+ elseif type(other) == "table" then
+ glm.vec3_div(self.data, other.data, dest.data)
+ else
+ error(string.format("cannot divide Vec3 by %s", type(other)))
+ end
+ return dest
+end
+
+
+
+
+function Vec3.copyTo(self, dest)
+ glm.vec3_copy(self.data, dest.data)
+end
+
+
+function Vec3.zero(self)
+ glm.vec3_zero(self.data)
+end
+
+
+function Vec3.zero(self)
+ glm.vec3_zero(self.data)
+end
+function Vec3.one(self)
+ glm.vec3_one(self.data)
+end
+
+
+function Vec3.dot(self, other)
+ return glm.vec3_dot(self.data, other.data)
+end
+
+
+function Vec3.crossTo(self, other, dest)
+ glm.vec3_cross(self.data, other.data, dest.data)
+end
+function Vec3.cross(self, other)
+ local dest = Vec3()
+ self:crossTo(other, dest)
+ return dest
+end
+
+
+function Vec3.crossnTo(self, other, dest)
+ glm.vec3_crossn(self.data, other.data, dest.data)
+end
+function Vec3.crossn(self, other)
+ local dest = Vec3()
+ self:crossTo(other, dest)
+ return dest
+end
+
+
+function Vec3.norm2(self)
+ return glm.vec3_norm2(self.data)
+end
+function Vec3.norm(self)
+ return glm.vec3_norm(self.data)
+end
+
+
+function Vec3.normalize(self)
+ glm.vec3_normalize(self.data)
+end
+function Vec3.normalizeTo(self, dest)
+ glm.vec3_normalize_to(self.data, dest.data)
+end
+
+
+return vec3.Vec3