From 16638d9f089e727bc9afff6b4e5bdb8022d57418 Mon Sep 17 00:00:00 2001 From: Aaro Saila Date: Mon, 26 Jan 2026 16:20:34 +0200 Subject: [PATCH] new shapes and fps limiter --- src/Shape.cpp | 37 ++++++++++++++++++------- src/headers/Shape.hpp | 2 +- src/main.cpp | 61 +++++++++++++++++++++++------------------ src/shaders/shader.frag | 4 +-- 4 files changed, 65 insertions(+), 39 deletions(-) diff --git a/src/Shape.cpp b/src/Shape.cpp index e47d349..d9d8b77 100644 --- a/src/Shape.cpp +++ b/src/Shape.cpp @@ -19,11 +19,11 @@ Shape::Shape( ShapeInfo& shapeInfo, glm::vec3 pos ) - : rotation { 0.0f, 0.0f, 0.0f } + : pos { pos } + , rotation { 0.0f, 0.0f, 0.0f } , scale { 1.0f, 1.0f, 1.0f } , vertices { shapeInfo.vertices } , indices { shapeInfo.indices } - , pos { pos } , vao { 0 } , vbo { 0 } , ebo { 0 } @@ -105,20 +105,37 @@ std::map shapes { // clang-format on } }, + { + "square", + { + // clang-format off + .vertices = { + -0.5f, 0.5f, 0.0f, // top-left, 0 + 0.5f, 0.5f, 0.0f, // top-right, 1 + 0.5f, -0.5f, 0.0f, // bottom-right, 2 + -0.5f, -0.5f, 0.0f, // bottom-left, 3 + }, + .indices = { + 0, 1, 2, + 0, 3, 2 + } + // clang-format on + } + }, { "cube", { // clang-format off .vertices = { - -0.5f, 0.5f, 0.0f, // front top-left, 0 - 0.5f, 0.5f, 0.0f, // front top-right, 1 - 0.5f, -0.5f, 0.0f, // front bottom-right, 2 - -0.5f, -0.5f, 0.0f, // front bottom-left, 3 + -0.5f, 0.5f, -0.5f, // front top-left, 0 + 0.5f, 0.5f, -0.5f, // front top-right, 1 + 0.5f, -0.5f, -0.5f, // front bottom-right, 2 + -0.5f, -0.5f, -0.5f, // front bottom-left, 3 - -0.5f, 0.5f, -1.0f, // rear top-left, 4 - 0.5f, 0.5f, -1.0f, // rear top-right, 5 - 0.5f, -0.5f, -1.0f, // rear bottom-right, 6 - -0.5f, -0.5f, -1.0f, // rear bottom-left, 7 + -0.5f, 0.5f, 0.5f, // rear top-left, 4 + 0.5f, 0.5f, 0.5f, // rear top-right, 5 + 0.5f, -0.5f, 0.5f, // rear bottom-right, 6 + -0.5f, -0.5f, 0.5f, // rear bottom-left, 7 }, .indices = { 0, 1, 2, diff --git a/src/headers/Shape.hpp b/src/headers/Shape.hpp index c8038c2..b32940a 100644 --- a/src/headers/Shape.hpp +++ b/src/headers/Shape.hpp @@ -14,6 +14,7 @@ struct ShapeInfo { class Shape { public: + glm::vec3 pos; glm::vec3 rotation; glm::vec3 scale; @@ -28,7 +29,6 @@ public: private: std::vector vertices; std::vector indices; - glm::vec3 pos; unsigned int vao; unsigned int vbo; unsigned int ebo; diff --git a/src/main.cpp b/src/main.cpp index 106a57b..abee9b7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,8 @@ #include #include #include +#include +#include #include "glad/glad.h" #include @@ -30,16 +32,19 @@ static struct { Settings settings {}; -float delta {}; +double delta {}; Camera camera { - glm::vec3 { 0.0f, 0.0f, 1.0f }, + glm::vec3 { 0.0f, 0.0f, 3.0f }, 70.0f, 70.0f, 1.0f, 0.1f }; +// TODO: fps limiter is off by ~4 for some reason at 120-144 fps range. +double fps_lock { 1.0f / 61.0f }; + void print_glfw_error() { const char* desc {}; const int err { glfwGetError(&desc) }; @@ -153,35 +158,24 @@ int main() { auto shader { std::make_shared(settings.vertex_shader_path, settings.fragment_shader_path) }; shader->use(); - std::vector created_shapes { - { shader, shapes.at("triangle"), { 0.0f, 0.0f, 0.0f } }, - { shader, shapes.at("triangle"), { 0.5f, 0.0f, -0.5f } }, - { shader, shapes.at("triangle"), { -0.5f, 0.0f, -0.5f } }, - { shader, shapes.at("triangle"), { 0.0f, 0.0f, -1.0f } }, - { shader, shapes.at("cube"), { 0.0f, 0.0f, -0.25f }} - }; - created_shapes.at(1).rotation.y = 90.0f; - created_shapes.at(2).rotation.y = 90.0f; + Shape plane { shader, shapes.at("square") }; + plane.pos.y = -1.0f; + plane.rotation.x = 90.0f; + plane.scale *= 100; - created_shapes.at(4).scale /= 2; - // created_shapes.at(4).scale.z /= 2; - - std::array colors { - glm::vec4 { 1.0f, 0.0f, 0.0f, 0.0f }, - { 0.0f, 1.0f, 0.0f, 0.0f }, - { 0.0f, 0.0f, 1.0f, 0.0f }, - { 1.0f, 1.0f, 0.0f, 0.0f }, - { 1.0f, 1.0f, 1.0f, 0.0f } - }; + Shape square { shader, shapes.at("square") }; + Shape cube { shader, shapes.at("cube") }; long unsigned int frame { 0 }; - float last_frame_time { 0 }; + double last_frame_time { 0 }; while (!glfwWindowShouldClose(window)) { - const float current_time { static_cast(glfwGetTime()) }; + const double current_time { glfwGetTime() }; delta = current_time - last_frame_time; last_frame_time = current_time; + std::println("FPS: {}", 1.0f / delta); + std::println("Delta: {}", delta); std::println("Frame: {}", frame); frame++; @@ -205,9 +199,24 @@ int main() { }; shader->set_mat4("projection", projection); - for (std::size_t i { 0 }; i < created_shapes.size(); i++) { - shader->set_vec4("thecolor", colors[i]); - created_shapes[i].draw(); + shader->set_vec4("color", glm::normalize(glm::vec4 { 207.0f, 90.0f, 28.0f, 1.0f })); + plane.draw(); + + shader->set_vec4("color", { 0.0f, 0.0f, 1.0f, 1.0f }); + square.rotation.z += 45.0f * delta; + square.pos.x = std::cos(current_time); + square.pos.y = std::sin(current_time); + square.draw(); + + shader->set_vec4("color", { 1.0f, 1.0f, 1.0f, 1.0f }); + cube.rotation.y += 45.0f * delta; + cube.draw(); + + const double time_since_last_frame { glfwGetTime() - last_frame_time }; + if (time_since_last_frame < fps_lock) { + const unsigned int sleep_time_ns { static_cast((fps_lock - time_since_last_frame) * 1000000000.0f) }; + const std::chrono::nanoseconds sleep_duration { sleep_time_ns }; + std::this_thread::sleep_for(sleep_duration); } glfwSwapBuffers(window); diff --git a/src/shaders/shader.frag b/src/shaders/shader.frag index 4ad5148..0d49a4f 100644 --- a/src/shaders/shader.frag +++ b/src/shaders/shader.frag @@ -2,9 +2,9 @@ out vec4 frag_color; -uniform vec4 thecolor; +uniform vec4 color; void main() { // frag_color = vec4(1.0f, 0.0f, 0.0f, 1.0f); - frag_color = thecolor; + frag_color = color; }