mirror of
https://github.com/TheRedShip/RT_GPU.git
synced 2025-09-27 18:48:36 +02:00
add relative path for materials in obj files and for OBJ elements in scenes
This commit is contained in:
@ -6,7 +6,7 @@
|
||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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<glm::vec3> &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<glm::vec3> vertices, size_t cur);
|
||||
std::vector<std::string> objSplit(std::string str, std::string delim);
|
||||
void getFaceVertices(std::vector<glm::vec3> &faceVertices, std::stringstream &line);
|
||||
|
||||
std::string _filename;
|
||||
std::ifstream _file;
|
||||
std::vector<glm::vec3> _vertices;
|
||||
std::vector<glm::vec2> _textureVertices;
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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<std::string, std::function<Object *(std::stringstream &)>> object_parsers;
|
||||
};
|
||||
|
3
scenes/obj/test.mtl
Normal file
3
scenes/obj/test.mtl
Normal file
@ -0,0 +1,3 @@
|
||||
newmtl patate
|
||||
d 0.5
|
||||
Kd 1 1 1
|
9
scenes/obj/test.obj
Normal file
9
scenes/obj/test.obj
Normal file
@ -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
|
@ -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
|
||||
|
@ -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)
|
||||
|
@ -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))
|
||||
{
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user