Add parser for obj mesh format

This commit is contained in:
Théophile Bastian 2018-01-28 23:03:57 +01:00
parent 5c19700e42
commit 6c55d262ed
3 changed files with 50 additions and 1 deletions

View file

@ -7,7 +7,8 @@ CXXLIBS=
TARGETS=
OBJS=Implicit.o \
Mesh.o
Mesh.o \
util/ObjParser.o
all: $(TARGETS:=.bin)

32
util/ObjParser.cpp Normal file
View file

@ -0,0 +1,32 @@
#include "ObjParser.hpp"
ObjParser::ObjParser(const std::string& path)
: path(path)
{}
Mesh ObjParser::parse() {
std::ifstream handle(path);
Mesh output;
if(!handle.is_open())
throw std::runtime_error(std::string("Cannot open the file ") + path);
while(!handle.eof()) {
char type;
handle >> type;
if(type == 'v') { // vertice
double x, y, z;
handle >> x >> y >> z;
output.add_vertice(Point(x, y, z));
}
else {
int v1, v2, v3;
handle >> v1 >> v2 >> v3;
output.add_face(Face(v1, v2, v3));
}
handle.ignore('\n');
}
return output;
}

16
util/ObjParser.hpp Normal file
View file

@ -0,0 +1,16 @@
/** Parses .obj mesh files, outputting a `Mesh` */
#include <fstream>
#include <string>
#include <stdexcept>
#include "../Mesh.hpp"
class ObjParser {
public:
ObjParser(const std::string& path);
Mesh parse();
private:
std::string path;
};