From 226573aaba96973a4e9190ee0770a02cb0d4f5df Mon Sep 17 00:00:00 2001 From: TheRedShip Date: Mon, 20 Jan 2025 16:27:02 +0100 Subject: [PATCH] + | Offset bvh in the parsing --- includes/RT/ObjParser.hpp | 3 ++- includes/RT/Scene.hpp | 2 +- includes/RT/SceneParser.hpp | 1 + scenes/dragon.rt | 4 ++-- shaders/trace.glsl | 4 +++- srcs/class/ObjParser.cpp | 4 ++-- srcs/class/Scene.cpp | 7 ++++--- srcs/class/SceneParser.cpp | 20 +++++++++++++++----- 8 files changed, 30 insertions(+), 15 deletions(-) diff --git a/includes/RT/ObjParser.hpp b/includes/RT/ObjParser.hpp index ef35df7..c64b7f3 100644 --- a/includes/RT/ObjParser.hpp +++ b/includes/RT/ObjParser.hpp @@ -20,8 +20,9 @@ class ObjParser public: ObjParser(std::string &filename); ~ObjParser(); - void parse(Scene &scene); + void parse(Scene &scene, glm::vec3 offset); + private: glm::vec3 getVertex(std::stringstream &line); glm::vec2 getUV(std::stringstream &line); diff --git a/includes/RT/Scene.hpp b/includes/RT/Scene.hpp index 095ec13..63bead0 100644 --- a/includes/RT/Scene.hpp +++ b/includes/RT/Scene.hpp @@ -111,7 +111,7 @@ class Scene void updateLightAndObjects(int mat_id); std::set getGPULights(); - void addBvh(std::vector &triangles); + void addBvh(std::vector &triangles, glm::vec3 offset); const std::vector &getObjectData() const; const std::vector &getTriangleData() const; diff --git a/includes/RT/SceneParser.hpp b/includes/RT/SceneParser.hpp index 3fae29f..88d9605 100644 --- a/includes/RT/SceneParser.hpp +++ b/includes/RT/SceneParser.hpp @@ -25,6 +25,7 @@ class SceneParser private: void parseMaterial(std::stringstream &line); void parseCamera(std::stringstream &line); + void parseObj(std::stringstream &line); Scene *_scene; diff --git a/scenes/dragon.rt b/scenes/dragon.rt index 929cdf4..541cf41 100644 --- a/scenes/dragon.rt +++ b/scenes/dragon.rt @@ -24,8 +24,8 @@ pl 0 -2 0 0 1 0 2 // floor qu -1 1.999 -1 2 0 0 0 0 2 6 -OBJ obj/Dragon_80K.obj -# OBJ obj/Dragon_80K.obj +OBJ obj/Dragon_80K.obj 0 0 0 +OBJ obj/Dragon_80K.obj 0 0 1 # OBJ obj/teapot.obj # OBJ obj/Model.obj diff --git a/shaders/trace.glsl b/shaders/trace.glsl index c4befa1..5b186ed 100644 --- a/shaders/trace.glsl +++ b/shaders/trace.glsl @@ -129,7 +129,7 @@ hitInfo traverseBVHs(Ray ray) { GPUBvhData bvh_data = BvhData[i]; - ray.origin = ray.origin + vec3(float(i), 0., 0.); + ray.origin -= bvh_data.offset; hitInfo temp_hit = traceBVH(ray, bvh_data); if (temp_hit.t < hit.t) @@ -141,6 +141,8 @@ hitInfo traverseBVHs(Ray ray) hit.position = temp_hit.position; hit.normal = temp_hit.normal; } + + ray.origin += bvh_data.offset; } return (hit); diff --git a/srcs/class/ObjParser.cpp b/srcs/class/ObjParser.cpp index da354c4..e8641d2 100644 --- a/srcs/class/ObjParser.cpp +++ b/srcs/class/ObjParser.cpp @@ -240,7 +240,7 @@ void ObjParser::parseMtl(std::stringstream &input_line, Scene &scene) file.close(); } -void ObjParser::parse(Scene &scene) +void ObjParser::parse(Scene &scene, glm::vec3 offset) { std::string line; std::string identifier; @@ -275,6 +275,6 @@ void ObjParser::parse(Scene &scene) } } - scene.addBvh(_triangles); + scene.addBvh(_triangles, offset); } diff --git a/srcs/class/Scene.cpp b/srcs/class/Scene.cpp index 8c72cdd..39ba8d2 100644 --- a/srcs/class/Scene.cpp +++ b/srcs/class/Scene.cpp @@ -142,7 +142,7 @@ void Scene::addObject(Object *obj) _gpu_objects.push_back(gpu_obj); } -void Scene::addBvh(std::vector &triangles) +void Scene::addBvh(std::vector &triangles, glm::vec3 offset) { GPUBvhData new_bvh_data; std::vector new_bvhs_list; @@ -153,8 +153,9 @@ void Scene::addBvh(std::vector &triangles) BVH *bvh = new BVH(triangles, 0, triangles.size()); new_bvhs_list = bvh->getGPUBvhs(); - - new_bvh_data.offset = glm::vec3(0., 0., 0.); + + std::cout << glm::to_string(offset) << std::endl; + new_bvh_data.offset = offset; new_bvh_data.bvh_start_index = _gpu_bvh.size(); new_bvh_data.triangle_start_index = _gpu_triangles.size(); diff --git a/srcs/class/SceneParser.cpp b/srcs/class/SceneParser.cpp index 83c62ed..6ff5a12 100644 --- a/srcs/class/SceneParser.cpp +++ b/srcs/class/SceneParser.cpp @@ -95,6 +95,20 @@ void SceneParser::parseCamera(std::stringstream &line) } +void SceneParser::parseObj(std::stringstream &line) +{ + std::string name; + float x = 0.; + float y = 0.; + float z = 0.; + + line >> name; + line >> x >> y >> z; + + ObjParser obj(name); + obj.parse(*_scene, glm::vec3(x, y, z)); +} + bool SceneParser::parseLine(const std::string &line) { if (line.empty() || line[0] == '#') @@ -128,11 +142,7 @@ bool SceneParser::parseLine(const std::string &line) else if (identifier == "CAM") this->parseCamera(ss); else if (identifier == "OBJ") - { - ss >> identifier; - ObjParser obj(identifier); - obj.parse(*_scene); - } + this->parseObj(ss); } catch (const std::exception& e) {