Last active
April 23, 2019 12:15
-
-
Save ainsleyrutterford/9abf9275fa3d0d1d559c5c9aac808bee to your computer and use it in GitHub Desktop.
Converts .obj files to a vector of Triangles.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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