diff options
-rw-r--r-- | demo/MeshInstance.lua | 14 | ||||
-rw-r--r-- | demo/Node.lua | 19 | ||||
-rw-r--r-- | demo/main.lua | 39 |
3 files changed, 46 insertions, 26 deletions
diff --git a/demo/MeshInstance.lua b/demo/MeshInstance.lua index 7164f18..a3e6da2 100644 --- a/demo/MeshInstance.lua +++ b/demo/MeshInstance.lua @@ -5,11 +5,17 @@ local MeshInstance = {} MeshInstance.prototype = {} setmetatable(MeshInstance.prototype, { __index = Node.prototype}) -MeshInstance.prototype.draw = function(self, shader, camera) +MeshInstance.prototype.draw = function(self, camera, shader) + local shader = shader or self.shader + honey.shader.set_mat4(shader, 'model', self.transform.array) honey.shader.set_mat4(shader, 'view', camera.view.array) honey.shader.set_mat4(shader, 'projection', camera.projection.array) honey.mesh.draw(self.mesh, shader) + + for _, child in ipairs(self.children) do + child:draw(camera) + end end -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -19,13 +25,13 @@ MeshInstance.mt.__index = MeshInstance.prototype -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -MeshInstance.new = function(parent, position, rotation, scale, mesh) +MeshInstance.new = function(parent, position, rotation, scale, mesh, shader) local meshinstance = Node.new(parent, position, rotation, scale) + meshinstance.mesh = mesh + meshinstance.shader = shader setmetatable(meshinstance, MeshInstance.mt) - meshinstance.mesh = mesh - return meshinstance end diff --git a/demo/Node.lua b/demo/Node.lua index 08916dd..ffb2aa5 100644 --- a/demo/Node.lua +++ b/demo/Node.lua @@ -8,16 +8,16 @@ Node.prototype = {} Node.prototype.updateTransform = function(self) honey.cglm.mat4.identity(self.transform.array) - self.transform:scale(self.scale) + self.transform:translate(self.position) self.transform:rotate(Vector.Vec3.ZERO, Vector.Vec3.Z_UNIT, self.rotation:at(2)) self.transform:rotate(Vector.Vec3.ZERO, Vector.Vec3.Y_UNIT, self.rotation:at(1)) self.transform:rotate(Vector.Vec3.ZERO, Vector.Vec3.X_UNIT, self.rotation:at(0)) - self.transform:translate(self.position) + self.transform:scale(self.scale) if self.parent ~= nil then - self.transform:mul(self.parent.transform, self.transform) + self.parent.transform:mul(self.transform, self.transform) end for _, child in ipairs(self.children) do @@ -25,20 +25,27 @@ Node.prototype.updateTransform = function(self) end end +Node.prototype.draw = function(self, camera, shader) + -- do not draw base nodes, but recursively draw children. + for _, child in ipairs(self.children) do + child:draw(camera, shader) + end +end + Node.prototype.translate = function(self, translation) self.position:add(translation, self.position) end Node.prototype.pitch = function(self, angle) - self.rotation:setAt(0, angle) + self.rotation:setAt(0, self.rotation:at(0) + angle) end Node.prototype.yaw = function(self, angle) - self.rotation:setAt(1, angle) + self.rotation:setAt(1, self.rotation:at(1) + angle) end Node.prototype.roll = function(self, angle) - self.rotation:setAt(2, angle) + self.rotation:setAt(2, self.rotation:at(2) + angle) end -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/demo/main.lua b/demo/main.lua index dea393c..e8d3cac 100644 --- a/demo/main.lua +++ b/demo/main.lua @@ -1,15 +1,11 @@ local Vector = require('Vector') local Matrix = require('Matrix') local FPSCamera = require('FPSCamera') +local Node = require('Node') local ScreenQuad = require('ScreenQuad') local MeshInstance = require('MeshInstance') FPSCamera.movement_speed = 5 -local model = Matrix.Mat4.eye() -model:rotate(Vector.Vec3.ZERO, Vector.Vec3.Y_UNIT, math.pi) -model:translate(Vector.Vec3.new{0,0,-2}) -print(model) - honey.input.key.bind(honey.input.key.escape, honey.exit) local buffer = false @@ -57,22 +53,34 @@ void main() { color = vec4(texture(tex, UV).xyz, 1); } ]] +local sceneRoot = Node.new(nil, + Vector.Vec3.new(), + Vector.Vec3.new(), + Vector.Vec3.new{1,1,1}) + local shader = honey.shader.new(vertex_shader, fragment_shader) -local suzanne = MeshInstance.new(nil, +local suzanne = MeshInstance.new(sceneRoot, Vector.Vec3.new{0,0,-3}, Vector.Vec3.new{0,math.pi,0}, - Vector.Vec3.new{1,1,1}, - honey.mesh.load('Suzanne.obj')[1]) + Vector.Vec3.new{0.5,1,0.5}, + honey.mesh.load('Suzanne.obj')[1], + shader) local plane = MeshInstance.new(suzanne, - Vector.Vec3.new{-2,5,0}, + Vector.Vec3.new{1,0,0}, Vector.Vec3.new{0,0,0}, Vector.Vec3.new{1,1,1}, - honey.primitives.plane(4,4)) + honey.primitives.plane(4,4), + shader) +local plane2 = MeshInstance.new(suzanne, + Vector.Vec3.new{5,0,0}, + Vector.Vec3.new{0,math.pi,0}, + Vector.Vec3.new{1,1,1}, + honey.primitives.plane(4,4), + shader) + suzanne.update = function(self, dt) - local movement = Vector.Vec3.new() - movement:add(Vector.Vec3.X_UNIT, movement) - movement:muls(dt, movement) - self:translate(movement) + local angle = dt * math.pi + self:yaw(angle) self:updateTransform() end @@ -94,8 +102,7 @@ end function draw_suzanne() honey.texture.use(tex, 0) - suzanne:draw(shader, FPSCamera) - plane:draw(shader, FPSCamera) + sceneRoot:draw(FPSCamera) end function honey.draw() |