diff options
-rw-r--r-- | assets/green-grass.jpg | bin | 0 -> 406872 bytes | |||
-rw-r--r-- | honey/ecs-systems.lua | 54 | ||||
-rw-r--r-- | main.lua | 34 | ||||
-rw-r--r-- | shaders/mesh.frag | 17 | ||||
-rw-r--r-- | shaders/mesh.vert | 21 | ||||
-rw-r--r-- | shaders/quad.vert | 21 |
6 files changed, 103 insertions, 44 deletions
diff --git a/assets/green-grass.jpg b/assets/green-grass.jpg Binary files differnew file mode 100644 index 0000000..3d05294 --- /dev/null +++ b/assets/green-grass.jpg diff --git a/honey/ecs-systems.lua b/honey/ecs-systems.lua index b468228..1213763 100644 --- a/honey/ecs-systems.lua +++ b/honey/ecs-systems.lua @@ -38,39 +38,39 @@ end --===== transform cascading =====-- -transform = function(params) +node = function(params) return { db = params.db, priority = 2, update = function(self, dt) - local entities = self.db:queryComponent("transform") + local nodes = self.db:queryComponent("node") - -- prepare transforms - for id, transform in pairs(entities) do - transform._visited = false + -- prepare nodes + for id, node in pairs(nodes) do + node._visited = false end -- helper function - local function recursiveTransform(transform) - if transform._visited then - return transform._matrix + local function recursiveTransform(node) + if node._visited then + return node._matrix end - if not transform.parent then - transform._matrix = transform.matrix + if not node.parent then + node._matrix = node.matrix else - local parentTransform = self.db:getComponent(transform.parent, "transform") + local parentTransform = self.db:getComponent(node.parent, "node") local parentMatrix = recursiveTransform(parentTransform) - transform._matrix = parentMatrix * transform.matrix + node._matrix = parentMatrix * node.matrix end - transform._visited = true - return transform._matrix + node._visited = true + return node._matrix end - -- compute transforms - for id, transform in pairs(entities) do - recursiveTransform(transform) + -- compute nodes + for id, node in pairs(nodes) do + recursiveTransform(node) end end, } @@ -120,7 +120,7 @@ function renderCamera(params) update = function(self, dt) for id, camera in pairs(self.db:queryComponent("camera")) do local projection = camera.projection - local cameraTransform = self.db:getComponent(id, "transform") + local cameraTransform = self.db:getComponent(id, "node") local view = Mat4() if cameraTransform then honey.glm.mat4_inv(cameraTransform._matrix.data, view.data) @@ -131,9 +131,9 @@ function renderCamera(params) local entities = self.db:queryComponent("renderMesh") for entity, tbl in pairs(entities) do -- get model - local transform = self.db:getComponent(entity, "transform") + local node = self.db:getComponent(entity, "node") local model = - (transform and transform._matrix) or + (node and node._matrix) or Mat4():identity() -- get shader local shader = honey.shader.loadShader( @@ -237,7 +237,7 @@ physics = function(params) ) end ode.BodySetMass(body, mass) - local m = self.db:getComponent(id, "transform").matrix + local m = self.db:getComponent(id, "node").matrix ode.BodySetPosition( body, m[1][4], m[2][4], m[3][4] @@ -304,13 +304,13 @@ physics = function(params) -- remove all contact joints ode.JointGroupEmpty(self.contactGroup) - -- update entity transforms + -- update entity nodes for id, physics in pairs(query) do local x,y,z = ode.BodyGetPosition(physics._body) local d,a,b,c = ode.BodyGetQuaternion(physics._body) - local transform = self.db:getComponent(id, "transform") + local node = self.db:getComponent(id, "node") local q = Quaternion{a,b,c,d} - transform.matrix + node.matrix :identity() :translate(Vec3{x,y,z}) :mul(Quaternion{a,b,c,d}:toMat4()) @@ -336,9 +336,9 @@ local function createGeom(self, id, collision) elseif collision.class == "capsule" then geom = ode.CreateCapsule(self.space, collision.radius, collision.length) elseif collision.class == "plane" then - local transform = self.db:getComponent(id, "transform") - local m = transform.matrix - local normal = transform.matrix:mulv3(Vec3{0,1,0}):normalize() + local node = self.db:getComponent(id, "node") + local m = node.matrix + local normal = node.matrix:mulv3(Vec3{0,1,0}):normalize() local position = Vec3{m[1][4], m[2][4], m[3][4]} print(position) local d = normal:dot(position) @@ -36,19 +36,19 @@ end) local entities = ecs.EntityDb() local systems = ecs.SystemDb(entities) -systems:addSystem(sys.transform) +systems:addSystem(sys.node) systems:addSystem(sys.renderCamera) systems:addSystem(sys.script) systems:addSystem(sys.collision, {space=space}) systems:addSystem(sys.physics, {space=space, world=world}) package.loaded['baseRotationScript'] = function(entities, id, dt) - local transform = entities:getComponent(id, "transform") - transform.matrix:rotateZ(math.pi * dt) + local node = entities:getComponent(id, "node") + node.matrix:rotateZ(math.pi * dt) end package.loaded['cameraRotationScript'] = function(entities, id, dt) - local transform = entities:getComponent(id, "transform") + local node = entities:getComponent(id, "node") local z = entities:getComponent(id, "z") - transform.matrix + node.matrix :identity() :translate(Vec3{0, 0, z.value + math.sin(math.pi * glfw.GetTime())}) end @@ -65,7 +65,7 @@ function setupEntities() local plane = entities:createEntity() entities:addComponents(plane, { - transform = { + node = { matrix = Mat4() :identity() :rotateZ(math.rad(5)) @@ -76,7 +76,7 @@ function setupEntities() }) local planeMesh = entities:createEntityWithComponents{ - transform = { + node = { parent = plane, matrix = Mat4():identity():rotateX(0.5*math.pi):scale(Vec3{20,20,20}), }, @@ -96,7 +96,7 @@ function setupEntities() shader = { vertex="vertex.glsl", fragment="fragment.glsl" }, mesh = { filename="assets/icosahedron.obj", index=1 }, }, - transform = { + node = { matrix = Mat4() :identity() :translate(Vec3{0,1,0}) @@ -124,7 +124,7 @@ function setupEntities() --mesh = { filename="assets/tetrahedron.obj", index=1 }, mesh = { filename="builtin.quad", index=1 }, }, - transform = { + node = { parent=id, matrix=Mat4():identity():translate(Vec3{0, 2, 0}), }, @@ -139,7 +139,7 @@ function setupEntities() }) local capsule = entities:createEntityWithComponents{ - transform = { + node = { matrix = Mat4():identity():translate(Vec3{0,10,0}):rotateX(0.5*math.pi) }, collision = { @@ -165,7 +165,7 @@ function setupEntities() local pivotPivot = entities:createEntityWithComponents{ - transform = { + node = { parent = capsule, matrix = Mat4():identity():rotateX(-0.5*math.pi), }, @@ -173,7 +173,7 @@ function setupEntities() local capcamPivot = entities:createEntityWithComponents{ - transform = { + node = { parent = pivotPivot, matrix = Mat4():identity(), }, @@ -195,14 +195,14 @@ function setupEntities() print(dx,dy) prevx, prevy = data.xpos, data.ypos - local transform = entities:getComponent(id, "transform") + local node = entities:getComponent(id, "node") local py = entities:getComponent(id, "pitchyaw") py.pitch = py.pitch - dy py.yaw = py.yaw - dx if py.pitch > 89.9 then py.pitch = 89.9 end if py.pitch < -89.9 then py.pitch = -89.9 end - transform.matrix + node.matrix :identity() :rotateY(math.rad(py.yaw)) :rotateX(math.rad(py.pitch)) @@ -252,7 +252,7 @@ function setupEntities() projection = Mat4():perspective(math.rad(45), 640/480, 0.1, 1000), render="screen", }, - transform = { + node = { parent = capcamPivot, matrix = Mat4():identity():translate(Vec3{0,0,10}), }, @@ -261,7 +261,7 @@ function setupEntities() local skybox = entities:createEntityWithComponents{ - transform = { + node = { parent = capsule, matrix = Mat4():identity():scale(Vec3{2,2,2}), }, @@ -283,7 +283,7 @@ function setupEntities() projection=Mat4():perspective(math.rad(45), 640/480, 0.1, 1000), render="screen", }, - transform={ + node={ matrix=Mat4():identity():rotateX(math.rad(-20)):translate(Vec3{0, 5, 30}), }, z = {value=-60}, diff --git a/shaders/mesh.frag b/shaders/mesh.frag new file mode 100644 index 0000000..532d1ac --- /dev/null +++ b/shaders/mesh.frag @@ -0,0 +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); +} diff --git a/shaders/mesh.vert b/shaders/mesh.vert new file mode 100644 index 0000000..cb20d1f --- /dev/null +++ b/shaders/mesh.vert @@ -0,0 +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; +} diff --git a/shaders/quad.vert b/shaders/quad.vert new file mode 100644 index 0000000..cb20d1f --- /dev/null +++ b/shaders/quad.vert @@ -0,0 +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; +} |