From 8bb877ad57e7e41d928408e81f4e8cca63e171f7 Mon Sep 17 00:00:00 2001 From: sanine-a Date: Sun, 1 Nov 2020 13:04:05 -0600 Subject: add basic node types --- demo/Camera.lua | 34 ++++++++++++++++++++++++++++++++++ demo/FPSCamera.lua | 32 +++++++++++++------------------- demo/MeshInstance.lua | 30 ++++++++++++++++++++++++++++++ demo/Node.lua | 47 +++++++++++++++++++++++++++++++++++++++++++++++ demo/ScreenQuad.lua | 8 ++++---- demo/main.lua | 5 +++++ src/honey.c | 13 +++++++++++++ src/texture/texture.c | 2 ++ 8 files changed, 148 insertions(+), 23 deletions(-) create mode 100644 demo/Camera.lua create mode 100644 demo/MeshInstance.lua create mode 100644 demo/Node.lua 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 diff --git a/src/honey.c b/src/honey.c index ce1a38c..4ffb83f 100644 --- a/src/honey.c +++ b/src/honey.c @@ -95,6 +95,16 @@ int honey_lua_enable_depth_test(lua_State* L) return 0; } +int honey_lua_set_viewport_size(lua_State* L) +{ + int width, height; + honey_lua_parse_arguments(L, 2, + HONEY_INTEGER, &width, + HONEY_INTEGER, &height); + glViewport(0,0,width,height); + return 0; +} + /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ bool honey_setup(lua_State** L) @@ -139,6 +149,9 @@ bool honey_setup(lua_State** L) lua_pushcfunction(*L, honey_lua_enable_depth_test); lua_setfield(*L, -2, "enable_depth_test"); + lua_pushcfunction(*L, honey_lua_set_viewport_size); + lua_setfield(*L, -2, "set_viewport_size"); + lua_setglobal(*L, "honey"); return true; diff --git a/src/texture/texture.c b/src/texture/texture.c index 9ddf441..37e38e9 100644 --- a/src/texture/texture.c +++ b/src/texture/texture.c @@ -246,6 +246,8 @@ void honey_texture_framebuffer_object_new(unsigned int* destination, honey_texture* depth, int width, int height) { + glBindTexture(GL_TEXTURE_2D, 0); + glGenFramebuffers(1, destination); glBindFramebuffer(GL_FRAMEBUFFER, *destination); -- cgit v1.2.1