diff options
author | sanine-a <sanine.not@pm.me> | 2020-11-01 13:04:05 -0600 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2020-11-01 13:04:05 -0600 |
commit | 8bb877ad57e7e41d928408e81f4e8cca63e171f7 (patch) | |
tree | 5cc4a79a492bd3c7224070b3bd00aba512a9b053 /demo | |
parent | cf75cc42cd11c31ff9402751bec98ba607fd820f (diff) |
add basic node types
Diffstat (limited to 'demo')
-rw-r--r-- | demo/Camera.lua | 34 | ||||
-rw-r--r-- | demo/FPSCamera.lua | 32 | ||||
-rw-r--r-- | demo/MeshInstance.lua | 30 | ||||
-rw-r--r-- | demo/Node.lua | 47 | ||||
-rw-r--r-- | demo/ScreenQuad.lua | 8 | ||||
-rw-r--r-- | demo/main.lua | 5 |
6 files changed, 133 insertions, 23 deletions
diff --git a/demo/Camera.lua b/demo/Camera.lua new file mode 100644 index 0000000..de14b6c --- /dev/null +++ b/demo/Camera.lua @@ -0,0 +1,34 @@ +local Vector = require('Vector') +local Matrix = require('Matrix') +local Node = require('Node') + +local Camera = {} + +Camera.prototype = {} +setmetatable(Camera.prototype, { __index = Node.prototype }) + +Camera.prototype.updateView = function(self) + self.basis = self.transform:basis() + Matrix.Mat4.look(self.position, self.basis.z, self.basis.y, self.view) +end + +-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Camera.mt = {} +Camera.mt.__index = Camera.prototype + +-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Camera.new = function(parent, position, rotation, scale, fov, aspect, near, far) + local camera = Node.new(parent, position, rotation, scale) + setmetatable(camera, Camera.mt) + + camera.view = Matrix.Mat4.new() + camera:updateView() + camera.projection = Matrix.Mat4.perspective(fov, aspect, near, far) + + return camera +end + +return Camera + diff --git a/demo/FPSCamera.lua b/demo/FPSCamera.lua index 7c33242..033f19b 100644 --- a/demo/FPSCamera.lua +++ b/demo/FPSCamera.lua @@ -1,7 +1,12 @@ local Vector = require('Vector') local Matrix = require('Matrix') +local Camera = require('Camera') -local camera = {} +local camera = Camera.new(nil, + Vector.Vec3.new(), + Vector.Vec3.new(), + Vector.Vec3.new{1,1,1}, + math.rad(90), 640/480, 0.1, 1000) camera.pitch = 0 camera.yaw = 0 @@ -10,22 +15,7 @@ camera.sensitivity = 0.1 camera.movement_speed = 1 -camera.position = Vector.Vec3.new{0,0,-1} - -camera.view = Matrix.Mat4.eye() -camera.basis = camera.view:basis() - -camera.projection = Matrix.Mat4.perspective(math.rad(90), - 640/480, - 0.1, - 8192) - function camera:update(dt) - local M = Matrix.Mat4.eye() - M:rotate(Vector.Vec3.ZERO, Vector.Vec3.Y_UNIT, math.rad(self.yaw)) - M:rotate(Vector.Vec3.ZERO, Vector.Vec3.X_UNIT, math.rad(self.pitch)) - self.basis = M:basis() - movement = Vector.Vec3.new() if honey.input.key.is_down(honey.input.key.w) then movement:add(self.basis.z, movement) @@ -39,7 +29,7 @@ function camera:update(dt) if honey.input.key.is_down(honey.input.key.d) then movement:sub(self.basis.x, movement) end - + movement:setAt(1, 0) movement:normalize() @@ -51,8 +41,9 @@ function camera:update(dt) end movement:muls(self.movement_speed*dt, movement) self.position:add(movement, self.position) - - Matrix.Mat4.look(self.position, self.basis.z, Vector.Vec3.Y_UNIT, self.view) + + self:updateTransform() + self:updateView() end camera.mouse_pos = {} @@ -71,6 +62,9 @@ honey.input.mouse.bind_movement( if camera.pitch > 89 then camera.pitch = 89 end if camera.pitch < -89 then camera.pitch = -89 end + + camera.rotation:setAt(0, math.rad(camera.pitch)) + camera.rotation:setAt(1, math.rad(camera.yaw)) end ) diff --git a/demo/MeshInstance.lua b/demo/MeshInstance.lua new file mode 100644 index 0000000..6e176fd --- /dev/null +++ b/demo/MeshInstance.lua @@ -0,0 +1,30 @@ +local Node = require('Node') + +local MeshInstance = {} + +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.mesh.draw(self.mesh, shader) +end + +-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +MeshInstance.mt = {} +MeshInstance.mt.__index = MeshInstance.prototype + +-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +MeshInstance.new = function(parent, position, rotation, scale, mesh) + local meshinstance = Node.new(parent, postion, rotation, scale) + setmetatable(meshinstance, MeshInstance.mt) + + meshinstance.mesh = mesh + + return meshinstance +end diff --git a/demo/Node.lua b/demo/Node.lua new file mode 100644 index 0000000..46c372a --- /dev/null +++ b/demo/Node.lua @@ -0,0 +1,47 @@ +local Vector = require('Vector') +local Matrix = require('Matrix') + +local Node = {} + +Node.prototype = {} +Node.prototype.updateTransform = function(self) + honey.cglm.mat4.identity(self.transform.array) + + self.transform:scale(self.scale) + + 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) + + if parent ~= nil then + self.transform:mul(self.parent.transform) + end +end + +-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Node.mt = {} +Node.mt.__index = Node.prototype + +-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Node.new = function(parent, position, rotation, scale) + local node = {} + + setmetatable(node, Node.mt) + + node.parent = parent + + node.position = position + node.rotation = rotation + node.scale = scale + + node.transform = Matrix.Mat4.eye() + node:updateTransform() + + return node +end + +return Node diff --git a/demo/ScreenQuad.lua b/demo/ScreenQuad.lua index 5d78cf3..70794c1 100644 --- a/demo/ScreenQuad.lua +++ b/demo/ScreenQuad.lua @@ -28,18 +28,18 @@ out vec4 color; void main() { - color = vec4(1 - texture(tex, UV).xyz, 1); + color = vec4(texture(tex, UV).xyz, 1); } ]] ScreenQuad.shader = honey.shader.new(vertexShader, fragmentShader) ScreenQuad.tex = honey.texture.new() ScreenQuad.depth = honey.texture.new() -honey.texture.create(ScreenQuad.tex, 'rgb', 640, 480); -honey.texture.create(ScreenQuad.depth, 'depth', 640, 480); +honey.texture.create(ScreenQuad.tex, 'rgb', 640, 640); +honey.texture.create(ScreenQuad.depth, 'depth', 640, 640); --honey.texture.load(ScreenQuad.texture, 'checkerboard.png', false) -ScreenQuad.fb = honey.texture.new_framebuffer(ScreenQuad.tex, ScreenQuad.depth, 640, 480) +ScreenQuad.fb = honey.texture.new_framebuffer(ScreenQuad.tex, ScreenQuad.depth, 640, 640) ScreenQuad.draw = function(self) honey.texture.use(self.tex, 0) diff --git a/demo/main.lua b/demo/main.lua index ab3e8b3..1ae1e0e 100644 --- a/demo/main.lua +++ b/demo/main.lua @@ -67,6 +67,8 @@ local color = Vector.Vec4.new() local total_frames = 0 local total_time = 0 +honey.window.set_size(640, 480) + function honey.update(dt) total_time = total_time + dt FPSCamera:update(dt) @@ -92,16 +94,19 @@ function honey.draw() if buffer then honey.set_framebuffer(ScreenQuad.fb) + honey.set_viewport_size(640,640) honey.clear_color(Vector.Vec4.new().array, true, true, false) honey.enable_depth_test(true) draw_suzanne() honey.set_framebuffer(0) + honey.set_viewport_size(640, 480) honey.enable_depth_test(false) honey.clear_color(Vector.Vec4.new{0,0,1,1}.array, true, false, false) ScreenQuad:draw() else honey.clear_color(Vector.Vec4.new{1,1,0,1}.array, true, true, false) + honey.set_viewport_size(640, 480) honey.enable_depth_test(true) draw_suzanne() end |