add relative path for materials in obj files and for OBJ elements in scenes

This commit is contained in:
2025-01-21 16:15:07 +01:00
parent 8fbaf4a75c
commit 46ef719f29
16 changed files with 81 additions and 24 deletions

View File

@ -6,20 +6,21 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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<glm::vec3> &vertices, int inv
std::vector<std::string> ObjParser::objSplit(std::string str, std::string delim)
{
std::vector<std::string> 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)

View File

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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))
{

View File

@ -6,13 +6,13 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}