32 lines
698 B
C++
32 lines
698 B
C++
#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;
|
|
}
|