+ | Offset bvh in the parsing

This commit is contained in:
TheRedShip
2025-01-20 16:27:02 +01:00
parent 73bb4462b5
commit 226573aaba
8 changed files with 30 additions and 15 deletions

View File

@ -20,7 +20,8 @@ class ObjParser
public: public:
ObjParser(std::string &filename); ObjParser(std::string &filename);
~ObjParser(); ~ObjParser();
void parse(Scene &scene);
void parse(Scene &scene, glm::vec3 offset);
private: private:
glm::vec3 getVertex(std::stringstream &line); glm::vec3 getVertex(std::stringstream &line);

View File

@ -111,7 +111,7 @@ class Scene
void updateLightAndObjects(int mat_id); void updateLightAndObjects(int mat_id);
std::set<int> getGPULights(); std::set<int> getGPULights();
void addBvh(std::vector<Triangle> &triangles); void addBvh(std::vector<Triangle> &triangles, glm::vec3 offset);
const std::vector<GPUObject> &getObjectData() const; const std::vector<GPUObject> &getObjectData() const;
const std::vector<GPUTriangle> &getTriangleData() const; const std::vector<GPUTriangle> &getTriangleData() const;

View File

@ -25,6 +25,7 @@ class SceneParser
private: private:
void parseMaterial(std::stringstream &line); void parseMaterial(std::stringstream &line);
void parseCamera(std::stringstream &line); void parseCamera(std::stringstream &line);
void parseObj(std::stringstream &line);
Scene *_scene; Scene *_scene;

View File

@ -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 qu -1 1.999 -1 2 0 0 0 0 2 6
OBJ obj/Dragon_80K.obj OBJ obj/Dragon_80K.obj 0 0 0
# OBJ obj/Dragon_80K.obj OBJ obj/Dragon_80K.obj 0 0 1
# OBJ obj/teapot.obj # OBJ obj/teapot.obj
# OBJ obj/Model.obj # OBJ obj/Model.obj

View File

@ -129,7 +129,7 @@ hitInfo traverseBVHs(Ray ray)
{ {
GPUBvhData bvh_data = BvhData[i]; 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); hitInfo temp_hit = traceBVH(ray, bvh_data);
if (temp_hit.t < hit.t) if (temp_hit.t < hit.t)
@ -141,6 +141,8 @@ hitInfo traverseBVHs(Ray ray)
hit.position = temp_hit.position; hit.position = temp_hit.position;
hit.normal = temp_hit.normal; hit.normal = temp_hit.normal;
} }
ray.origin += bvh_data.offset;
} }
return (hit); return (hit);

View File

@ -240,7 +240,7 @@ void ObjParser::parseMtl(std::stringstream &input_line, Scene &scene)
file.close(); file.close();
} }
void ObjParser::parse(Scene &scene) void ObjParser::parse(Scene &scene, glm::vec3 offset)
{ {
std::string line; std::string line;
std::string identifier; std::string identifier;
@ -275,6 +275,6 @@ void ObjParser::parse(Scene &scene)
} }
} }
scene.addBvh(_triangles); scene.addBvh(_triangles, offset);
} }

View File

@ -142,7 +142,7 @@ void Scene::addObject(Object *obj)
_gpu_objects.push_back(gpu_obj); _gpu_objects.push_back(gpu_obj);
} }
void Scene::addBvh(std::vector<Triangle> &triangles) void Scene::addBvh(std::vector<Triangle> &triangles, glm::vec3 offset)
{ {
GPUBvhData new_bvh_data; GPUBvhData new_bvh_data;
std::vector<GPUBvh> new_bvhs_list; std::vector<GPUBvh> new_bvhs_list;
@ -154,7 +154,8 @@ void Scene::addBvh(std::vector<Triangle> &triangles)
BVH *bvh = new BVH(triangles, 0, triangles.size()); BVH *bvh = new BVH(triangles, 0, triangles.size());
new_bvhs_list = bvh->getGPUBvhs(); 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.bvh_start_index = _gpu_bvh.size();
new_bvh_data.triangle_start_index = _gpu_triangles.size(); new_bvh_data.triangle_start_index = _gpu_triangles.size();

View File

@ -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) bool SceneParser::parseLine(const std::string &line)
{ {
if (line.empty() || line[0] == '#') if (line.empty() || line[0] == '#')
@ -128,11 +142,7 @@ bool SceneParser::parseLine(const std::string &line)
else if (identifier == "CAM") else if (identifier == "CAM")
this->parseCamera(ss); this->parseCamera(ss);
else if (identifier == "OBJ") else if (identifier == "OBJ")
{ this->parseObj(ss);
ss >> identifier;
ObjParser obj(identifier);
obj.parse(*_scene);
}
} }
catch (const std::exception& e) catch (const std::exception& e)
{ {