#include "ObjModel.h" #include "utils.h" #include #include #include #include void ObjModel::Init(const char*modelFilePath) { struct VertexInfo { float v[3]; VertexInfo() { memset(v, 0, sizeof(float) * 3); } }; struct VertexDefine { int positionIndex; int texcoordIndex; int normalIndex; }; std::vector positions; std::vector texcoords; std::vector normals; std::vector vertices; std::vector faces; //load model from file char*fileContent = LoadFileContent(modelFilePath); //decode model std::stringstream ssFileContent(fileContent); char szOneLine[256]; std::string temp; while (!ssFileContent.eof()) { memset(szOneLine, 0, 256); ssFileContent.getline(szOneLine, 256); if (strlen(szOneLine)>0) { std::stringstream ssOneLine(szOneLine); if (szOneLine[0]=='v') { if (szOneLine[1]=='t') { VertexInfo vi; ssOneLine >> temp;//vt ssOneLine >> vi.v[0]; ssOneLine >> vi.v[1]; texcoords.push_back(vi); } else if (szOneLine[1]=='n') { VertexInfo vi; ssOneLine >> temp;//vn ssOneLine >> vi.v[0]; ssOneLine >> vi.v[1]; ssOneLine >> vi.v[2]; normals.push_back(vi); } else { VertexInfo vi; ssOneLine >> temp;//v ssOneLine >> vi.v[0]; ssOneLine >> vi.v[1]; ssOneLine >> vi.v[2]; positions.push_back(vi); } } else if(szOneLine[0]=='f') { // ssOneLine >> temp;//f std::string vertexStr; for (int i = 0; i < 3; ++i) { ssOneLine >> vertexStr; size_t pos = vertexStr.find_first_of('/'); std::string positionIndexStr = vertexStr.substr(0, pos); size_t pos2 = vertexStr.find_first_of('/', pos + 1); std::string texcoordIndexStr = vertexStr.substr(pos + 1, pos2 - pos - 1); std::string normalIndexStr = vertexStr.substr(pos2 + 1, vertexStr.length() - pos2 - 1); VertexDefine vd; vd.positionIndex = atoi(positionIndexStr.c_str())-1; vd.texcoordIndex = atoi(texcoordIndexStr.c_str())-1; vd.normalIndex = atoi(normalIndexStr.c_str())-1; //trim the same vertice int nCurrentVertexIndex = -1; size_t nCurrentVerticeCount = vertices.size(); for(int j=0;j