new shapes and fps limiter

This commit is contained in:
2026-01-26 16:20:34 +02:00
parent fed672f23e
commit 16638d9f08
4 changed files with 65 additions and 39 deletions

View File

@@ -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<std::string, ShapeInfo> 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,

View File

@@ -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<float> vertices;
std::vector<unsigned int> indices;
glm::vec3 pos;
unsigned int vao;
unsigned int vbo;
unsigned int ebo;

View File

@@ -1,6 +1,8 @@
#include <map>
#include <print>
#include <string>
#include <thread>
#include <chrono>
#include "glad/glad.h"
#include <GLFW/glfw3.h>
@@ -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<Shader>(settings.vertex_shader_path, settings.fragment_shader_path) };
shader->use();
std::vector<Shape> 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<glm::vec4, 5> 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<float>(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<unsigned int>((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);

View File

@@ -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;
}