Skip to content

Instantly share code, notes, and snippets.

@ainsleyrutterford
Last active April 23, 2019 12:15
Show Gist options
  • Save ainsleyrutterford/9abf9275fa3d0d1d559c5c9aac808bee to your computer and use it in GitHub Desktop.
Save ainsleyrutterford/9abf9275fa3d0d1d559c5c9aac808bee to your computer and use it in GitHub Desktop.
Converts .obj files to a vector of Triangles.
#include <iostream>
#include <fstream>
#include <sstream>
#include "TestModelH.h"
using namespace std;
using glm::vec3;
using glm::vec4;
vector<Triangle> load_obj(string filename) {
ifstream source(filename);
vector<vec4> vertices;
vector<Triangle> triangles;
string line;
while (getline(source, line)) {
istringstream in(line);
string s;
in >> s;
if (s == "v") {
float x, y, z;
in >> x >> y >> z;
vertices.push_back(vec4(x, y, z, 1.f));
} else if (s == "f") {
int v1, v2, v3;
in >> v1 >> v2 >> v3;
Triangle triangle = Triangle(vertices[v1-1], vertices[v2-1], vertices[v3-1], vec3(0.8f, 0.8f, 0.8f));
triangle.normal.x *= -1;
triangle.normal.y *= -1;
triangle.normal.z *= -1;
triangles.push_back(triangle);
}
}
return triangles;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment