From 46ef719f291f2606e5b94156d32dbe0ba2c88e56 Mon Sep 17 00:00:00 2001 From: tomoron Date: Tue, 21 Jan 2025 16:15:07 +0100 Subject: [PATCH] add relative path for materials in obj files and for OBJ elements in scenes --- includes/RT/ObjParser.hpp | 6 ++- includes/RT/SceneParser.hpp | 5 +- {obj => scenes/obj}/Dragon_800K.obj | 0 {obj => scenes/obj}/Dragon_80K.obj | 0 {obj => scenes/obj}/Dragon_8K.obj | 0 {obj => scenes/obj}/Lowpoly_tree_sample.mtl | 0 {obj => scenes/obj}/Lowpoly_tree_sample.obj | 0 {obj => scenes/obj}/Model.obj | 0 {obj => scenes/obj}/small_teapot.obj | 0 {obj => scenes/obj}/teapot.obj | 0 scenes/obj/test.mtl | 3 ++ scenes/obj/test.obj | 9 ++++ scenes/test.rt | 19 ++++---- srcs/class/ObjParser.cpp | 53 ++++++++++++++++++--- srcs/class/Scene.cpp | 4 +- srcs/class/SceneParser.cpp | 6 +-- 16 files changed, 81 insertions(+), 24 deletions(-) rename {obj => scenes/obj}/Dragon_800K.obj (100%) rename {obj => scenes/obj}/Dragon_80K.obj (100%) rename {obj => scenes/obj}/Dragon_8K.obj (100%) rename {obj => scenes/obj}/Lowpoly_tree_sample.mtl (100%) rename {obj => scenes/obj}/Lowpoly_tree_sample.obj (100%) rename {obj => scenes/obj}/Model.obj (100%) rename {obj => scenes/obj}/small_teapot.obj (100%) rename {obj => scenes/obj}/teapot.obj (100%) create mode 100644 scenes/obj/test.mtl create mode 100644 scenes/obj/test.obj diff --git a/includes/RT/ObjParser.hpp b/includes/RT/ObjParser.hpp index 8937310..41dc772 100644 --- a/includes/RT/ObjParser.hpp +++ b/includes/RT/ObjParser.hpp @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/16 15:00:49 by tomoron #+# #+# */ -/* Updated: 2025/01/18 18:32:42 by tomoron ### ########.fr */ +/* Updated: 2025/01/21 16:01:21 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ class ObjParser { public: - ObjParser(std::string &filename); + ObjParser(std::string &filename, std::string &scene_filename); ~ObjParser(); void parse(Scene &scene, glm::vec3 offset, float scale, glm::mat4 transform); @@ -31,10 +31,12 @@ class ObjParser void parseMtl(std::stringstream &line, Scene &scene); bool addTriangleFromPolygon(std::vector &vertices, int inv); void addTriangle(glm::vec3 v1, glm::vec3 v2, glm::vec3 v3); + std::string getFilePath(std::string &file); int pointInTriangle(glm::vec3 pts[3], std::vector vertices, size_t cur); std::vector objSplit(std::string str, std::string delim); void getFaceVertices(std::vector &faceVertices, std::stringstream &line); + std::string _filename; std::ifstream _file; std::vector _vertices; std::vector _textureVertices; diff --git a/includes/RT/SceneParser.hpp b/includes/RT/SceneParser.hpp index 88d9605..142803a 100644 --- a/includes/RT/SceneParser.hpp +++ b/includes/RT/SceneParser.hpp @@ -6,7 +6,7 @@ /* By: TheRed +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/26 21:37:37 by TheRed #+# #+# */ -/* Updated: 2025/01/16 15:02:01 by tomoron ### ########.fr */ +/* Updated: 2025/01/21 15:15:13 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ class SceneParser { public: - SceneParser(Scene *scene); + SceneParser(Scene *scene, char *filename); bool parseLine(const std::string &line); @@ -28,6 +28,7 @@ class SceneParser void parseObj(std::stringstream &line); Scene *_scene; + std::string _filename; std::map> object_parsers; }; diff --git a/obj/Dragon_800K.obj b/scenes/obj/Dragon_800K.obj similarity index 100% rename from obj/Dragon_800K.obj rename to scenes/obj/Dragon_800K.obj diff --git a/obj/Dragon_80K.obj b/scenes/obj/Dragon_80K.obj similarity index 100% rename from obj/Dragon_80K.obj rename to scenes/obj/Dragon_80K.obj diff --git a/obj/Dragon_8K.obj b/scenes/obj/Dragon_8K.obj similarity index 100% rename from obj/Dragon_8K.obj rename to scenes/obj/Dragon_8K.obj diff --git a/obj/Lowpoly_tree_sample.mtl b/scenes/obj/Lowpoly_tree_sample.mtl similarity index 100% rename from obj/Lowpoly_tree_sample.mtl rename to scenes/obj/Lowpoly_tree_sample.mtl diff --git a/obj/Lowpoly_tree_sample.obj b/scenes/obj/Lowpoly_tree_sample.obj similarity index 100% rename from obj/Lowpoly_tree_sample.obj rename to scenes/obj/Lowpoly_tree_sample.obj diff --git a/obj/Model.obj b/scenes/obj/Model.obj similarity index 100% rename from obj/Model.obj rename to scenes/obj/Model.obj diff --git a/obj/small_teapot.obj b/scenes/obj/small_teapot.obj similarity index 100% rename from obj/small_teapot.obj rename to scenes/obj/small_teapot.obj diff --git a/obj/teapot.obj b/scenes/obj/teapot.obj similarity index 100% rename from obj/teapot.obj rename to scenes/obj/teapot.obj diff --git a/scenes/obj/test.mtl b/scenes/obj/test.mtl new file mode 100644 index 0000000..e47f3a6 --- /dev/null +++ b/scenes/obj/test.mtl @@ -0,0 +1,3 @@ +newmtl patate +d 0.5 +Kd 1 1 1 diff --git a/scenes/obj/test.obj b/scenes/obj/test.obj new file mode 100644 index 0000000..a075e91 --- /dev/null +++ b/scenes/obj/test.obj @@ -0,0 +1,9 @@ +v 0 0 0 +v 0 50 0 +v 50 50 0 +v 50 0 0 + +mtllib obj/test.mtl + +usemtl patate +f 1 2 3 4 diff --git a/scenes/test.rt b/scenes/test.rt index d919a93..dbd3bef 100644 --- a/scenes/test.rt +++ b/scenes/test.rt @@ -13,13 +13,14 @@ MAT 255 255 255 5.0 0.0 0.0 // white light 6 MAT 255 255 255 0.0 0.0 0.0 TRN // glass 7 -pl 0 0 -40 0 0 1 1 // back wall -pl 0 0 40 0 0 -1 5 // front wall -pl 40 0 0 -1 0 0 3 // right wall -pl -40 0 0 1 0 0 2 // left wall -pl 0 60 0 0 -1 0 4 // ceiling -pl 0 -10 0 0 1 0 2 // floor +#pl 0 0 -40 0 0 1 1 // back wall +#pl 0 0 40 0 0 -1 5 // front wall +#pl 40 0 0 -1 0 0 3 // right wall +#pl -40 0 0 1 0 0 2 // left wall +#pl 0 60 0 0 -1 0 4 // ceiling +#pl 0 -10 0 0 1 0 2 // floor +# +#qu -20 59 -20 40 0 0 0 0 40 6 -qu -20 59 -20 40 0 0 0 0 40 6 - -OBJ obj/Model.obj +OBJ obj/Tree1.obj 0 0 0 +# OBJ obj/Dragon_800K.obj 10 10 -2 diff --git a/srcs/class/ObjParser.cpp b/srcs/class/ObjParser.cpp index fa494dc..59a9915 100644 --- a/srcs/class/ObjParser.cpp +++ b/srcs/class/ObjParser.cpp @@ -6,20 +6,21 @@ /* By: ycontre +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/16 15:00:33 by tomoron #+# #+# */ -/* Updated: 2025/01/18 21:07:12 by ycontre ### ########.fr */ +/* Updated: 2025/01/21 16:02:48 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "RT.hpp" -ObjParser::ObjParser(std::string &filename) +ObjParser::ObjParser(std::string &filename, std::string &scene_filename) { _mat = 0; - _file.open(filename); + _filename = getFilePath(scene_filename) + filename; + std::cout << _filename << std::endl; + _file.open(_filename); if(!_file.is_open()) throw std::runtime_error("OBJ : could not open object file"); - } ObjParser::~ObjParser() @@ -27,6 +28,18 @@ ObjParser::~ObjParser() _file.close(); } +std::string ObjParser::getFilePath(std::string &file) +{ + int index; + + if(file.find("/") == std::string::npos) + return(""); + index = file.length() - 1; + while(index && file[index] != '/') + index--; + return(file.substr(0, index + 1)); +} + glm::vec3 ObjParser::getVertex(std::stringstream &line) { glm::vec3 res; @@ -116,7 +129,6 @@ bool ObjParser::addTriangleFromPolygon(std::vector &vertices, int inv std::vector ObjParser::objSplit(std::string str, std::string delim) { - std::vector res; while(str.find(delim) != std::string::npos) @@ -179,6 +191,7 @@ void ObjParser::parseMtl(std::stringstream &input_line, Scene &scene) Material *mat; input_line >> filename; + filename = getFilePath(_filename) + filename; file.open(filename); mat = 0; if(!file.is_open()) @@ -201,7 +214,8 @@ void ObjParser::parseMtl(std::stringstream &input_line, Scene &scene) throw std::runtime_error("OBJ: syntax error in material file, missing material name"); mat = new Material; memset(mat, 0, sizeof(Material)); - mat->metallic = 1.0f; + mat->refraction = 1.0f; + mat->roughness = 1.0f; continue; } if(!mat) @@ -229,6 +243,28 @@ void ObjParser::parseMtl(std::stringstream &input_line, Scene &scene) if(!(lineStream >> mat->refraction)) throw std::runtime_error("OBJ: syntax error while getting material refraction"); } + else if(identifier == "Tr") + { + float prob; + + if(!(lineStream >> prob)) + throw std::runtime_error("OBJ : syntax error wile getting material transparency"); + if(prob == 0) + continue; + mat->metallic = 1 - prob; + mat->type = 2; + } + else if(identifier == "d") + { + float prob; + + if(!(lineStream >> prob)) + throw std::runtime_error("OBJ : syntax error wile getting material transparency"); + if(prob == 1) + continue; + mat->metallic = prob; + mat->type = 2; + } else std::cerr << "unsupported material setting : " << identifier << std::endl; } @@ -238,6 +274,11 @@ void ObjParser::parseMtl(std::stringstream &input_line, Scene &scene) _matNames[matName] = scene.getMaterialData().size() - 1; } file.close(); + for(auto i = _matNames.begin(); i != _matNames.end(); i++) + { + std::cout << "key : " << i->first << std::endl; + std::cout << "value :" << i->second << std::endl; + } } void ObjParser::parse(Scene &scene, glm::vec3 offset, float scale, glm::mat4 transform) diff --git a/srcs/class/Scene.cpp b/srcs/class/Scene.cpp index 637479d..f3aab2c 100644 --- a/srcs/class/Scene.cpp +++ b/srcs/class/Scene.cpp @@ -6,7 +6,7 @@ /* By: ycontre +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/23 18:29:41 by ycontre #+# #+# */ -/* Updated: 2025/01/20 18:42:50 by ycontre ### ########.fr */ +/* Updated: 2025/01/21 14:45:04 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,7 +45,7 @@ bool Scene::parseScene(char *name) return (false); } - SceneParser scene_parser(this); + SceneParser scene_parser(this, name); while (std::getline(file, line)) { diff --git a/srcs/class/SceneParser.cpp b/srcs/class/SceneParser.cpp index e775558..26afc05 100644 --- a/srcs/class/SceneParser.cpp +++ b/srcs/class/SceneParser.cpp @@ -6,13 +6,13 @@ /* By: ycontre +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/26 21:43:51 by TheRed #+# #+# */ -/* Updated: 2025/01/16 15:17:10 by tomoron ### ########.fr */ +/* Updated: 2025/01/21 15:57:42 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "SceneParser.hpp" -SceneParser::SceneParser(Scene *scene) : _scene(scene) +SceneParser::SceneParser(Scene *scene, char *filename) : _scene(scene), _filename(filename) { object_parsers["sp"] = [](std::stringstream &ss) -> Object * { return (new Sphere(ss)); }; object_parsers["pl"] = [](std::stringstream &ss) -> Object * { return (new Plane(ss)); }; @@ -115,7 +115,7 @@ void SceneParser::parseObj(std::stringstream &line) glm::mat4 transform = glm::eulerAngleXYZ(glm::radians(xtransform), glm::radians(ytransform), glm::radians(ztransform)); - ObjParser obj(name); + ObjParser obj(name, _filename); obj.parse(*_scene, glm::vec3(x, y, z), (1.0 / scale), transform); }