diff options
author | sanine <sanine.not@pm.me> | 2023-04-07 00:53:03 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-04-07 00:53:03 -0500 |
commit | 3c9ba50944f1b136125e1191ccbc49fda1381123 (patch) | |
tree | f52a32da115d02eaeefe616122f607c92c62e57c | |
parent | f91a730b3a9ddd532c2ba9ff4f8b120a8ca2fa81 (diff) |
renormalize line endings
-rw-r--r-- | .gitignore | 8 | ||||
-rw-r--r-- | fragment.glsl | 34 | ||||
-rw-r--r-- | honey.bak/ecs-systems.lua | 152 | ||||
-rw-r--r-- | honey.bak/glm.lua | 722 | ||||
-rw-r--r-- | honey/ecs-systems.lua | 236 | ||||
-rw-r--r-- | honey/glm.lua | 712 | ||||
-rw-r--r-- | vertex.glsl | 42 |
7 files changed, 953 insertions, 953 deletions
@@ -1,4 +1,4 @@ -*.
-*~
-*.swp
-.*
+*. +*~ +*.swp +.* diff --git a/fragment.glsl b/fragment.glsl index c6ef691..532d1ac 100644 --- a/fragment.glsl +++ b/fragment.glsl @@ -1,17 +1,17 @@ -#version 410 core
-out vec4 FragColor;
-
-in vec3 position;
-in vec3 normal;
-in vec2 tex;
-
-uniform float time;
-uniform sampler2D ourTexture;
-
-void main()
-{
- //FragColor = vec4(sin(time), cos(time), -sin(time), 1.0f);
- //FragColor = vec4(normal, 1.0f);
- //FragColor = vec4(tex, 1.0f, 1.0f);
- FragColor = texture(ourTexture, tex);
-}
+#version 410 core +out vec4 FragColor; + +in vec3 position; +in vec3 normal; +in vec2 tex; + +uniform float time; +uniform sampler2D ourTexture; + +void main() +{ + //FragColor = vec4(sin(time), cos(time), -sin(time), 1.0f); + //FragColor = vec4(normal, 1.0f); + //FragColor = vec4(tex, 1.0f, 1.0f); + FragColor = texture(ourTexture, tex); +} diff --git a/honey.bak/ecs-systems.lua b/honey.bak/ecs-systems.lua index 5dec159..e3f9d56 100644 --- a/honey.bak/ecs-systems.lua +++ b/honey.bak/ecs-systems.lua @@ -1,76 +1,76 @@ -local ecs = require 'honey.ecs'
-
-
-local module = {}
-setmetatable(module, {__index=_G})
-setfenv(1, module)
-
-
-
---===== transform cascading =====--
-
-local function recursiveComputeTransform(entity)
- if entity._transformComputed then
- return entity._transform
- end
- if entity.parent == false then
- entity._transformComputed = true
- entity._transform = entity.transform
- return entity.transform
- end
-
- entity._transformComputed = true
- local parentTransform = recursiveComputeTransform(entity.parent)
- entity._transform = parentTransform * entity.transform
- return entity._transform
-end
-
--- update transforms
-transformCascade = {
- filter=ecs.Filter.AND{"transform", "parent"},
- prepareEntity=function(self, entity)
- entity._transform = nil
- entity._transformComputed = false
- end,
- update=function(self, entity, dt)
- recursiveComputeTransform(entity)
- end,
- priority=98,
-}
-
-
---===== rendering =====--
-
-function renderCam(camera, priority)
- local priority = priority or 99
- return {
- filter=ecs.Filter.AND{"mesh", "shader", "transform"},
- update=function(self, entity, dt)
- entity.shader:use()
- entity.shader:configure{
- matrix={
- model=entity._transform,
- view=camera.view,
- projection=camera.projection,
- },
- }
- entity.mesh:drawElements()
- end,
- nopause=true,
- priority=priority,
- }
-end
-
---===== update functions =====--
-
-update = {
- filter=ecs.Filter.AND{"update"},
- update=function(self, entity, dt)
- entity.update(entity, dt)
- end,
- priority=50,
-}
-
-
-
-return module
+local ecs = require 'honey.ecs' + + +local module = {} +setmetatable(module, {__index=_G}) +setfenv(1, module) + + + +--===== transform cascading =====-- + +local function recursiveComputeTransform(entity) + if entity._transformComputed then + return entity._transform + end + if entity.parent == false then + entity._transformComputed = true + entity._transform = entity.transform + return entity.transform + end + + entity._transformComputed = true + local parentTransform = recursiveComputeTransform(entity.parent) + entity._transform = parentTransform * entity.transform + return entity._transform +end + +-- update transforms +transformCascade = { + filter=ecs.Filter.AND{"transform", "parent"}, + prepareEntity=function(self, entity) + entity._transform = nil + entity._transformComputed = false + end, + update=function(self, entity, dt) + recursiveComputeTransform(entity) + end, + priority=98, +} + + +--===== rendering =====-- + +function renderCam(camera, priority) + local priority = priority or 99 + return { + filter=ecs.Filter.AND{"mesh", "shader", "transform"}, + update=function(self, entity, dt) + entity.shader:use() + entity.shader:configure{ + matrix={ + model=entity._transform, + view=camera.view, + projection=camera.projection, + }, + } + entity.mesh:drawElements() + end, + nopause=true, + priority=priority, + } +end + +--===== update functions =====-- + +update = { + filter=ecs.Filter.AND{"update"}, + update=function(self, entity, dt) + entity.update(entity, dt) + end, + priority=50, +} + + + +return module diff --git a/honey.bak/glm.lua b/honey.bak/glm.lua index af8dcb3..c7c83b9 100644 --- a/honey.bak/glm.lua +++ b/honey.bak/glm.lua @@ -1,361 +1,361 @@ -local glm = honey.glm
-
-local module = {}
-setmetatable(module, {__index=_G})
-setfenv(1, module)
-
-
-Vec3 = {}
-Mat4 = {}
-Quaternion = {}
-
-
---===== 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("Vec3[%.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
-
-
-----------------------------------------
-
-local RowLookup = {}
-function RowLookup.new(_, row, data)
- local self = {
- row=row,
- data=data,
- }
- setmetatable(self, RowLookup)
- return self
-end
-setmetatable(RowLookup, {__call=RowLookup.new})
-function RowLookup.__index(self, col)
- return glm.mat4_get(self.data, col-1, self.row-1)
-end
-function RowLookup.__newindex(self, col, value)
- return glm.mat4_set(self.data, col-1, self.row-1, value)
-end
-
-
---===== Mat4 =====--
-
-
-function Mat4.new(_, self, values)
- local self = {}
- self.type = "mat4"
- self.data = glm.mat4_create()
- setmetatable(self, Mat4)
- if values then
- self[1][1] = values[1]
- self[1][2] = values[2]
- self[1][3] = values[3]
- self[1][4] = values[4]
-
- self[2][1] = values[5]
- self[2][2] = values[6]
- self[2][3] = values[7]
- self[2][4] = values[8]
-
- self[3][1] = values[9]
- self[3][2] = values[10]
- self[3][3] = values[11]
- self[3][4] = values[12]
-
- self[4][1] = values[13]
- self[4][2] = values[14]
- self[4][3] = values[15]
- self[4][4] = values[16]
- end
- return self
-end
-setmetatable(Mat4, {__call=Mat4.new})
-
-
-function Mat4.__index(self, key)
- if type(key) == "number" then
- return RowLookup(key, self.data)
- else
- return Mat4[key]
- end
-end
-
-
-function Mat4.__tostring(self)
- return string.format(
- "/ %0.4f, %0.4f, %0.4f, %0.4f \\\n" ..
- "| %0.4f, %0.4f, %0.4f, %0.4f |\n" ..
- "| %0.4f, %0.4f, %0.4f, %0.4f |\n" ..
- "\\ %0.4f, %0.4f, %0.4f, %0.4f /",
- self[1][1], self[1][2], self[1][3], self[1][4],
- self[2][1], self[2][2], self[2][3], self[2][4],
- self[3][1], self[3][2], self[3][3], self[3][4],
- self[4][1], self[4][2], self[4][3], self[4][4]
- )
-end
-
-
-function Mat4.__mul(self, other)
- if other.type == "mat4" then
- local dest = Mat4()
- glm.mat4_mul(self.data, other.data, dest.data)
- return dest
- elseif other.type == "vec4" then
- -- todo
- elseif other.type == "vec3" then
- local dest = Vec3()
- glm.mat4_mulv3(self.data, other.data, 1.0, dest.data)
- return dest
- else
- error(string.format("cannot multiply Mat4 by %s", type(other)))
- end
-end
-
-
-function Mat4.copy(self, other)
- glm.mat4_copy(other.data, self.data)
- return self
-end
-function Mat4.copyTo(self, dest)
- glm.mat4_copy(self.data, dest.data)
- return self
-end
-
-
-function Mat4.identity(self)
- glm.mat4_identity(self.data)
- return self
-end
-
-
-function Mat4.zero(self)
- glm.mat4_zero(self.data)
- return self
-end
-
-
-function Mat4.mul(self, other)
- glm.mat4_mul(self.data, other.data, self.data)
- return self
-end
-
-
-function Mat4.translate(self, vec)
- glm.translate(self.data, vec.data)
- return self
-end
-
-
-function Mat4.rotateX(self, angle)
- glm.rotate_x(self.data, angle, self.data)
- return self
-end
-function Mat4.rotateY(self, angle)
- glm.rotate_y(self.data, angle, self.data)
- return self
-end
-function Mat4.rotateZ(self, angle)
- glm.rotate_z(self.data, angle, self.data)
- return self
-end
-
-
-function Mat4.scale(self, vec)
- glm.scale(self.data, vec.data)
- return self
-end
-
-
-function Mat4.perspective(self, fovy, aspect, near, far)
- glm.perspective(fovy, aspect, near, far, self.data)
- return self
-end
-function Mat4.perspectiveResize(self, aspect)
- glm.perspective_resize(aspect, self.data)
- return self
-end
-
-
---===== Quaternion =====--
-
-
-Quaternion.__index = Quaternion
-
-
-function Quaternion.new(_, tbl)
- local tbl = tbl or { 0, 0, 0, 0 }
- local self = {}
- self.data = glm.quat_create()
- glm.quat_init(self.data, unpack(tbl))
- setmetatable(self, Quaternion)
- return self
-end
-setmetatable(Quaternion, {__call=Quaternion.new})
-
-
-function Quaternion.toMat4(self)
- local m = Mat4()
- glm.quat_mat4(self.data, m.data)
- return m
-end
-
-
----------------------------
-
-
-return module
+local glm = honey.glm + +local module = {} +setmetatable(module, {__index=_G}) +setfenv(1, module) + + +Vec3 = {} +Mat4 = {} +Quaternion = {} + + +--===== 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("Vec3[%.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 + + +---------------------------------------- + +local RowLookup = {} +function RowLookup.new(_, row, data) + local self = { + row=row, + data=data, + } + setmetatable(self, RowLookup) + return self +end +setmetatable(RowLookup, {__call=RowLookup.new}) +function RowLookup.__index(self, col) + return glm.mat4_get(self.data, col-1, self.row-1) +end +function RowLookup.__newindex(self, col, value) + return glm.mat4_set(self.data, col-1, self.row-1, value) +end + + +--===== Mat4 =====-- + + +function Mat4.new(_, self, values) + local self = {} + self.type = "mat4" + self.data = glm.mat4_create() + setmetatable(self, Mat4) + if values then + self[1][1] = values[1] + self[1][2] = values[2] + self[1][3] = values[3] + self[1][4] = values[4] + + self[2][1] = values[5] + self[2][2] = values[6] + self[2][3] = values[7] + self[2][4] = values[8] + + self[3][1] = values[9] + self[3][2] = values[10] + self[3][3] = values[11] + self[3][4] = values[12] + + self[4][1] = values[13] + self[4][2] = values[14] + self[4][3] = values[15] + self[4][4] = values[16] + end + return self +end +setmetatable(Mat4, {__call=Mat4.new}) + + +function Mat4.__index(self, key) + if type(key) == "number" then + return RowLookup(key, self.data) + else + return Mat4[key] + end +end + + +function Mat4.__tostring(self) + return string.format( + "/ %0.4f, %0.4f, %0.4f, %0.4f \\\n" .. + "| %0.4f, %0.4f, %0.4f, %0.4f |\n" .. + "| %0.4f, %0.4f, %0.4f, %0.4f |\n" .. + "\\ %0.4f, %0.4f, %0.4f, %0.4f /", + self[1][1], self[1][2], self[1][3], self[1][4], + self[2][1], self[2][2], self[2][3], self[2][4], + self[3][1], self[3][2], self[3][3], self[3][4], + self[4][1], self[4][2], self[4][3], self[4][4] + ) +end + + +function Mat4.__mul(self, other) + if other.type == "mat4" then + local dest = Mat4() + glm.mat4_mul(self.data, other.data, dest.data) + return dest + elseif other.type == "vec4" then + -- todo + elseif other.type == "vec3" then + local dest = Vec3() + glm.mat4_mulv3(self.data, other.data, 1.0, dest.data) + return dest + else + error(string.format("cannot multiply Mat4 by %s", type(other))) + end +end + + +function Mat4.copy(self, other) + glm.mat4_copy(other.data, self.data) + return self +end +function Mat4.copyTo(self, dest) + glm.mat4_copy(self.data, dest.data) + return self +end + + +function Mat4.identity(self) + glm.mat4_identity(self.data) + return self +end + + +function Mat4.zero(self) + glm.mat4_zero(self.data) + return self +end + + +function Mat4.mul(self, other) + glm.mat4_mul(self.data, other.data, self.data) + return self +end + + +function Mat4.translate(self, vec) + glm.translate(self.data, vec.data) + return self +end + + +function Mat4.rotateX(self, angle) + glm.rotate_x(self.data, angle, self.data) + return self +end +function Mat4.rotateY(self, angle) + glm.rotate_y(self.data, angle, self.data) + return self +end +function Mat4.rotateZ(self, angle) + glm.rotate_z(self.data, angle, self.data) + return self +end + + +function Mat4.scale(self, vec) + glm.scale(self.data, vec.data) + return self +end + + +function Mat4.perspective(self, fovy, aspect, near, far) + glm.perspective(fovy, aspect, near, far, self.data) + return self +end +function Mat4.perspectiveResize(self, aspect) + glm.perspective_resize(aspect, self.data) + return self +end + + +--===== Quaternion =====-- + + +Quaternion.__index = Quaternion + + +function Quaternion.new(_, tbl) + local tbl = tbl or { 0, 0, 0, 0 } + local self = {} + self.data = glm.quat_create() + glm.quat_init(self.data, unpack(tbl)) + setmetatable(self, Quaternion) + return self +end +setmetatable(Quaternion, {__call=Quaternion.new}) + + +function Quaternion.toMat4(self) + local m = Mat4() + glm.quat_mat4(self.data, m.data) + return m +end + + +--------------------------- + + +return module diff --git a/honey/ecs-systems.lua b/honey/ecs-systems.lua index 18f50a8..9995304 100644 --- a/honey/ecs-systems.lua +++ b/honey/ecs-systems.lua @@ -1,118 +1,118 @@ -local ecs = require 'honey.ecs'
-local gl = honey.gl
-local glfw = honey.glfw
-
-
-local module = {}
-setmetatable(module, {__index=_G})
-setfenv(1, module)
-
-
-
---===== transform cascading =====--
-
-transform = function(params)
- return {
- db = params.db,
- update = function(self, dt)
- local entities = self.db:queryComponent("transform")
-
- -- prepare transforms
- for id, transform in pairs(entities) do
- transform._visited = false
- end
-
- -- helper function
- local function recursiveTransform(transform)
- if transform._visited then
- return transform._matrix
- end
-
- if not transform.parent then
- transform._matrix = transform.matrix
- else
- local parentTransform = self.db:getComponent(transform.parent, "transform")
- local parentMatrix = recursiveTransform(parentTransform)
- transform._matrix = parentMatrix * transform.matrix
- end
- transform._visited = true
- return transform._matrix
- end
-
- -- compute transforms
- for id, transform in pairs(entities) do
- recursiveTransform(transform)
- end
- end,
- priority = 0,
- }
-end
-
-
-
---===== rendering =====--
-
-function renderCamera(params)
- return {
- camera = params.camera,
- db = params.db,
- priority = params.priority or 99,
- update = function(self, dt)
- local cameraParams = self.db:getComponent(self.camera, "camera")
- local cameraTransform = self.db:getComponent(self.camera, "transform")
- local view
- if cameraTransform then
- view = cameraTransform._matrix
- else
- view = Mat4():identity()
- end
-
- local entities = self.db:queryComponent("renderMesh")
- for entity, tbl in pairs(entities) do
- -- get shader
- local shader = honey.shader.loadShader(tbl.shader.vertex, tbl.shader.fragment)
- shader:use()
-
- -- bind textures
- local texOffset = 0
- for name, texTbl in pairs(tbl.textures or {}) do
- local texture = honey.image.loadImage(texTbl.filename, texTbl.params)
- gl.BindTexture(gl.TEXTURE_2D + texOffset, texture.texture)
- shader:setInt(name, texOffset)
- texOffset = texOffset + 1
- end
-
- -- configure default uniforms
- local query = self.db:getComponent(entity, "transform")
- local model = (query and query._matrix) or Mat4():identity()
- shader:configure{
- float={
- time=glfw.GetTime(),
- },
- matrix={
- view=view,
- projection=cameraParams.projection,
- model=model,
- },
- }
-
- -- draw mesh
- local mesh = honey.mesh.loadMesh(tbl.mesh.filename, tbl.mesh.index)
- mesh:drawElements()
-
- -- unbind textures
- for i=0,texOffset-1 do
- gl.BindTexture(gl.TEXTURE_2D + i, 0)
- end
- end
- end,
- }
-end
-
-
-
---===== update functions =====--
-
-
-
-return module
+local ecs = require 'honey.ecs' +local gl = honey.gl +local glfw = honey.glfw + + +local module = {} +setmetatable(module, {__index=_G}) +setfenv(1, module) + + + +--===== transform cascading =====-- + +transform = function(params) + return { + db = params.db, + update = function(self, dt) + local entities = self.db:queryComponent("transform") + + -- prepare transforms + for id, transform in pairs(entities) do + transform._visited = false + end + + -- helper function + local function recursiveTransform(transform) + if transform._visited then + return transform._matrix + end + + if not transform.parent then + transform._matrix = transform.matrix + else + local parentTransform = self.db:getComponent(transform.parent, "transform") + local parentMatrix = recursiveTransform(parentTransform) + transform._matrix = parentMatrix * transform.matrix + end + transform._visited = true + return transform._matrix + end + + -- compute transforms + for id, transform in pairs(entities) do + recursiveTransform(transform) + end + end, + priority = 0, + } +end + + + +--===== rendering =====-- + +function renderCamera(params) + return { + camera = params.camera, + db = params.db, + priority = params.priority or 99, + update = function(self, dt) + local cameraParams = self.db:getComponent(self.camera, "camera") + local cameraTransform = self.db:getComponent(self.camera, "transform") + local view + if cameraTransform then + view = cameraTransform._matrix + else + view = Mat4():identity() + end + + local entities = self.db:queryComponent("renderMesh") + for entity, tbl in pairs(entities) do + -- get shader + local shader = honey.shader.loadShader(tbl.shader.vertex, tbl.shader.fragment) + shader:use() + + -- bind textures + local texOffset = 0 + for name, texTbl in pairs(tbl.textures or {}) do + local texture = honey.image.loadImage(texTbl.filename, texTbl.params) + gl.BindTexture(gl.TEXTURE_2D + texOffset, texture.texture) + shader:setInt(name, texOffset) + texOffset = texOffset + 1 + end + + -- configure default uniforms + local query = self.db:getComponent(entity, "transform") + local model = (query and query._matrix) or Mat4():identity() + shader:configure{ + float={ + time=glfw.GetTime(), + }, + matrix={ + view=view, + projection=cameraParams.projection, + model=model, + }, + } + + -- draw mesh + local mesh = honey.mesh.loadMesh(tbl.mesh.filename, tbl.mesh.index) + mesh:drawElements() + + -- unbind textures + for i=0,texOffset-1 do + gl.BindTexture(gl.TEXTURE_2D + i, 0) + end + end + end, + } +end + + + +--===== update functions =====-- + + + +return module diff --git a/honey/glm.lua b/honey/glm.lua index 20d4727..dada37f 100644 --- a/honey/glm.lua +++ b/honey/glm.lua @@ -1,356 +1,356 @@ -local glm = honey.glm
-
-local module = {}
-setmetatable(module, {__index=_G})
-setfenv(1, module)
-
-
-Vec3 = {}
-Mat4 = {}
-Quaternion = {}
-
-
---===== 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("Vec3[%.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
-
-
-----------------------------------------
-
-local RowLookup = {}
-function RowLookup.new(_, row, data)
- local self = {
- row=row,
- data=data,
- }
- setmetatable(self, RowLookup)
- return self
-end
-setmetatable(RowLookup, {__call=RowLookup.new})
-function RowLookup.__index(self, col)
- return glm.mat4_get(self.data, col-1, self.row-1)
-end
-function RowLookup.__newindex(self, col, value)
- return glm.mat4_set(self.data, col-1, self.row-1, value)
-end
-
-
---===== Mat4 =====--
-
-
-function Mat4.new(_, self, values)
- local self = {}
- self.type = "mat4"
- self.data = glm.mat4_create()
- setmetatable(self, Mat4)
- if values then
- self[1][1] = values[1]
- self[1][2] = values[2]
- self[1][3] = values[3]
- self[1][4] = values[4]
-
- self[2][1] = values[5]
- self[2][2] = values[6]
- self[2][3] = values[7]
- self[2][4] = values[8]
-
- self[3][1] = values[9]
- self[3][2] = values[10]
- self[3][3] = values[11]
- self[3][4] = values[12]
-
- self[4][1] = values[13]
- self[4][2] = values[14]
- self[4][3] = values[15]
- self[4][4] = values[16]
- end
- return self
-end
-setmetatable(Mat4, {__call=Mat4.new})
-
-
-function Mat4.__index(self, key)
- if type(key) == "number" then
- return RowLookup(key, self.data)
- else
- return Mat4[key]
- end
-end
-
-
-function Mat4.__tostring(self)
- return string.format(
- "/ %0.4f, %0.4f, %0.4f, %0.4f \\\n" ..
- "| %0.4f, %0.4f, %0.4f, %0.4f |\n" ..
- "| %0.4f, %0.4f, %0.4f, %0.4f |\n" ..
- "\\ %0.4f, %0.4f, %0.4f, %0.4f /",
- self[1][1], self[1][2], self[1][3], self[1][4],
- self[2][1], self[2][2], self[2][3], self[2][4],
- self[3][1], self[3][2], self[3][3], self[3][4],
- self[4][1], self[4][2], self[4][3], self[4][4]
- )
-end
-
-
-function Mat4.__mul(self, other)
- if other.type == "mat4" then
- local dest = Mat4()
- glm.mat4_mul(self.data, other.data, dest.data)
- return dest
- elseif other.type == "vec4" then
- -- todo
- elseif other.type == "vec3" then
- local dest = Vec3()
- glm.mat4_mulv3(self.data, other.data, 1.0, dest.data)
- return dest
- else
- error(string.format("cannot multiply Mat4 by %s", type(other)))
- end
-end
-
-
-function Mat4.copyTo(self, dest)
- glm.mat4_copy(self.data, dest.data)
-end
-
-
-function Mat4.identity(self)
- glm.mat4_identity(self.data)
- return self
-end
-
-
-function Mat4.zero(self)
- glm.mat4_zero(self.data)
- return self
-end
-
-
-function Mat4.mul(self, other)
- glm.mat4_mul(self.data, other.data, self.data)
- return self
-end
-
-
-function Mat4.translate(self, vec)
- glm.translate(self.data, vec.data)
- return self
-end
-
-
-function Mat4.rotateX(self, angle)
- glm.rotate_x(self.data, angle, self.data)
- return self
-end
-function Mat4.rotateY(self, angle)
- glm.rotate_y(self.data, angle, self.data)
- return self
-end
-function Mat4.rotateZ(self, angle)
- glm.rotate_z(self.data, angle, self.data)
- return self
-end
-
-
-function Mat4.scale(self, vec)
- glm.scale(self.data, vec.data)
- return self
-end
-
-
-function Mat4.perspective(self, fovy, aspect, near, far)
- glm.perspective(fovy, aspect, near, far, self.data)
- return self
-end
-function Mat4.perspectiveResize(self, aspect)
- glm.perspective_resize(aspect, self.data)
- return self
-end
-
-
---===== Quaternion =====--
-
-
-Quaternion.__index = Quaternion
-
-
-function Quaternion.new(_, tbl)
- local tbl = tbl or { 0, 0, 0, 0 }
- local self = {}
- self.data = glm.quat_create()
- glm.quat_init(self.data, unpack(tbl))
- setmetatable(self, Quaternion)
- return self
-end
-setmetatable(Quaternion, {__call=Quaternion.new})
-
-
-function Quaternion.toMat4(self)
- local m = Mat4()
- glm.quat_mat4(self.data, m.data)
- return m
-end
-
-
----------------------------
-
-
-return module
+local glm = honey.glm + +local module = {} +setmetatable(module, {__index=_G}) +setfenv(1, module) + + +Vec3 = {} +Mat4 = {} +Quaternion = {} + + +--===== 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("Vec3[%.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 + + +---------------------------------------- + +local RowLookup = {} +function RowLookup.new(_, row, data) + local self = { + row=row, + data=data, + } + setmetatable(self, RowLookup) + return self +end +setmetatable(RowLookup, {__call=RowLookup.new}) +function RowLookup.__index(self, col) + return glm.mat4_get(self.data, col-1, self.row-1) +end +function RowLookup.__newindex(self, col, value) + return glm.mat4_set(self.data, col-1, self.row-1, value) +end + + +--===== Mat4 =====-- + + +function Mat4.new(_, self, values) + local self = {} + self.type = "mat4" + self.data = glm.mat4_create() + setmetatable(self, Mat4) + if values then + self[1][1] = values[1] + self[1][2] = values[2] + self[1][3] = values[3] + self[1][4] = values[4] + + self[2][1] = values[5] + self[2][2] = values[6] + self[2][3] = values[7] + self[2][4] = values[8] + + self[3][1] = values[9] + self[3][2] = values[10] + self[3][3] = values[11] + self[3][4] = values[12] + + self[4][1] = values[13] + self[4][2] = values[14] + self[4][3] = values[15] + self[4][4] = values[16] + end + return self +end +setmetatable(Mat4, {__call=Mat4.new}) + + +function Mat4.__index(self, key) + if type(key) == "number" then + return RowLookup(key, self.data) + else + return Mat4[key] + end +end + + +function Mat4.__tostring(self) + return string.format( + "/ %0.4f, %0.4f, %0.4f, %0.4f \\\n" .. + "| %0.4f, %0.4f, %0.4f, %0.4f |\n" .. + "| %0.4f, %0.4f, %0.4f, %0.4f |\n" .. + "\\ %0.4f, %0.4f, %0.4f, %0.4f /", + self[1][1], self[1][2], self[1][3], self[1][4], + self[2][1], self[2][2], self[2][3], self[2][4], + self[3][1], self[3][2], self[3][3], self[3][4], + self[4][1], self[4][2], self[4][3], self[4][4] + ) +end + + +function Mat4.__mul(self, other) + if other.type == "mat4" then + local dest = Mat4() + glm.mat4_mul(self.data, other.data, dest.data) + return dest + elseif other.type == "vec4" then + -- todo + elseif other.type == "vec3" then + local dest = Vec3() + glm.mat4_mulv3(self.data, other.data, 1.0, dest.data) + return dest + else + error(string.format("cannot multiply Mat4 by %s", type(other))) + end +end + + +function Mat4.copyTo(self, dest) + glm.mat4_copy(self.data, dest.data) +end + + +function Mat4.identity(self) + glm.mat4_identity(self.data) + return self +end + + +function Mat4.zero(self) + glm.mat4_zero(self.data) + return self +end + + +function Mat4.mul(self, other) + glm.mat4_mul(self.data, other.data, self.data) + return self +end + + +function Mat4.translate(self, vec) + glm.translate(self.data, vec.data) + return self +end + + +function Mat4.rotateX(self, angle) + glm.rotate_x(self.data, angle, self.data) + return self +end +function Mat4.rotateY(self, angle) + glm.rotate_y(self.data, angle, self.data) + return self +end +function Mat4.rotateZ(self, angle) + glm.rotate_z(self.data, angle, self.data) + return self +end + + +function Mat4.scale(self, vec) + glm.scale(self.data, vec.data) + return self +end + + +function Mat4.perspective(self, fovy, aspect, near, far) + glm.perspective(fovy, aspect, near, far, self.data) + return self +end +function Mat4.perspectiveResize(self, aspect) + glm.perspective_resize(aspect, self.data) + return self +end + + +--===== Quaternion =====-- + + +Quaternion.__index = Quaternion + + +function Quaternion.new(_, tbl) + local tbl = tbl or { 0, 0, 0, 0 } + local self = {} + self.data = glm.quat_create() + glm.quat_init(self.data, unpack(tbl)) + setmetatable(self, Quaternion) + return self +end +setmetatable(Quaternion, {__call=Quaternion.new}) + + +function Quaternion.toMat4(self) + local m = Mat4() + glm.quat_mat4(self.data, m.data) + return m +end + + +--------------------------- + + +return module diff --git a/vertex.glsl b/vertex.glsl index 1e9fb67..cb20d1f 100644 --- a/vertex.glsl +++ b/vertex.glsl @@ -1,21 +1,21 @@ -#version 410 core
-layout (location = 0) in vec3 in_position;
-layout (location = 1) in vec3 in_normal;
-layout (location = 2) in vec2 in_tex;
-
-uniform mat4 model;
-uniform mat4 view;
-uniform mat4 projection;
-
-out vec3 position;
-out vec3 normal;
-out vec2 tex;
-
-void main()
-{
- gl_Position = projection * view * model * vec4(in_position, 1.0);
- position = in_position;
- //normal = vec3(model * vec4(in_normal, 1.0f));
- normal = in_normal;
- tex = in_tex;
-}
+#version 410 core +layout (location = 0) in vec3 in_position; +layout (location = 1) in vec3 in_normal; +layout (location = 2) in vec2 in_tex; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +out vec3 position; +out vec3 normal; +out vec2 tex; + +void main() +{ + gl_Position = projection * view * model * vec4(in_position, 1.0); + position = in_position; + //normal = vec3(model * vec4(in_normal, 1.0f)); + normal = in_normal; + tex = in_tex; +} |