Created
January 10, 2012 11:19
-
-
Save satoruhiga/1588533 to your computer and use it in GitHub Desktop.
load .obj file
This file contains hidden or 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
#pragma once | |
#include "ofMain.h" | |
bool loadObj(string path, ofMesh &mesh, ofImage *tex = NULL) | |
{ | |
path = ofToDataPath(path); | |
if (!ofFile::doesFileExist(path)) return false; | |
ifstream ifs(path.c_str()); | |
mesh.clear(); | |
vector<ofVec3f> vertices, normals; | |
vector<ofFloatColor> colors; | |
vector<ofVec2f> texcoords; | |
vector< vector<string> > faces; | |
char line[512]; | |
while (!ifs.eof()) | |
{ | |
ifs.getline(line, sizeof(line), '\n'); | |
vector<string> elem = ofSplitString(line, " ", true, true); | |
if (elem.empty()) continue; | |
else if (elem[0] == "#") continue; | |
else if (elem[0] == "v") | |
{ | |
if (elem.size() == 4) | |
{ | |
vertices.push_back(ofVec3f(ofToFloat(elem[1]), | |
ofToFloat(elem[2]), | |
ofToFloat(elem[3]))); | |
} | |
else if (elem.size() == 7) | |
{ | |
vertices.push_back(ofVec3f(ofToFloat(elem[1]), | |
ofToFloat(elem[2]), | |
ofToFloat(elem[3]))); | |
colors.push_back(ofFloatColor(ofToFloat(elem[4]), | |
ofToFloat(elem[5]), | |
ofToFloat(elem[6]))); | |
} | |
} | |
else if (elem[0] == "vn" && elem.size() == 4) | |
{ | |
normals.push_back(ofVec3f(ofToFloat(elem[1]), | |
ofToFloat(elem[2]), | |
ofToFloat(elem[3]))); | |
} | |
else if (elem[0] == "vt" && (elem.size() == 3 || elem.size() == 4) && tex != NULL) | |
{ | |
texcoords.push_back(ofVec2f(tex->getWidth() * ofToFloat(elem[1]), | |
tex->getHeight() * (1 - ofToFloat(elem[2])))); | |
} | |
else if (elem[0] == "f" && elem.size() == 4) | |
{ | |
vector<string> s(3); | |
s[0] = elem[1]; | |
s[1] = elem[2]; | |
s[2] = elem[3]; | |
faces.push_back(s); | |
} | |
} | |
if (faces.empty()) | |
{ | |
for (int i = 0; i < vertices.size(); i++) | |
mesh.addVertex(vertices[i]); | |
for (int i = 0; i < colors.size(); i++) | |
mesh.addColor(colors[i]); | |
for (int i = 0; i < normals.size(); i++) | |
mesh.addNormal(normals[i]); | |
if (tex != NULL) | |
{ | |
for (int i = 0; i < texcoords.size(); i++) | |
mesh.addTexCoord(texcoords[i]); | |
} | |
mesh.setMode(OF_PRIMITIVE_POINTS); | |
} | |
else | |
{ | |
for (int i = 0; i < faces.size(); i++) | |
{ | |
vector<string> &elem = faces[i]; | |
vector<string> v1 = ofSplitString(elem[0], "/", false, true); | |
vector<string> v2 = ofSplitString(elem[1], "/", false, true); | |
vector<string> v3 = ofSplitString(elem[2], "/", false, true); | |
if (v1[0] != "") | |
{ | |
mesh.addVertex(vertices[ofToInt(v1[0]) - 1]); | |
mesh.addVertex(vertices[ofToInt(v2[0]) - 1]); | |
mesh.addVertex(vertices[ofToInt(v3[0]) - 1]); | |
if (!colors.empty()) | |
{ | |
mesh.addColor(colors[ofToInt(v1[0]) - 1]); | |
mesh.addColor(colors[ofToInt(v2[0]) - 1]); | |
mesh.addColor(colors[ofToInt(v3[0]) - 1]); | |
} | |
} | |
if (v1.size() > 1 && v1[1] != "" && tex != NULL) | |
{ | |
mesh.addTexCoord(texcoords[ofToInt(v1[1]) - 1]); | |
mesh.addTexCoord(texcoords[ofToInt(v2[1]) - 1]); | |
mesh.addTexCoord(texcoords[ofToInt(v3[1]) - 1]); | |
} | |
if (v1.size() > 2 && v1[2] != "") | |
{ | |
mesh.addNormal(normals[ofToInt(v1[2]) - 1]); | |
mesh.addNormal(normals[ofToInt(v2[2]) - 1]); | |
mesh.addNormal(normals[ofToInt(v3[2]) - 1]); | |
} | |
int t = mesh.getNumIndices(); | |
mesh.addTriangle(t, t+1, t+2); | |
} | |
mesh.setMode(OF_PRIMITIVE_TRIANGLES); | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment