summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsanine-a <sanine.not@pm.me>2020-11-07 13:14:30 -0600
committersanine-a <sanine.not@pm.me>2020-11-07 13:14:30 -0600
commit94572182ea873d5dadbc8428330d4c00eead2c80 (patch)
treeb9152a7d474c236c29d42e133300b8513690bfef
parent8bb877ad57e7e41d928408e81f4e8cca63e171f7 (diff)
add basic Node transform functions and implement transform cascading
-rw-r--r--demo/FPSCamera.lua6
-rw-r--r--demo/Matrix.lua2
-rw-r--r--demo/MeshInstance.lua8
-rw-r--r--demo/Node.lua32
-rw-r--r--demo/ScreenQuad.lua6
-rw-r--r--demo/main.lua35
6 files changed, 64 insertions, 25 deletions
diff --git a/demo/FPSCamera.lua b/demo/FPSCamera.lua
index 033f19b..5a9fabf 100644
--- a/demo/FPSCamera.lua
+++ b/demo/FPSCamera.lua
@@ -40,7 +40,7 @@ function camera:update(dt)
movement:sub(Vector.Vec3.Y_UNIT, movement)
end
movement:muls(self.movement_speed*dt, movement)
- self.position:add(movement, self.position)
+ self:translate(movement)
self:updateTransform()
self:updateView()
@@ -60,8 +60,8 @@ honey.input.mouse.bind_movement(
camera.pitch = camera.pitch + camera.sensitivity * dy
camera.yaw = camera.yaw - camera.sensitivity * dx
- if camera.pitch > 89 then camera.pitch = 89 end
- if camera.pitch < -89 then camera.pitch = -89 end
+ if camera.pitch > 89.9 then camera.pitch = 89.9 end
+ if camera.pitch < -89.9 then camera.pitch = -89.9 end
camera.rotation:setAt(0, math.rad(camera.pitch))
camera.rotation:setAt(1, math.rad(camera.yaw))
diff --git a/demo/Matrix.lua b/demo/Matrix.lua
index d38b20b..d07944e 100644
--- a/demo/Matrix.lua
+++ b/demo/Matrix.lua
@@ -199,7 +199,7 @@ Matrix.Mat4.prototype.mul = function(self, M, dest)
result = dest
end
- honey.cglm.mat4.mul(self.array, M.array, result)
+ honey.cglm.mat4.mul(self.array, M.array, result.array)
return result
end
diff --git a/demo/MeshInstance.lua b/demo/MeshInstance.lua
index 6e176fd..7164f18 100644
--- a/demo/MeshInstance.lua
+++ b/demo/MeshInstance.lua
@@ -6,10 +6,9 @@ MeshInstance.prototype = {}
setmetatable(MeshInstance.prototype, { __index = Node.prototype})
MeshInstance.prototype.draw = function(self, shader, camera)
- honey.texture.use(self.texture, 0)
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.view.array)
+ honey.shader.set_mat4(shader, 'projection', camera.projection.array)
honey.mesh.draw(self.mesh, shader)
end
@@ -21,10 +20,13 @@ MeshInstance.mt.__index = MeshInstance.prototype
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
MeshInstance.new = function(parent, position, rotation, scale, mesh)
- local meshinstance = Node.new(parent, postion, rotation, scale)
+ local meshinstance = Node.new(parent, position, rotation, scale)
+
setmetatable(meshinstance, MeshInstance.mt)
meshinstance.mesh = mesh
return meshinstance
end
+
+return MeshInstance
diff --git a/demo/Node.lua b/demo/Node.lua
index 46c372a..08916dd 100644
--- a/demo/Node.lua
+++ b/demo/Node.lua
@@ -4,6 +4,7 @@ local Matrix = require('Matrix')
local Node = {}
Node.prototype = {}
+
Node.prototype.updateTransform = function(self)
honey.cglm.mat4.identity(self.transform.array)
@@ -15,11 +16,31 @@ Node.prototype.updateTransform = function(self)
self.transform:translate(self.position)
- if parent ~= nil then
- self.transform:mul(self.parent.transform)
+ if self.parent ~= nil then
+ self.transform:mul(self.parent.transform, self.transform)
+ end
+
+ for _, child in ipairs(self.children) do
+ child:updateTransform()
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)
+end
+
+Node.prototype.yaw = function(self, angle)
+ self.rotation:setAt(1, angle)
+end
+
+Node.prototype.roll = function(self, angle)
+ self.rotation:setAt(2, angle)
+end
+
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Node.mt = {}
@@ -37,6 +58,13 @@ Node.new = function(parent, position, rotation, scale)
node.position = position
node.rotation = rotation
node.scale = scale
+
+ node.children = {}
+
+ if parent ~= nil then
+ local index = #parent.children
+ parent.children[index + 1] = node
+ end
node.transform = Matrix.Mat4.eye()
node:updateTransform()
diff --git a/demo/ScreenQuad.lua b/demo/ScreenQuad.lua
index 70794c1..ba9c27e 100644
--- a/demo/ScreenQuad.lua
+++ b/demo/ScreenQuad.lua
@@ -35,11 +35,11 @@ ScreenQuad.shader = honey.shader.new(vertexShader, fragmentShader)
ScreenQuad.tex = honey.texture.new()
ScreenQuad.depth = honey.texture.new()
-honey.texture.create(ScreenQuad.tex, 'rgb', 640, 640);
-honey.texture.create(ScreenQuad.depth, 'depth', 640, 640);
+honey.texture.create(ScreenQuad.tex, 'rgb', 640, 480);
+honey.texture.create(ScreenQuad.depth, 'depth', 640, 480);
--honey.texture.load(ScreenQuad.texture, 'checkerboard.png', false)
-ScreenQuad.fb = honey.texture.new_framebuffer(ScreenQuad.tex, ScreenQuad.depth, 640, 640)
+ScreenQuad.fb = honey.texture.new_framebuffer(ScreenQuad.tex, ScreenQuad.depth, 640, 480)
ScreenQuad.draw = function(self)
honey.texture.use(self.tex, 0)
diff --git a/demo/main.lua b/demo/main.lua
index 1ae1e0e..dea393c 100644
--- a/demo/main.lua
+++ b/demo/main.lua
@@ -2,6 +2,7 @@ local Vector = require('Vector')
local Matrix = require('Matrix')
local FPSCamera = require('FPSCamera')
local ScreenQuad = require('ScreenQuad')
+local MeshInstance = require('MeshInstance')
FPSCamera.movement_speed = 5
local model = Matrix.Mat4.eye()
@@ -57,12 +58,23 @@ void main() {
} ]]
local shader = honey.shader.new(vertex_shader, fragment_shader)
-local suzanne = honey.mesh.load('Suzanne.obj')[1]
-local plane = honey.primitives.plane(4,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 suzanne = MeshInstance.new(nil,
+ 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])
+local plane = MeshInstance.new(suzanne,
+ Vector.Vec3.new{-2,5,0},
+ Vector.Vec3.new{0,0,0},
+ Vector.Vec3.new{1,1,1},
+ honey.primitives.plane(4,4))
+suzanne.update = function(self, dt)
+ local movement = Vector.Vec3.new()
+ movement:add(Vector.Vec3.X_UNIT, movement)
+ movement:muls(dt, movement)
+ self:translate(movement)
+ self:updateTransform()
+end
local total_frames = 0
local total_time = 0
@@ -72,6 +84,7 @@ honey.window.set_size(640, 480)
function honey.update(dt)
total_time = total_time + dt
FPSCamera:update(dt)
+ suzanne:update(dt)
if total_time > 1 then
print('FPS: '..tostring(total_frames/total_time))
total_time = 0
@@ -81,12 +94,8 @@ end
function draw_suzanne()
honey.texture.use(tex, 0)
- honey.shader.set_mat4(shader, 'model', model.array)
- honey.shader.set_mat4(shader, 'view', FPSCamera.view.array)
- honey.shader.set_mat4(shader, 'projection', FPSCamera.projection.array)
- honey.shader.set_float(shader, 'time', total_time)
- honey.mesh.draw(suzanne, shader)
- honey.mesh.draw(plane, shader)
+ suzanne:draw(shader, FPSCamera)
+ plane:draw(shader, FPSCamera)
end
function honey.draw()
@@ -94,7 +103,7 @@ function honey.draw()
if buffer then
honey.set_framebuffer(ScreenQuad.fb)
- honey.set_viewport_size(640,640)
+ honey.set_viewport_size(480,640)
honey.clear_color(Vector.Vec4.new().array, true, true, false)
honey.enable_depth_test(true)
draw_suzanne()