diff options
Diffstat (limited to 'demo')
-rw-r--r-- | demo/Camera.lua | 15 | ||||
-rw-r--r-- | demo/FPSCamera.lua | 22 | ||||
-rw-r--r-- | demo/MeshInstance.lua | 6 | ||||
-rw-r--r-- | demo/ScreenQuad.lua | 2 | ||||
-rw-r--r-- | demo/Shader.lua | 53 | ||||
-rw-r--r-- | demo/SpatialShader.lua | 42 | ||||
-rw-r--r-- | demo/main.lua | 100 |
7 files changed, 137 insertions, 103 deletions
diff --git a/demo/Camera.lua b/demo/Camera.lua index de14b6c..fda23f2 100644 --- a/demo/Camera.lua +++ b/demo/Camera.lua @@ -1,15 +1,11 @@ -local Vector = require('Vector') -local Matrix = require('Matrix') -local Node = require('Node') - local Camera = {} Camera.prototype = {} -setmetatable(Camera.prototype, { __index = Node.prototype }) +setmetatable(Camera.prototype, { __index = honey.nodeMetatable.__index }) Camera.prototype.updateView = function(self) self.basis = self.transform:basis() - Matrix.Mat4.look(self.position, self.basis.z, self.basis.y, self.view) + self.view:look(self.position, self.basis.z, self.basis.y) end -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -20,12 +16,13 @@ Camera.mt.__index = Camera.prototype -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Camera.new = function(parent, position, rotation, scale, fov, aspect, near, far) - local camera = Node.new(parent, position, rotation, scale) + local camera = honey.node(parent, position, rotation, scale) setmetatable(camera, Camera.mt) - camera.view = Matrix.Mat4.new() + camera.view = honey.glm.mat4() camera:updateView() - camera.projection = Matrix.Mat4.perspective(fov, aspect, near, far) + camera.projection = honey.glm.mat4() + camera.projection:perspective(fov, aspect, near, far) return camera end diff --git a/demo/FPSCamera.lua b/demo/FPSCamera.lua index 5a9fabf..a1ab00f 100644 --- a/demo/FPSCamera.lua +++ b/demo/FPSCamera.lua @@ -1,11 +1,9 @@ -local Vector = require('Vector') -local Matrix = require('Matrix') local Camera = require('Camera') local camera = Camera.new(nil, - Vector.Vec3.new(), - Vector.Vec3.new(), - Vector.Vec3.new{1,1,1}, + honey.glm.vec3(), + honey.glm.vec3(), + honey.glm.vec3{1,1,1}, math.rad(90), 640/480, 0.1, 1000) camera.pitch = 0 @@ -16,7 +14,7 @@ camera.sensitivity = 0.1 camera.movement_speed = 1 function camera:update(dt) - movement = Vector.Vec3.new() + movement = honey.glm.vec3() if honey.input.key.is_down(honey.input.key.w) then movement:add(self.basis.z, movement) end @@ -30,14 +28,14 @@ function camera:update(dt) movement:sub(self.basis.x, movement) end - movement:setAt(1, 0) + movement:set(1, 0) movement:normalize() if honey.input.key.is_down(honey.input.key.left_shift) then - movement:add(Vector.Vec3.Y_UNIT, movement) + movement:add(honey.glm.UNIT_Y, movement) end if honey.input.key.is_down(honey.input.key.left_control) then - movement:sub(Vector.Vec3.Y_UNIT, movement) + movement:sub(honey.glm.UNIT_Y, movement) end movement:muls(self.movement_speed*dt, movement) self:translate(movement) @@ -63,11 +61,11 @@ honey.input.mouse.bind_movement( 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)) + camera.rotation:set(0, math.rad(camera.pitch)) + camera.rotation:set(1, math.rad(camera.yaw)) end ) -honey.input.mouse.set_mode(honey.input.mouse.mode.disabled) +honey.input.mouse.set_mode(honey.input.mouse.mode.captured) return camera diff --git a/demo/MeshInstance.lua b/demo/MeshInstance.lua index b12edba..eceec5d 100644 --- a/demo/MeshInstance.lua +++ b/demo/MeshInstance.lua @@ -1,9 +1,7 @@ -local Node = require('Node') - local MeshInstance = {} MeshInstance.prototype = {} -setmetatable(MeshInstance.prototype, { __index = Node.prototype}) +setmetatable(MeshInstance.prototype, { __index = honey.nodeMetatable.__index }) MeshInstance.prototype.draw = function(self, camera, shader) local shader = shader or self.shader @@ -20,7 +18,7 @@ MeshInstance.mt.__index = MeshInstance.prototype -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ MeshInstance.new = function(parent, position, rotation, scale, mesh, shader) - local meshinstance = Node.new(parent, position, rotation, scale) + local meshinstance = honey.node(parent, position, rotation, scale) meshinstance.mesh = mesh meshinstance.shader = shader diff --git a/demo/ScreenQuad.lua b/demo/ScreenQuad.lua index ba9c27e..4cc247f 100644 --- a/demo/ScreenQuad.lua +++ b/demo/ScreenQuad.lua @@ -31,7 +31,7 @@ void main() color = vec4(texture(tex, UV).xyz, 1); } ]] -ScreenQuad.shader = honey.shader.new(vertexShader, fragmentShader) +ScreenQuad.shader = honey.shader(vertexShader, fragmentShader) ScreenQuad.tex = honey.texture.new() ScreenQuad.depth = honey.texture.new() diff --git a/demo/Shader.lua b/demo/Shader.lua deleted file mode 100644 index 494bd53..0000000 --- a/demo/Shader.lua +++ /dev/null @@ -1,53 +0,0 @@ -local Shader = {} - -Shader.prototype = {} - -Shader.prototype.setInteger = function(self, uniform, integer) - honey.shader.set_int(self.program, uniform, integer) -end - -Shader.prototype.setFloat = function(self, uniform, float) - honey.shader.set_float(self.program, uniform, float) -end - -Shader.prototype.setVec3 = function(self, uniform, vector) - honey.shader.set_vec3(self.program, uniform, vector.array) -end - -Shader.prototype.setVec4 = function(self, uniform, vector) - honey.shader.set_vec4(self.program, uniform, vector.array) -end - -Shader.prototype.setMat3 = function(self, uniform, matrix) - honey.shader.set_mat3(self.program, uniform, matrix.array) -end - -Shader.prototype.setMat4 = function(self, uniform, matrix) - honey.shader.set_mat4(self.program, uniform, matrix.array) -end - -Shader.prototype.drawMesh = function(self, mesh) - honey.mesh.draw(mesh.mesh, self.program) -end - -Shader.prototype.delete = function(self) - honey.shader.delete(self.program) -end - --- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Shader.mt = {} -Shader.mt.__index = Shader.prototype - --- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Shader.new = function(vertexCode, fragmentCode) - local shader = {} - shader.program = honey.shader.new(vertexCode, fragmentCode) - - setmetatable(shader, Shader.mt) - - return shader -end - -return Shader diff --git a/demo/SpatialShader.lua b/demo/SpatialShader.lua index 4a9f979..5f83e59 100644 --- a/demo/SpatialShader.lua +++ b/demo/SpatialShader.lua @@ -1,5 +1,3 @@ -local Shader = require('Shader') - local VertexCode = [[ #version 330 core @@ -42,17 +40,44 @@ void main() local SpatialShader = {} SpatialShader.prototype = {} -setmetatable(SpatialShader.prototype, { __index = Shader.prototype }) + +SpatialShader.prototype.use = function(self) + self.shader:use() +end + +SpatialShader.prototype.setInteger = function(self, name, value) + self.shader:setInteger(name, value) +end + +SpatialShader.prototype.setFloat = function(self, name, value) + self.shader:setFloat(name, value) +end + +SpatialShader.prototype.setVec3 = function(self, name, value) + self.shader:setVec3(name, value) +end + +SpatialShader.prototype.setVec4 = function(self, name, value) + self.shader:setVec4(name, value) +end + +SpatialShader.prototype.setMat3 = function(self, name, value) + self.shader:setMat3(name, value) +end + +SpatialShader.prototype.setMat4 = function(self, name, value) + self.shader:setMat4(name, value) +end SpatialShader.prototype.setCamera = function(self, camera) - self:setMat4('view', camera.view) - self:setMat4('projection', camera.projection) + self.shader:setMat4('view', camera.view) + self.shader:setMat4('projection', camera.projection) end SpatialShader.prototype.drawMesh = function(self, mesh) - self:setMat4('model', mesh.transform) + self.shader:setMat4('model', mesh.transform) honey.texture.use(self.albedo, 0) - honey.mesh.draw(mesh.mesh, self.program) + honey.mesh.draw(mesh.mesh, self.shader) end -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -63,7 +88,8 @@ SpatialShader.mt.__index = SpatialShader.prototype -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ SpatialShader.new = function(albedo) - local spatialshader = Shader.new(VertexCode, FragmentCode) + local spatialshader = {} + spatialshader.shader = honey.shader(VertexCode, FragmentCode) spatialshader.albedo = albedo setmetatable(spatialshader, SpatialShader.mt) diff --git a/demo/main.lua b/demo/main.lua index ec0bac1..3382d8d 100644 --- a/demo/main.lua +++ b/demo/main.lua @@ -1,22 +1,90 @@ -local root = honey.node(nil, - honey.glm.vec3{0,0,0}, - honey.glm.vec3{0,0,0}, - honey.glm.vec3{1,1,1}) +local FPSCamera = require('FPSCamera') +local SpatialShader = require('SpatialShader') +local ScreenQuad = require('ScreenQuad') +local MeshInstance = require('MeshInstance') +FPSCamera.movement_speed = 5 -local child = honey.node(root, - honey.glm.vec3{0,0,0}, - honey.glm.vec3{0,0,0}, - honey.glm.vec3{1,1,1}) +honey.input.key.bind(honey.input.key.escape, honey.exit) -local v = honey.glm.vec3{1, 2, 3} +local buffer = false +honey.input.key.bind(honey.input.key.f, function(action) if action == 1 then buffer = not buffer end end) -local M = honey.glm.mat4() -M:set(1,3, 1) +local tex = honey.texture.new() +honey.texture.load(tex, 'checkerboard.png', false) -print(M:mul(M)) +local sceneRoot = honey.node(nil, + honey.glm.vec3(), + honey.glm.vec3(), + honey.glm.vec3{1,1,1}) -print(v) -print(honey.glm.vec3{2, 0.001, 0}) -print(honey.glm.vec4{100, 0, 0, 20.44}) +local shader = SpatialShader.new(tex) +local lightDirection = honey.glm.vec3{1,1,1} +lightDirection:normalize() +shader:setVec3('directional_lights[0].direction', lightDirection) +shader:setVec3('directional_lights[0].color', honey.glm.vec3{0,1,0}) +local meshes = honey.mesh.load('Suzanne.obj') +local suzanne = MeshInstance.new(sceneRoot, + honey.glm.vec3{0,0,3}, + honey.glm.vec3{0,math.pi,0}, + honey.glm.vec3{0.5,1,0.5}, + meshes[1], + shader) +local plane = MeshInstance.new(suzanne, + honey.glm.vec3{1,0,0}, + honey.glm.vec3{0,0,0}, + honey.glm.vec3{1,1,1}, + honey.primitives.plane(4,4), + shader) +local plane2 = MeshInstance.new(suzanne, + honey.glm.vec3{5,0,0}, + honey.glm.vec3{0,math.pi,0}, + honey.glm.vec3{1,1,1}, + honey.primitives.plane(4,4), + shader) -v.dot(v, v) +suzanne.update = function(self, dt) + self:rotate('y', dt) +end + +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) + sceneRoot:updateCascade(dt) + if total_time > 1 then + print('FPS: '..tostring(total_frames/total_time)) + total_time = 0 + total_frames = 0 + end +end + +function draw_suzanne() + sceneRoot:drawCascade(FPSCamera) +end + +function honey.draw() + total_frames = total_frames + 1 + + if buffer then + honey.set_framebuffer(ScreenQuad.fb) + honey.set_viewport_size(480,640) + honey.clear_color(honey.glm.vec4(), 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(honey.glm.vec4{0,0,1,1}, true, false, false) + ScreenQuad:draw() + else + honey.clear_color(honey.glm.vec4{1,1,0,1}, true, true, false) + honey.set_viewport_size(640, 480) + honey.enable_depth_test(true) + draw_suzanne() + end +end |