feat: assignment 3

This commit is contained in:
2025-10-13 18:23:01 +02:00
parent db6e548476
commit 49219fc597
13 changed files with 364 additions and 61 deletions

View File

@@ -10,10 +10,12 @@
#include "buffer.h"
#include "camera.h"
#include "logger.h"
#include "material.h"
#include "mesh.h"
#include "model.h"
#include "shader.h"
#include "state.h"
#include "texture.h"
#include "vertex.h"
#include "world.h"
@@ -83,8 +85,8 @@ void Engine::initialize()
// Initialize default shader
Logger::info("Loading default shaders...");
state::default_shader =
std::make_shared<Shader>("data/shaders/vertex.glsl", "data/shaders/fragment.glsl");
state::default_shader = std::make_shared<Shader>(
"data/shaders/vertex.glsl", "data/shaders/fragment.glsl");
if (std::strlen(state::default_shader->error()) > 0) {
Logger::error(sstr("Failed to initialize shaders: ",
state::default_shader->error()));
@@ -140,9 +142,10 @@ void Engine::setup()
world_ = std::make_unique<World>();
Logger::info("World created");
// Create camera
// Create camera at position (0, 1, 3) with -20 degrees X rotation
camera_ = std::make_shared<Camera>();
camera_->set_position(glm::vec3(0.0f, 0.0f, 6.0f));
camera_->set_position(glm::vec3(0.0f, 1.0f, 3.0f));
camera_->set_rotation(glm::vec3(glm::radians(-20.0f), 0.0f, 0.0f));
camera_->set_projection(
glm::perspective(glm::radians(45.0f),
static_cast<float>(screen_width_)
@@ -153,31 +156,79 @@ void Engine::setup()
world_->add_entity(camera_);
Logger::info("Camera created and added to world");
// Create triangle mesh
std::vector<Vertex> vertices = {
{{0.0f, 0.5f, 0.0f}, {0.0f, 1.0f, 0.0f}},
{{-0.5f, -0.5f, 0.0f}, {0.0f, 0.0f, 1.0f}},
{{0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 0.0f}}};
std::vector<uint16_t> indices = {0, 1, 2};
// Load textures
auto top_texture = Texture::load("data/textures/top.png");
auto front_texture = Texture::load("data/textures/front.png");
auto buffer = std::make_shared<Buffer>(vertices, indices);
mesh_ = std::make_shared<Mesh>();
mesh_->add_buffer(buffer);
Logger::info(sstr("Mesh created with ", vertices.size(),
" vertices and ", indices.size(), " indices"));
// Create cube mesh with two buffers
mesh_ = std::make_shared<Mesh>();
// Buffer 1: Top and bottom faces
std::vector<Vertex> top_bottom_vertices = {
// Top face (y = 0.5)
{{-0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}},
{{0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 0.0f}},
{{0.5f, 0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}},
{{-0.5f, 0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}},
// Bottom face (y = -0.5)
{{-0.5f, -0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}},
{{0.5f, -0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 0.0f}},
{{0.5f, -0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}},
{{-0.5f, -0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}}};
std::vector<uint16_t> top_bottom_indices = {// Top face
0, 1, 2, 0, 2, 3,
// Bottom face
4, 6, 5, 4, 7, 6};
auto top_bottom_buffer =
std::make_shared<Buffer>(top_bottom_vertices, top_bottom_indices);
Material top_material(top_texture);
mesh_->add_buffer(top_bottom_buffer, top_material);
// Buffer 2: Front, back, left, right faces
std::vector<Vertex> side_vertices = {
// Front face (z = 0.5)
{{-0.5f, -0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}},
{{0.5f, -0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 0.0f}},
{{0.5f, 0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}},
{{-0.5f, 0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}},
// Back face (z = -0.5)
{{-0.5f, -0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 0.0f}},
{{-0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}},
{{0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}},
{{0.5f, -0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}},
// Left face (x = -0.5)
{{-0.5f, -0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}},
{{-0.5f, -0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 0.0f}},
{{-0.5f, 0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}},
{{-0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}},
// Right face (x = 0.5)
{{0.5f, -0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 0.0f}},
{{0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}},
{{0.5f, 0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}},
{{0.5f, -0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}};
std::vector<uint16_t> side_indices = {// Front face
0, 1, 2, 0, 2, 3,
// Back face
4, 5, 6, 4, 6, 7,
// Left face
8, 9, 10, 8, 10, 11,
// Right face
12, 13, 14, 12, 14, 15};
auto side_buffer =
std::make_shared<Buffer>(side_vertices, side_indices);
Material side_material(front_texture);
mesh_->add_buffer(side_buffer, side_material);
Logger::info("Cube mesh created with two buffers");
// Create model at origin
auto model = std::make_shared<Model>(mesh_);
model->set_position(glm::vec3(0.0f, 0.0f, 0.0f));
models_.push_back(model);
world_->add_entity(model);
// Create 9 models in a 3x3 grid
for (int row = 0; row < 3; ++row) {
for (int col = 0; col < 3; ++col) {
auto model = std::make_shared<Model>(mesh_);
model->set_position(
glm::vec3(-3.0f + static_cast<float>(col) * 3.0f,
0.0f, static_cast<float>(-row) * 3.0f));
models_.push_back(model);
world_->add_entity(model);
}
}
Logger::info(sstr("Created ", models_.size(), " models in scene"));
Logger::info("Scene setup complete");
}