diff options
author | sanine-a <sanine.not@pm.me> | 2020-10-28 20:49:45 -0500 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2020-10-28 20:49:45 -0500 |
commit | 6199061d7367127aee3fb5e10990f751f5b97622 (patch) | |
tree | 84c4c1ca29a156a1c8e6d819f30092157f3852c8 | |
parent | b2d79c96c92f44e07e42129071da0357603664ed (diff) |
add Enum.lua and refactor Vector.lua
-rw-r--r-- | demo/Enum.lua | 18 | ||||
-rw-r--r-- | demo/FPSCamera.lua | 42 | ||||
-rw-r--r-- | demo/Vector.lua | 359 | ||||
-rw-r--r-- | demo/main.lua | 17 |
4 files changed, 358 insertions, 78 deletions
diff --git a/demo/Enum.lua b/demo/Enum.lua new file mode 100644 index 0000000..d7fe469 --- /dev/null +++ b/demo/Enum.lua @@ -0,0 +1,18 @@ +local Enum = function (enumStrings) + local enumTable = {} + for _,str in pairs(enumStrings) do + enumTable[str] = str + end + + local enum = {} + setmetatable(enum, { __index = enumTable, + __newindex = function(table, key, value) + error('Attempt to edit enum!') + end + } + ) + + return enum +end + +return Enum diff --git a/demo/FPSCamera.lua b/demo/FPSCamera.lua index 6b4e4f9..5e21117 100644 --- a/demo/FPSCamera.lua +++ b/demo/FPSCamera.lua @@ -1,4 +1,4 @@ -local Vec3, Vec4 = require('Vector')() +local Vector = require('Vector') local Mat3, Mat4 = require('Matrix')() local Basis = function(M) @@ -18,9 +18,9 @@ local Basis = function(M) local z2 = honey.cglm.get_value(m, Mat3.index(3,3)) local b = {} - b.x = Vec3.new{x0, x1, x2} - b.y = Vec3.new{y0, y1, y2} - b.z = Vec3.new{z0, z1, z2} + b.x = Vector.Vec3.new{x0, x1, x2} + b.y = Vector.Vec3.new{y0, y1, y2} + b.z = Vector.Vec3.new{z0, z1, z2} return b end @@ -34,7 +34,7 @@ camera.sensitivity = 0.1 camera.movement_speed = 1 -camera.position = Vec3.new{0,0,-1} +camera.position = Vector.Vec3.new{0,0,-1} camera.view = Mat4.new() honey.cglm.mat4.identity(camera.view) @@ -52,22 +52,30 @@ honey.cglm.camera.perspective( function camera:update() local M = Mat4.new() honey.cglm.mat4.identity(M) - honey.cglm.affine.rotate(M, Vec3.ZERO, self.basis.x, math.rad(self.pitch)) - honey.cglm.affine.rotate(M, Vec3.ZERO, Vec3.Y_UNIT, math.rad(self.yaw)) + honey.cglm.affine.rotate(M, Vector.Vec3.ZERO.array, self.basis.x.array, math.rad(self.pitch)) + honey.cglm.affine.rotate(M, Vector.Vec3.ZERO.array, Vector.Vec3.Y_UNIT.array, math.rad(self.yaw)) self.basis = Basis(M) - movement = Vec3.new() - if honey.input.key.is_down(honey.input.key.w) then honey.cglm.vec3.add(movement, self.basis.z, movement) end - if honey.input.key.is_down(honey.input.key.a) then honey.cglm.vec3.add(movement, self.basis.x, movement) end - if honey.input.key.is_down(honey.input.key.s) then honey.cglm.vec3.sub(movement, self.basis.z, movement) end - if honey.input.key.is_down(honey.input.key.d) then honey.cglm.vec3.sub(movement, self.basis.x, movement) end + movement = Vector.Vec3.new() + if honey.input.key.is_down(honey.input.key.w) then + movement:add(self.basis.z, movement) + end + if honey.input.key.is_down(honey.input.key.a) then + movement:add(self.basis.x, movement) + end + if honey.input.key.is_down(honey.input.key.s) then + movement:sub(self.basis.z, movement) + end + if honey.input.key.is_down(honey.input.key.d) then + movement:sub(self.basis.x, movement) + end - honey.cglm.set_value(movement, 1, 0) - honey.cglm.vec3.normalize(movement) - honey.cglm.vec3.muls(self.movement_speed, movement, movement) - honey.cglm.vec3.add(self.position, movement, self.position) + movement:setAt(1, 0) + movement:normalize() + movement:muls(self.movement_speed, movement) + self.position:add(movement, self.position) - honey.cglm.camera.look(self.position, self.basis.z, Vec3.Y_UNIT, self.view) + honey.cglm.camera.look(self.position.array, self.basis.z.array, Vector.Vec3.Y_UNIT.array, self.view) end camera.mouse_pos = {} diff --git a/demo/Vector.lua b/demo/Vector.lua index 89fd26a..534be3e 100644 --- a/demo/Vector.lua +++ b/demo/Vector.lua @@ -1,65 +1,316 @@ -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 Enum = require('Enum') + +local Vector = {} + +Vector.type = Enum{'Vec3', 'Vec4'} + +-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +-- +-- Vec3 +-- +-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Vector.Vec3 = {} + +Vector.Vec3.prototype = {} + +Vector.Vec3.prototype.type = Vector.type.Vec3 + +Vector.Vec3.prototype.at = function(self, index) + return honey.cglm.get_value(self.array, index) +end + +Vector.Vec3.prototype.setAt = function(self, index, value) + honey.cglm.set_value(self.array, index, value) +end + +Vector.Vec3.prototype.dot = function(self, v, dest) + local result + if dest == nil then + result = Vector.Vec3.new() + else + result = dest + end + + honey.cglm.vec3.dot(self.array, v.array, result.array) + return result +end + +Vector.Vec3.prototype.norm2 = function(self) + return honey.cglm.vec3.norm2(self.array) +end + +Vector.Vec3.prototype.norm = function(self) + return honey.cglm.vec3.norm(self.array) +end + +Vector.Vec3.prototype.add = function(self, v, dest) + local result + if dest == nil then + result = Vector.Vec3.new() + else + result = dest + end + + honey.cglm.vec3.add(self.array, v.array, result.array) + return result +end + +Vector.Vec3.prototype.adds = function(self, s, dest) + local result + if dest == nil then + result = Vector.Vec3.new() + else + result = dest + end + + honey.cglm.vec3.adds(s, self.array, result.array) + return result +end + +Vector.Vec3.prototype.sub = function(self, v, dest) + local result + if dest == nil then + result = Vector.Vec3.new() + else + result = dest + end + + honey.cglm.vec3.sub(self.array, v.array, result.array) + return result +end + +Vector.Vec3.prototype.mul = function(self, v, dest) + local result + if dest == nil then + result = Vector.Vec3.new() + else + result = dest + end + + honey.cglm.vec3.mul(self.array, v.array, result.array) + return result +end + +Vector.Vec3.prototype.muls = function(self, s, dest) + local result + if dest == nil then + result = Vector.Vec3.new() + else + result = dest + end + + honey.cglm.vec3.muls(s, self.array, result.array) + return result +end + +Vector.Vec3.prototype.normalize = function(self) + honey.cglm.vec3.normalize(self.array) +end + +Vector.Vec3.prototype.distanceTo = function(self, v) + return honey.cglm.vec3.distance(self.array, v.array) +end + +Vector.Vec3.prototype.lerp = function(self, v, s, dest) + local result + if dest == nil then + result = Vector.Vec3.new() + else + result = dest + end + + honey.cglm.vec3.lerp(self.array, v.array, s, result.array) + return result +end + +-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Vector.Vec3.mt = {} +Vector.Vec3.mt.__index = Vector.Vec3.prototype +Vector.Vec3.mt.__tostring = function(vec3) + local str = 'vec3('.. + tostring(vec3:at(0))..', '.. + tostring(vec3:at(1))..', '.. + tostring(vec3:at(2))..')' + return str +end + +-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Vector.Vec3.new = function(tbl) + local vec3 = {} + vec3.array = honey.cglm.new_array_zero(3) + setmetatable(vec3, Vector.Vec3.mt) + + if tbl == nil then return vec3 end + + for i=0,2 do + vec3:setAt(i, tbl[i+1]) + end + + return vec3 +end + +Vector.Vec3.ZERO = Vector.Vec3.new() +Vector.Vec3.X_UNIT = Vector.Vec3.new{1, 0, 0} +Vector.Vec3.Y_UNIT = Vector.Vec3.new{0, 1, 0} +Vector.Vec3.Z_UNIT = Vector.Vec3.new{0, 0, 1} -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +-- +-- Vec4 +-- +-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Vector.Vec4 = {} + +Vector.Vec4.prototype = {} + +Vector.Vec4.prototype.type = Vector.type.Vec4 + +Vector.Vec4.prototype.at = function(self, index) + return honey.cglm.get_value(self.array, index) +end + +Vector.Vec4.prototype.setAt = function(self, index, value) + honey.cglm.set_value(self.array, index, value) +end + +Vector.Vec4.prototype.dot = function(self, v, dest) + local result + if dest == nil then + result = Vector.Vec4.new() + else + result = dest + end + + honey.cglm.vec4.dot(self.array, v.array, result.array) + return result +end + +Vector.Vec4.prototype.norm2 = function(self) + return honey.cglm.vec4.norm2(self.array) +end + +Vector.Vec4.prototype.norm = function(self) + return honey.cglm.vec4.norm(self.array) +end + +Vector.Vec4.prototype.add = function(self, v, dest) + local result + if dest == nil then + result = Vector.Vec4.new() + else + result = dest + end + + honey.cglm.vec4.add(self.array, v.array, result.array) + return result +end + +Vector.Vec4.prototype.adds = function(self, s, dest) + local result + if dest == nil then + result = Vector.Vec4.new() + else + result = dest + end + + honey.cglm.vec4.adds(s, self.array, result.array) + return result +end -local Vec4 = {} +Vector.Vec4.prototype.sub = function(self, v, dest) + local result + if dest == nil then + result = Vector.Vec4.new() + else + result = dest + end -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 + honey.cglm.vec4.sub(self.array, v.array, result.array) + return result 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 +Vector.Vec4.prototype.mul = function(self, v, dest) + local result + if dest == nil then + result = Vector.Vec4.new() + else + result = dest + end - return vec4 + honey.cglm.vec4.mul(self.array, v.array, result.array) + return result end +Vector.Vec4.prototype.muls = function(self, s, dest) + local result + if dest == nil then + result = Vector.Vec4.new() + else + result = dest + end + + honey.cglm.vec4.muls(s, self.array, result.array) + return result +end + +Vector.Vec4.prototype.normalize = function(self) + honey.cglm.vec4.normalize(self.array) +end + +Vector.Vec4.prototype.distanceTo = function(self, v) + return honey.cglm.vec4.distance(self.array, v.array) +end + +Vector.Vec4.prototype.lerp = function(self, v, s, dest) + local result + if dest == nil then + result = Vector.Vec4.new() + else + result = dest + end + + honey.cglm.vec4.lerp(self.array, v.array, s, result.array) + return result +end + +-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Vector.Vec4.mt = {} +Vector.Vec4.mt.__index = Vector.Vec4.prototype +Vector.Vec4.mt.__tostring = function(vec4) + local str = 'vec4('.. + tostring(vec4:at(0))..', '.. + tostring(vec4:at(1))..', '.. + tostring(vec4:at(2))..', '.. + tostring(vec4:at(3))..')' + return str +end + +-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Vector.Vec4.new = function(tbl) + local vec4 = {} + vec4.array = honey.cglm.new_array_zero(4) + setmetatable(vec4, Vector.Vec4.mt) + + if tbl == nil then return vec4 end + + for i=0,3 do + vec4:setAt(i, tbl[i+1]) + end + + return vec4 +end + +-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +return Vector + + + -return function() return Vec3, Vec4 end diff --git a/demo/main.lua b/demo/main.lua index ec1a952..62ffcd2 100644 --- a/demo/main.lua +++ b/demo/main.lua @@ -1,10 +1,13 @@ -local Vec3, Vec4 = require('Vector')() +local Vector = require('Vector') local Mat3, Mat4 = require('Matrix')() local FPSCamera = require('FPSCamera') local model = Mat4.new() honey.cglm.mat4.identity(model) -honey.cglm.affine.rotate(model, Vec3.ZERO, Vec3.X_UNIT, math.pi/4) +honey.cglm.affine.rotate(model, + Vector.Vec3.ZERO.array, + Vector.Vec3.X_UNIT.array, + math.pi/4) honey.input.key.bind(honey.input.key.escape, honey.exit) @@ -31,15 +34,15 @@ void main() { color = base_color; } ]] local shader = honey.shader.new(vertex_shader, fragment_shader) local plane = honey.primitives.cube(10,10,10) -local color1 = Vec4.new{1,0,0,1} -local color2 = Vec4.new{0,0,1,1} -local color = honey.cglm.new_array_zero(4) +local color1 = Vector.Vec4.new{1,0,0,1} +local color2 = Vector.Vec4.new{0,0,1,1} +local color = Vector.Vec4.new() local total_time = 0 function honey.update(dt) total_time = total_time + dt - honey.cglm.vec4.lerp(color1, color2, 0.5*(math.sin(math.pi*total_time)+1), color) + color1:lerp(color2, 0.5*(math.sin(math.pi*total_time)+1), color) end function honey.draw() @@ -47,6 +50,6 @@ function honey.draw() honey.shader.set_mat4(shader, 'model', model) honey.shader.set_mat4(shader, 'view', FPSCamera.view) honey.shader.set_mat4(shader, 'projection', FPSCamera.projection) - honey.shader.set_vec4(shader, "base_color", color) + honey.shader.set_vec4(shader, "base_color", color.array) honey.mesh.draw(plane, shader) end |