diff options
author | sanine <sanine.not@pm.me> | 2023-05-07 22:12:00 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-05-07 22:12:00 -0500 |
commit | 09fa1809ab285d41180a5767d8cf408855986129 (patch) | |
tree | 6c024ef9f0cf4b47983c9c5f7d2ad3114981b67e | |
parent | 61f8813b0beb6228f99cf0ea80acee5f4cab063f (diff) |
implement basic third-person camera
-rw-r--r-- | assets/attribution.txt | 1 | ||||
-rw-r--r-- | assets/skybox.mtl | 13 | ||||
-rw-r--r-- | assets/skybox.obj | 46 | ||||
-rw-r--r-- | assets/skyboxsun5deg2_tn.jpg | bin | 0 -> 20477 bytes | |||
-rw-r--r-- | honey/ecs-systems.lua | 2 | ||||
-rw-r--r-- | honey/window.lua | 8 | ||||
-rw-r--r-- | main.lua | 87 |
7 files changed, 152 insertions, 5 deletions
diff --git a/assets/attribution.txt b/assets/attribution.txt new file mode 100644 index 0000000..9755abb --- /dev/null +++ b/assets/attribution.txt @@ -0,0 +1 @@ +skyboxsun5deg2_tn.jpg: https://reije081.home.xs4all.nl/skyboxes/ diff --git a/assets/skybox.mtl b/assets/skybox.mtl new file mode 100644 index 0000000..60d07e9 --- /dev/null +++ b/assets/skybox.mtl @@ -0,0 +1,13 @@ +# Blender MTL File: 'None' +# Material Count: 1 + +newmtl Material.001 +Ns 250.000000 +Ka 1.000000 1.000000 1.000000 +Kd 0.800000 0.800000 0.800000 +Ks 0.500000 0.500000 0.500000 +Ke 0.000000 0.000000 0.000000 +Ni 1.450000 +d 1.000000 +illum 2 +map_Kd skyboxsun5deg2_tn.jpg diff --git a/assets/skybox.obj b/assets/skybox.obj new file mode 100644 index 0000000..1a34cf8 --- /dev/null +++ b/assets/skybox.obj @@ -0,0 +1,46 @@ +# Blender v3.5.1 OBJ File: '' +# www.blender.org +mtllib skybox.mtl +o Cube_Cube.001 +v -100.000000 -100.000000 100.000000 +v -100.000000 100.000000 100.000000 +v -100.000000 -100.000000 -100.000000 +v -100.000000 100.000000 -100.000000 +v 100.000000 -100.000000 100.000000 +v 100.000000 100.000000 100.000000 +v 100.000000 -100.000000 -100.000000 +v 100.000000 100.000000 -100.000000 +vt 0.750000 0.334117 +vt 1.000000 0.665883 +vt 1.000000 0.334117 +vt 0.500000 0.334117 +vt 0.750000 0.665883 +vt 0.250000 0.334117 +vt 0.500000 0.665883 +vt -0.000000 0.334117 +vt 0.250000 0.665883 +vt 0.250000 0.002352 +vt 0.500000 0.002352 +vt 0.500000 0.997648 +vt -0.000000 0.665883 +vt 0.250000 0.997648 +vn 1.0000 0.0000 0.0000 +vn 0.0000 0.0000 1.0000 +vn -1.0000 0.0000 0.0000 +vn 0.0000 0.0000 -1.0000 +vn 0.0000 1.0000 0.0000 +vn 0.0000 -1.0000 0.0000 +usemtl Material.001 +s off +f 3/1/1 2/2/1 1/3/1 +f 7/4/2 4/5/2 3/1/2 +f 5/6/3 8/7/3 7/4/3 +f 1/8/4 6/9/4 5/6/4 +f 1/10/5 7/4/5 3/11/5 +f 6/9/6 4/12/6 8/7/6 +f 3/1/1 4/5/1 2/2/1 +f 7/4/2 8/7/2 4/5/2 +f 5/6/3 6/9/3 8/7/3 +f 1/8/4 2/13/4 6/9/4 +f 1/10/5 5/6/5 7/4/5 +f 6/9/6 2/14/6 4/12/6 diff --git a/assets/skyboxsun5deg2_tn.jpg b/assets/skyboxsun5deg2_tn.jpg Binary files differnew file mode 100644 index 0000000..d3e9904 --- /dev/null +++ b/assets/skyboxsun5deg2_tn.jpg diff --git a/honey/ecs-systems.lua b/honey/ecs-systems.lua index 3243d02..b468228 100644 --- a/honey/ecs-systems.lua +++ b/honey/ecs-systems.lua @@ -261,7 +261,7 @@ physics = function(params) physics.angularVelocity = avel if physics.maxAngularSpeed then - ode.BodySetMaxAngularSpeed(physics.maxAngularSpeed) + ode.BodySetMaxAngularSpeed(body, physics.maxAngularSpeed) end physics._body = body diff --git a/honey/window.lua b/honey/window.lua index fa777d0..3b5d5c1 100644 --- a/honey/window.lua +++ b/honey/window.lua @@ -150,6 +150,14 @@ end function Window.setKeyCallback(self, cb) return glfw.SetKeyCallback(self.win, cb) end +function Window.setCursorPosCallback(self, cb) + return glfw.SetCursorPosCallback(self.win, cb) +end + + +function Window.setInputMode(self, mode, value) + return glfw.SetInputMode(self.win, mode, value) +end function Window.swapBuffers(self) @@ -14,6 +14,7 @@ local nvg = honey.nvg -- initialize honey local window = honey.init() +window:setInputMode(glfw.CURSOR, glfw.CURSOR_DISABLED) -- setup vector graphics local vg = nvg.CreateContext() @@ -127,7 +128,7 @@ function setupEntities() local capsule = entities:createEntityWithComponents{ transform = { - matrix = Mat4():identity():translate(Vec3{0,10,0}) + matrix = Mat4():identity():translate(Vec3{0,10,0}):rotateX(0.5*math.pi) }, collision = { class = "capsule", @@ -142,6 +143,7 @@ function setupEntities() radius = 1, length = 2, }, + maxAngularSpeed = 0, }, renderMesh = { mesh = { filename="assets/capsule.obj", index=1 }, @@ -150,14 +152,85 @@ function setupEntities() } + local pivotPivot = entities:createEntityWithComponents{ + transform = { + parent = capsule, + matrix = Mat4():identity():rotateX(-0.5*math.pi), + }, + } + + + local capcamPivot = entities:createEntityWithComponents{ + transform = { + parent = pivotPivot, + matrix = Mat4():identity(), + }, + pitchyaw = { + pitch = 0, + yaw = 0, + }, + onCursorPos = { script = "cameraCursorPos" }, + } + package.loaded["cameraCursorPos"] = (function() + local prevx, prevy = 0, 0 + return function(entities, id, data) + local dx = data.xpos - prevx + local dy = data.ypos - prevy + prevx, prevy = data.xpos, data.ypos + + local transform = entities:getComponent(id, "transform") + local py = entities:getComponent(id, "pitchyaw") + py.pitch = py.pitch - dy + py.yaw = py.yaw - dx + if py.pitch > 89.9 then py.pitch = 89.9 end + if py.pitch < -89.9 then py.pitch = -89.9 end + + transform.matrix + :identity() + :rotateY(math.rad(py.yaw)) + :rotateX(math.rad(py.pitch)) + end + end)() + + + local capcam = entities:createEntityWithComponents{ + camera = { + projection = Mat4():perspective(math.rad(45), 640/480, 0.1, 1000), + render="screen", + }, + transform = { + parent = capcamPivot, + matrix = Mat4():identity():translate(Vec3{0,0,10}), + }, + onWindowResize = { script = "cameraHandleResize" }, + } + + + local skybox = entities:createEntityWithComponents{ + transform = { + parent = capsule, + matrix = Mat4():identity():scale(Vec3{2,2,2}), + }, + renderMesh = { + mesh = { filename="assets/skybox.obj", index=1 }, + shader = { vertex="vertex.glsl", fragment="fragment.glsl" }, + textures = { + ourTexture = { + filename = "assets/skyboxsun5deg2_tn.jpg" + } + }, + }, + } + + local camera = entities:createEntity() entities:addComponents(camera, { - camera={ - projection=Mat4():perspective(math.rad(45), 640/480, 0.1, 100), + ccamera={ + projection=Mat4():perspective(math.rad(45), 640/480, 0.1, 1000), render="screen", }, transform={ - matrix=Mat4():identity():rotateX(math.rad(-20)):translate(Vec3{0, 10, 30}), + matrix=Mat4():identity():rotateX(math.rad(-20)):translate(Vec3{0, 5, 30}), }, z = {value=-60}, onKey = { @@ -184,6 +257,11 @@ window:setKeyCallback(function(_, key, scancode, action) end end) + +window:setCursorPosCallback(function(_, xpos, ypos) + dispatch(entities, "onCursorPos", {xpos=xpos, ypos=ypos}) +end) + -- resize window correctly window:setFramebufferSizeCallback(function(_, width, height) gl.Viewport(0, 0, width, height) @@ -193,6 +271,7 @@ end) package.loaded["cameraHandleResize"] = function(entities, id, data) local camera = entities:getComponent(id, "camera") + if not camera then return end camera.projection:perspectiveResize(data.width/data.height) end |