From 8f0b7b7dd4f780c932e902eeebaea157753f567b Mon Sep 17 00:00:00 2001 From: Jay Robson Date: Wed, 24 Jan 2024 22:01:33 +1100 Subject: [PATCH] added baked textures --- src/graphics/camera.cpp | 29 ++++++++++++++++------------- src/graphics/camera.hpp | 2 +- src/graphics/window.cpp | 7 +++---- src/main.cpp | 18 ++++++++++++++++++ 4 files changed, 38 insertions(+), 18 deletions(-) diff --git a/src/graphics/camera.cpp b/src/graphics/camera.cpp index 1e8653a..4bdf2a4 100644 --- a/src/graphics/camera.cpp +++ b/src/graphics/camera.cpp @@ -6,6 +6,7 @@ #include "input/keyboard.hpp" #include "../math.hpp" +#include #include #include #include @@ -34,10 +35,10 @@ void camera::move(double xoff, double yoff, double zoff) pos.z += zoff; } -void camera::update() +void camera::update(double dt) { glm::vec<2, double> off(0, 0); - double m = 0.002; + double m = 30; if(keyboard::is_pressed(GLFW_KEY_W)) off.y += 1; @@ -50,7 +51,7 @@ void camera::update() if(keyboard::is_pressed(GLFW_KEY_LEFT_SHIFT)) m *= 1.5; if(off.x != 0 || off.y != 0) - off /= std::sqrt(off.x * off.x + off.y * off.y); + off = glm::normalize(off); double angle = glm::radians(yaw); @@ -62,15 +63,15 @@ void camera::update() glm::vec<2, double> rotated = glm::vec<2, double>(off.x, off.y) * mat; bool on_ground = false; - velocity.z -= 0.000981; + velocity.z -= 9.81 * dt; - if(pos.z + velocity.z < 1.6) + if(pos.z + velocity.z * dt < 1.6) { on_ground = true; if(keyboard::is_pressed(GLFW_KEY_SPACE)) { - velocity.z += 0.04; + velocity.z = 3.5; } else @@ -85,16 +86,18 @@ void camera::update() m = 0; } - velocity.x += rotated.x * m; - velocity.y += rotated.y * m; - - if(std::abs(pos.x + velocity.x) > 2.9) + velocity.x += rotated.x * m * dt; + velocity.y += rotated.y * m * dt; + + if(std::abs(pos.x + velocity.x * dt) > 2.9) velocity.x = 0; - if(std::abs(pos.y + velocity.y) > 3.9) + if(std::abs(pos.y + velocity.y * dt) > 3.9) velocity.y = 0; - pos += velocity; - velocity *= glm::vec<3, double>(on_ground ? 0.9 : 0.999); + float m2 = std::pow(0.5, dt / (on_ground ? 0.05 : 1)); + + pos += velocity * dt; + velocity *= m2; camera_mat = glm::mat4(1); camera_mat = glm::rotate(camera_mat, (float)glm::radians(-pitch), glm::vec3(1, 0, 0)); diff --git a/src/graphics/camera.hpp b/src/graphics/camera.hpp index 0704ec4..93ddc1c 100644 --- a/src/graphics/camera.hpp +++ b/src/graphics/camera.hpp @@ -10,7 +10,7 @@ namespace sim::graphics::camera glm::mat4 get_matrix(); void rotate(double pitch, double yaw); void move(double x, double y, double z); -void update(); +void update(double dt); }; diff --git a/src/graphics/window.cpp b/src/graphics/window.cpp index 585ce9f..e9a3c54 100644 --- a/src/graphics/window.cpp +++ b/src/graphics/window.cpp @@ -95,12 +95,11 @@ void window::loop() MeshText.bind(); font::generate(MeshText, "Hello, World!\nThis is cool!\n=)", 0.1); - glClearColor(0, 0, 0, 1.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - camera::update(); glm::mat4 mat_projection = glm::perspective(glm::radians(90.0f), 1.0f, 0.01f, 20.f); glUniformMatrix4fv(shader::gl_projection, 1, false, &mat_projection[0][0]); + + glClearColor(0, 0, 0, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); MeshScene.bind(); MeshScene.render(); diff --git a/src/main.cpp b/src/main.cpp index 3cf4dcf..339c057 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,14 +1,32 @@ +#include + #include "graphics/window.hpp" +#include "graphics/camera.hpp" using namespace sim; +unsigned long get_now() +{ + struct timeval tv; + gettimeofday(&tv, nullptr); + return (unsigned long)tv.tv_sec * 1000000 + tv.tv_usec; +} + int main() { graphics::window::create(); + long clock = get_now(); + while(!graphics::window::should_close()) { + long now = get_now(); + long passed = now - clock; + double dt = (double)passed / 1e6; + clock += passed; + + graphics::camera::update(dt); graphics::window::loop(); }