add parsing of faces with "/" in obj files

This commit is contained in:
2025-01-18 19:10:58 +01:00
parent 4863a5ef77
commit d62b5a5de0
4 changed files with 53 additions and 25 deletions

BIN
RT

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/16 15:00:49 by tomoron #+# #+# */ /* Created: 2025/01/16 15:00:49 by tomoron #+# #+# */
/* Updated: 2025/01/17 17:19:51 by tomoron ### ########.fr */ /* Updated: 2025/01/18 18:32:42 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,14 +23,16 @@ class ObjParser
void parse(Scene &scene); void parse(Scene &scene);
private: private:
glm::vec3 getVertex(std::stringstream &line); glm::vec3 getVertex(std::stringstream &line);
glm::vec2 getUV(std::stringstream &line); glm::vec2 getUV(std::stringstream &line);
void addFace(std::stringstream &line, Scene &scene); void addFace(std::stringstream &line, Scene &scene);
long int checkVertexIndex(int index, size_t size); long int checkVertexIndex(int index, size_t size);
void parseMtl(std::stringstream &line, Scene &scene); void parseMtl(std::stringstream &line, Scene &scene);
bool addTriangleFromPolygon(std::vector<glm::vec3> &vertices, Scene &scene, int inv); bool addTriangleFromPolygon(std::vector<glm::vec3> &vertices, Scene &scene, int inv);
void addTriangle(glm::vec3 v1, glm::vec3 v2, glm::vec3 v3, Scene &scene); void addTriangle(glm::vec3 v1, glm::vec3 v2, glm::vec3 v3, Scene &scene);
int pointInTriangle(glm::vec3 pts[3], std::vector<glm::vec3> vertices, size_t cur); 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::ifstream _file; std::ifstream _file;
std::vector<glm::vec3> _vertices; std::vector<glm::vec3> _vertices;

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/16 15:00:33 by tomoron #+# #+# */ /* Created: 2025/01/16 15:00:33 by tomoron #+# #+# */
/* Updated: 2025/01/17 19:31:52 by tomoron ### ########.fr */ /* Updated: 2025/01/18 18:57:31 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -96,9 +96,6 @@ bool ObjParser::addTriangleFromPolygon(std::vector<glm::vec3> &vertices, Scene &
v2 = vertices[i]; v2 = vertices[i];
v3 = vertices[(i + 1) % vertices.size()]; v3 = vertices[(i + 1) % vertices.size()];
std::cout << glm::to_string(v1) << std::endl;
std::cout << glm::to_string(v2) << std::endl;
std::cout << glm::to_string(v3) << std::endl;
if (inv) if (inv)
dot = glm::cross(v2 - v1, v2 - v3).z; dot = glm::cross(v2 - v1, v2 - v3).z;
else else
@ -115,24 +112,53 @@ bool ObjParser::addTriangleFromPolygon(std::vector<glm::vec3> &vertices, Scene &
return(0); return(0);
} }
std::vector<std::string> ObjParser::objSplit(std::string str, std::string delim)
{
std::vector<std::string> res;
while(str.find(delim) != std::string::npos)
{
res.push_back(str.substr(0, str.find(delim)));
str.erase(0, str.find(delim) + 1);
}
res.push_back(str);
return(res);
}
void ObjParser::getFaceVertices(std::vector<glm::vec3> &faceVertices, std::stringstream &line)
{
std::string el;
std::vector<std::string> sp;
while(line >> el)
{
sp = objSplit(el, "/");
if(sp.size() > 3)
std::runtime_error("OBJ : too many values in an element of a face");
if(sp.size() == 0)
std::runtime_error("OBJ : wtf ?");
faceVertices.push_back(_vertices[checkVertexIndex(std::stoi(sp[0]), _vertices.size())]);
}
}
void ObjParser::addFace(std::stringstream &line, Scene &scene) void ObjParser::addFace(std::stringstream &line, Scene &scene)
{ {
int vert_index; std::vector<glm::vec3> faceVertices;
std::vector<glm::vec3> face_vertices;
std::string el;
while((line >> vert_index)) getFaceVertices(faceVertices, line);
face_vertices.push_back(_vertices[checkVertexIndex(vert_index, _vertices.size())]); if(!line.eof())
if(face_vertices.size() < 3) throw std::runtime_error("OBJ : an error occured while paring face");
if(faceVertices.size() < 3)
throw std::runtime_error("OBJ : face does not have enough vertices"); throw std::runtime_error("OBJ : face does not have enough vertices");
while(face_vertices.size() > 3) while(faceVertices.size() > 3)
if (!addTriangleFromPolygon(face_vertices, scene, 0)) if (!addTriangleFromPolygon(faceVertices, scene, 0))
addTriangleFromPolygon(face_vertices, scene, 1); addTriangleFromPolygon(faceVertices, scene, 1);
if(!line.eof()) if(!line.eof())
throw std::runtime_error("OBJ: an error occured while parsing face"); throw std::runtime_error("OBJ: an error occured while parsing face");
addTriangle(face_vertices[0], face_vertices[1], face_vertices[2], scene); addTriangle(faceVertices[0], faceVertices[1], faceVertices[2], scene);
} }
void ObjParser::addTriangle(glm::vec3 v1, glm::vec3 v2, glm::vec3 v3, Scene &scene) void ObjParser::addTriangle(glm::vec3 v1, glm::vec3 v2, glm::vec3 v3, Scene &scene)

View File

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */ /* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/13 16:16:24 by TheRed #+# #+# */ /* Created: 2024/10/13 16:16:24 by TheRed #+# #+# */
/* Updated: 2025/01/15 19:34:25 by ycontre ### ########.fr */ /* Updated: 2025/01/18 14:22:38 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -175,7 +175,7 @@ void Window::imGuiRender()
ImGui::Text("Fps: %d", int(_fps)); ImGui::Text("Fps: %d", int(_fps));
ImGui::Text("Frame: %d", _frameCount); ImGui::Text("Frame: %d", _frameCount);
ImGui::Text("Objects: %d", _scene->getObjectData().size() + _scene->getTriangleData().size()); ImGui::Text("Objects: %zu", _scene->getObjectData().size() + _scene->getTriangleData().size());
ImGui::Separator(); ImGui::Separator();
if (ImGui::Checkbox("Accumulate", &accumulate)) if (ImGui::Checkbox("Accumulate", &accumulate))