mirror of
https://github.com/TheRedShip/RT_GPU.git
synced 2025-09-28 02:58:35 +02:00
add mtl file support to obj parsing
This commit is contained in:
24
imgui.ini
24
imgui.ini
@ -1,24 +0,0 @@
|
|||||||
[Window][Debug##Default]
|
|
||||||
Pos=60,60
|
|
||||||
Size=400,400
|
|
||||||
|
|
||||||
[Window][Settings]
|
|
||||||
Pos=1515,93
|
|
||||||
Size=368,276
|
|
||||||
|
|
||||||
[Window][Material]
|
|
||||||
Pos=1620,226
|
|
||||||
Size=290,238
|
|
||||||
|
|
||||||
[Window][Camera]
|
|
||||||
Pos=1579,33
|
|
||||||
Size=279,183
|
|
||||||
|
|
||||||
[Window][Fog]
|
|
||||||
Pos=1621,467
|
|
||||||
Size=51,48
|
|
||||||
|
|
||||||
[Window][Fog settings]
|
|
||||||
Pos=1607,480
|
|
||||||
Size=284,137
|
|
||||||
|
|
@ -1,19 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* ObjParser.hpp :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2025/01/13 13:56:04 by tomoron #+# #+# */
|
|
||||||
/* Updated: 2025/01/13 13:56:05 by tomoron ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
# include "RT.hpp"
|
|
||||||
|
|
||||||
class ObjParser
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
ObjParser(std::stringstream RT
|
|
||||||
}
|
|
@ -6,7 +6,7 @@
|
|||||||
/* By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ */
|
/* By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/12/26 21:37:37 by TheRed #+# #+# */
|
/* Created: 2024/12/26 21:37:37 by TheRed #+# #+# */
|
||||||
/* Updated: 2025/01/13 17:39:22 by tomoron ### ########.fr */
|
/* Updated: 2025/01/15 12:34:11 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -27,8 +27,9 @@ class SceneParser
|
|||||||
void parseCamera(std::stringstream &line);
|
void parseCamera(std::stringstream &line);
|
||||||
void parseObj(std::stringstream &line);
|
void parseObj(std::stringstream &line);
|
||||||
glm::vec3 getVertex(std::stringstream &line);
|
glm::vec3 getVertex(std::stringstream &line);
|
||||||
Triangle *getFace(std::stringstream &line, std::vector<glm::vec3> &vertices);
|
Triangle *getFace(std::stringstream &line, std::vector<glm::vec3> &vertices, int mat);
|
||||||
long int getVertexIndex(std::stringstream &line, size_t size);
|
long int getVertexIndex(std::stringstream &line, size_t size);
|
||||||
|
void parseMtl(std::stringstream &line, std::map<std::string, int> &materials);
|
||||||
|
|
||||||
Scene *_scene;
|
Scene *_scene;
|
||||||
|
|
||||||
|
15
obj/Lowpoly_tree_sample.mtl
Normal file
15
obj/Lowpoly_tree_sample.mtl
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
# MTL written from D:\Cloud\Projects\Personal\Turbosquid\Lowpoly_Trees\Publish_Sample\Lowpoly_tree_sample.obj
|
||||||
|
newmtl Bark
|
||||||
|
Kd 0.207595 0.138513 0.055181
|
||||||
|
Ns 256
|
||||||
|
d 1
|
||||||
|
illum 1
|
||||||
|
Ka 0 0 0
|
||||||
|
Ks 0 0 0
|
||||||
|
newmtl Tree
|
||||||
|
Kd 0.256861 0.440506 0.110769
|
||||||
|
Ns 256
|
||||||
|
d 1
|
||||||
|
illum 1
|
||||||
|
Ka 0 0 0
|
||||||
|
Ks 0 0 0
|
1304
obj/Lowpoly_tree_sample.obj
Normal file
1304
obj/Lowpoly_tree_sample.obj
Normal file
File diff suppressed because it is too large
Load Diff
@ -22,4 +22,4 @@ sp -3.8 -0.1 0.60 0.5 5
|
|||||||
|
|
||||||
cy 0 1 2 0.5 2 -1.5 0 0.75 1
|
cy 0 1 2 0.5 2 -1.5 0 0.75 1
|
||||||
|
|
||||||
# OBJ obj/teapot.obj
|
OBJ obj/Lowpoly_tree_sample.obj
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/12/26 21:43:51 by TheRed #+# #+# */
|
/* Created: 2024/12/26 21:43:51 by TheRed #+# #+# */
|
||||||
/* Updated: 2025/01/13 18:49:10 by ycontre ### ########.fr */
|
/* Updated: 2025/01/15 13:27:43 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -97,10 +97,8 @@ glm::vec3 SceneParser::getVertex(std::stringstream &line)
|
|||||||
{
|
{
|
||||||
glm::vec3 res;
|
glm::vec3 res;
|
||||||
|
|
||||||
std::cout << line.str() << std::endl;
|
|
||||||
if(!(line >> res.x >> res.y >> res.z))
|
if(!(line >> res.x >> res.y >> res.z))
|
||||||
throw std::runtime_error("syntax error in obj file while parsing vertex");
|
throw std::runtime_error("syntax error in obj file while parsing vertex");
|
||||||
res.z *= -1;
|
|
||||||
return(res);
|
return(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,38 +115,133 @@ long int SceneParser::getVertexIndex(std::stringstream &line, size_t size)
|
|||||||
return(index - 1);
|
return(index - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
Triangle *SceneParser::getFace(std::stringstream &line, std::vector<glm::vec3> &vertices)
|
Triangle *SceneParser::getFace(std::stringstream &line, std::vector<glm::vec3> &vertices, int mat)
|
||||||
{
|
{
|
||||||
glm::vec3 triangle[3];
|
glm::vec3 triangle[3];
|
||||||
|
|
||||||
triangle[0] = vertices[getVertexIndex(line, vertices.size())];
|
triangle[0] = vertices[getVertexIndex(line, vertices.size())];
|
||||||
triangle[1] = vertices[getVertexIndex(line, vertices.size())];
|
triangle[1] = vertices[getVertexIndex(line, vertices.size())];
|
||||||
triangle[2] = vertices[getVertexIndex(line, vertices.size())];
|
triangle[2] = vertices[getVertexIndex(line, vertices.size())];
|
||||||
return (new Triangle(triangle[0], triangle[1], triangle[2], 0));
|
return (new Triangle(triangle[0], triangle[1], triangle[2], mat));
|
||||||
|
}
|
||||||
|
|
||||||
|
void SceneParser::parseMtl(std::stringstream &input_line, std::map<std::string, int> &materials)
|
||||||
|
{
|
||||||
|
std::string filename;
|
||||||
|
std::ifstream file;
|
||||||
|
std::string matName;
|
||||||
|
std::string identifier;
|
||||||
|
std::string line;
|
||||||
|
float tmp;
|
||||||
|
Material *mat;
|
||||||
|
|
||||||
|
input_line >> filename;
|
||||||
|
file.open(filename);
|
||||||
|
mat = 0;
|
||||||
|
if(!file.is_open())
|
||||||
|
throw std::runtime_error("OBJ : could not open material file");
|
||||||
|
while(getline(file, line))
|
||||||
|
{
|
||||||
|
if(line[0] == '#' || !line[0])
|
||||||
|
continue;
|
||||||
|
std::stringstream lineStream(line);
|
||||||
|
lineStream >> identifier;
|
||||||
|
if(identifier == "newmtl")
|
||||||
|
{
|
||||||
|
if(mat)
|
||||||
|
{
|
||||||
|
_scene->addMaterial(mat);
|
||||||
|
materials[matName] = _scene->getMaterialData().size() - 1;
|
||||||
|
}
|
||||||
|
lineStream >> matName;
|
||||||
|
if(matName.empty())
|
||||||
|
throw std::runtime_error("OBJ: syntax error in material file, missing material name");
|
||||||
|
mat = new Material;
|
||||||
|
bzero(mat, sizeof(Material));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if(!mat)
|
||||||
|
throw std::runtime_error("OBJ: error in material file, material name not defined");
|
||||||
|
if(/*identifier == "Ka" || */identifier == "Kd")
|
||||||
|
{
|
||||||
|
if(!(lineStream >> mat->color.x >> mat->color.y >> mat->color.z))
|
||||||
|
throw std::runtime_error("OBJ: syntax error while getting material color");
|
||||||
|
}
|
||||||
|
else if(identifier == "Ns")
|
||||||
|
{
|
||||||
|
if(!(lineStream >> mat->roughness) || mat->roughness > 1000 || mat->roughness < 0)
|
||||||
|
throw std::runtime_error("OBJ: syntax error while getting material softness");
|
||||||
|
mat->roughness /= 1000;
|
||||||
|
}
|
||||||
|
else if(identifier == "Ke")
|
||||||
|
{
|
||||||
|
if(!(lineStream >> tmp))
|
||||||
|
throw std::runtime_error("OBJ: syntax error while getting material emission");
|
||||||
|
mat->emission += tmp;
|
||||||
|
if(!(lineStream >> tmp))
|
||||||
|
throw std::runtime_error("OBJ: syntax error while getting material emission");
|
||||||
|
mat->emission += tmp;
|
||||||
|
if(!(lineStream >> tmp))
|
||||||
|
throw std::runtime_error("OBJ: syntax error while getting material emission");
|
||||||
|
mat->emission += tmp;
|
||||||
|
mat->emission /= 3;
|
||||||
|
}
|
||||||
|
else if(identifier == "Ni")
|
||||||
|
{
|
||||||
|
if(!(lineStream >> mat->refraction))
|
||||||
|
throw std::runtime_error("OBJ: syntax error while getting material refraction");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
std::cerr << "unsupported material setting : " << identifier << std::endl;
|
||||||
|
}
|
||||||
|
if(mat)
|
||||||
|
{
|
||||||
|
_scene->addMaterial(mat);
|
||||||
|
materials[matName] = _scene->getMaterialData().size() - 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SceneParser::parseObj(std::stringstream &objInfo)
|
void SceneParser::parseObj(std::stringstream &objInfo)
|
||||||
{
|
{
|
||||||
std::vector<glm::vec3> vertices;
|
std::vector<glm::vec3> vertices;
|
||||||
std::string filename;
|
std::string filename;
|
||||||
|
std::map<std::string, int> matNames;
|
||||||
std::string line;
|
std::string line;
|
||||||
std::string identifier;
|
std::string identifier;
|
||||||
std::ifstream file;
|
std::ifstream file;
|
||||||
|
int curMat;
|
||||||
|
|
||||||
objInfo >> filename;
|
objInfo >> filename;
|
||||||
file.open(filename);
|
file.open(filename);
|
||||||
|
curMat = 0;
|
||||||
if (!file.is_open())
|
if (!file.is_open())
|
||||||
throw std::runtime_error("OBJ : could not open object file");
|
throw std::runtime_error("OBJ : could not open object file");
|
||||||
while (getline(file, line))
|
while (getline(file, line))
|
||||||
{
|
{
|
||||||
|
try{
|
||||||
if(line[0] == '#' || line.empty())
|
if(line[0] == '#' || line.empty())
|
||||||
continue;
|
continue;
|
||||||
std::stringstream lineStream(line);
|
std::stringstream lineStream(line);
|
||||||
|
identifier = "";
|
||||||
lineStream >> identifier;
|
lineStream >> identifier;
|
||||||
if(identifier == "v")
|
if(identifier == "v")
|
||||||
vertices.push_back(getVertex(lineStream));
|
vertices.push_back(getVertex(lineStream));
|
||||||
else if (identifier == "f")
|
else if (identifier == "f")
|
||||||
_scene->addObject(getFace(lineStream, vertices));
|
_scene->addObject(getFace(lineStream, vertices, curMat));
|
||||||
|
else if (identifier == "mtllib")
|
||||||
|
parseMtl(lineStream, matNames);
|
||||||
|
else if (identifier == "usemtl")
|
||||||
|
{
|
||||||
|
lineStream >> identifier;
|
||||||
|
if(matNames.find(identifier) == matNames.end())
|
||||||
|
throw std::runtime_error("OBJ: invalid material name");
|
||||||
|
curMat = matNames[identifier];
|
||||||
|
}
|
||||||
|
}catch (std::exception &e)
|
||||||
|
{
|
||||||
|
std::cerr << line << std::endl;
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user