From 2ff3c3ec15da8db5fde0fadfd6575c7602e0f186 Mon Sep 17 00:00:00 2001 From: ExilProductions Date: Fri, 2 May 2025 15:52:43 +0200 Subject: [PATCH] updated main header --- Engine/loader/obj_loader.cpp | 81 ++++++++++++++++++++---------------- Engine/ps1engine.h | 2 +- Engine/time/time_manager.cpp | 23 +++++----- Engine/time/time_manager.h | 30 ++++++------- 4 files changed, 71 insertions(+), 65 deletions(-) diff --git a/Engine/loader/obj_loader.cpp b/Engine/loader/obj_loader.cpp index 48ebc37..91f86f8 100644 --- a/Engine/loader/obj_loader.cpp +++ b/Engine/loader/obj_loader.cpp @@ -5,47 +5,53 @@ #include #include -std::vector ObjLoader::load_obj(const std::string& obj_filepath, - Renderer& renderer) +std::vector ObjLoader::load_obj(const std::string &obj_filepath, + Renderer &renderer) { std::vector triangles; std::vector vertices; std::vector uvs; std::vector normals; - std::map material_textures; // Maps material names to textures + std::map material_textures; std::string current_material; std::string mtl_filepath; - // Helper to parse MTL file - auto parse_mtl = [&](const std::string& mtl_path) { + auto parse_mtl = [&](const std::string &mtl_path) + { std::ifstream mtl_file(mtl_path); - if (!mtl_file.is_open()) { - // Silently skip if MTL file cannot be opened + if (!mtl_file.is_open()) + { + return; } std::string line, current_mtl; - while (std::getline(mtl_file, line)) { + while (std::getline(mtl_file, line)) + { std::istringstream iss(line); std::string token; iss >> token; - if (token == "newmtl") { + if (token == "newmtl") + { iss >> current_mtl; } - else if (token == "map_Kd") { + else if (token == "map_Kd") + { std::string texture_path; std::getline(iss, texture_path); texture_path.erase(0, texture_path.find_first_not_of(" \t")); - - // Construct full texture path relative to MTL directory + std::filesystem::path mtl_dir = std::filesystem::path(mtl_path).parent_path(); std::filesystem::path tex_path = mtl_dir / texture_path; - - try { + + try + { material_textures[current_mtl] = renderer.load_texture(tex_path.string()); - } catch (const std::exception& e) { - // Fallback to default texture (id=0) if loading fails + } + catch (const std::exception &e) + { + material_textures[current_mtl] = Texture(0, 0, 0); } } @@ -53,78 +59,81 @@ std::vector ObjLoader::load_obj(const std::string& obj_filepath, mtl_file.close(); }; - // Open and parse OBJ file std::ifstream obj_file(obj_filepath); - if (!obj_file.is_open()) { + if (!obj_file.is_open()) + { throw std::runtime_error("Failed to open OBJ file: " + obj_filepath); } std::filesystem::path obj_dir = std::filesystem::path(obj_filepath).parent_path(); std::string line; - while (std::getline(obj_file, line)) { + while (std::getline(obj_file, line)) + { std::istringstream iss(line); std::string token; iss >> token; - if (token == "v") { + if (token == "v") + { float x, y, z; iss >> x >> y >> z; vertices.emplace_back(x, y, z); } - else if (token == "vt") { + else if (token == "vt") + { float u, v; iss >> u >> v; uvs.emplace_back(u, v); } - else if (token == "vn") { + else if (token == "vn") + { float x, y, z; iss >> x >> y >> z; normals.emplace_back(x, y, z); } - else if (token == "f") { - std::vector> face_vertices; // v/vt/vn indices + else if (token == "f") + { + std::vector> face_vertices; std::string vertex; - while (iss >> vertex) { + while (iss >> vertex) + { int v_idx = 0, vt_idx = 0, vn_idx = 0; std::sscanf(vertex.c_str(), "%d/%d/%d", &v_idx, &vt_idx, &vn_idx); - face_vertices.emplace_back(v_idx - 1, vt_idx - 1, vn_idx - 1); // Convert to 0-based + face_vertices.emplace_back(v_idx - 1, vt_idx - 1, vn_idx - 1); } - // Triangulate if necessary (e.g., for quads) - for (size_t i = 1; i < face_vertices.size() - 1; ++i) { + for (size_t i = 1; i < face_vertices.size() - 1; ++i) + { Triangle tri; auto [v0, vt0, vn0] = face_vertices[0]; auto [v1, vt1, vn1] = face_vertices[i]; auto [v2, vt2, vn2] = face_vertices[i + 1]; - // Assign vertices tri.v0 = vertices[v0]; tri.v1 = vertices[v1]; tri.v2 = vertices[v2]; - // Assign UVs (use default if not specified) tri.uv0 = vt0 >= 0 && vt0 < uvs.size() ? uvs[vt0] : Vec2(0, 0); tri.uv1 = vt1 >= 0 && vt1 < uvs.size() ? uvs[vt1] : Vec2(0, 0); tri.uv2 = vt2 >= 0 && vt2 < uvs.size() ? uvs[vt2] : Vec2(0, 0); - // Assign default color tri.color = Color(255, 255, 255); - // Assign texture based on current material - tri.texture = material_textures.count(current_material) ? - material_textures[current_material] : Texture(0, 0, 0); + tri.texture = material_textures.count(current_material) ? material_textures[current_material] : Texture(0, 0, 0); triangles.push_back(tri); } } - else if (token == "mtllib") { + else if (token == "mtllib") + { std::string mtl_name; iss >> mtl_name; mtl_filepath = (obj_dir / mtl_name).string(); parse_mtl(mtl_filepath); } - else if (token == "usemtl") { + else if (token == "usemtl") + { iss >> current_material; } } diff --git a/Engine/ps1engine.h b/Engine/ps1engine.h index 56888a4..717e7cd 100644 --- a/Engine/ps1engine.h +++ b/Engine/ps1engine.h @@ -1,5 +1,5 @@ #include -#include +#include #include #include #include \ No newline at end of file diff --git a/Engine/time/time_manager.cpp b/Engine/time/time_manager.cpp index d347e2d..654f052 100644 --- a/Engine/time/time_manager.cpp +++ b/Engine/time/time_manager.cpp @@ -2,31 +2,32 @@ #include Time::Time() - : lastFrameTime(glfwGetTime()) - , currentFrameTime(lastFrameTime) - , deltaTime(0) - , elapsedTime(0.0f) { + : lastFrameTime(glfwGetTime()), currentFrameTime(lastFrameTime), deltaTime(0), elapsedTime(0.0f) +{ } -void Time::Update() { +void Time::Update() +{ currentFrameTime = glfwGetTime(); float deltaTimeSeconds = static_cast(currentFrameTime - lastFrameTime); - - // Store delta time in fixed-point and update elapsed time + deltaTime = SecondsToFixed(deltaTimeSeconds); elapsedTime += deltaTimeSeconds; - + lastFrameTime = currentFrameTime; } -int32_t Time::GetDeltaTime() const { +int32_t Time::GetDeltaTime() const +{ return deltaTime; } -float Time::GetElapsedTime() const { +float Time::GetElapsedTime() const +{ return elapsedTime; } -int32_t Time::SecondsToFixed(float seconds) const { +int32_t Time::SecondsToFixed(float seconds) const +{ return static_cast(seconds * FIXED_POINT_SCALE); } \ No newline at end of file diff --git a/Engine/time/time_manager.h b/Engine/time/time_manager.h index 0729993..1ea25cf 100644 --- a/Engine/time/time_manager.h +++ b/Engine/time/time_manager.h @@ -3,30 +3,26 @@ #include -class Time { +class Time +{ public: Time(); - - // Update time and calculate delta time (call each frame) + void Update(); - - // Get delta time in seconds (fixed-point representation) + int32_t GetDeltaTime() const; - - // Get total elapsed time in seconds + float GetElapsedTime() const; private: - // Fixed-point scale for PS1-style precision - static constexpr int32_t FIXED_POINT_SCALE = 4096; // 12-bit fractional part - - double lastFrameTime; // Last frame timestamp (in seconds, GLFW time) - double currentFrameTime; // Current frame timestamp - int32_t deltaTime; // Delta time in fixed-point - float elapsedTime; // Total elapsed time in seconds - - // Convert float seconds to fixed-point + static constexpr int32_t FIXED_POINT_SCALE = 4096; + + double lastFrameTime; + double currentFrameTime; + int32_t deltaTime; + float elapsedTime; + int32_t SecondsToFixed(float seconds) const; }; -#endif // TIME_H \ No newline at end of file +#endif \ No newline at end of file