Bool ApplinkImporter::parseObjFile(char* path) { char line[256]; vector<string> stringArray; vector<string> subArray; ifstream fsImportObj; fsImportObj.open(path); if(!fsImportObj.is_open()){GePrint("File " + String(path) + " not found!");return false;} GePrint("Open file: " + String(path) + "."); long faceIndex = 0; int groupIndex = -1; // First group at 0 long pointIndex = 0; long texCoordIndex = 0; String matName; long idxMat = -1; StatusSetText("Parse file..."); while(!fsImportObj.eof()) { fsImportObj.getline(line, 255); //GePrint("row: " + String(buffer)); if(!strncmp("mtllib ", line, 7)) { char file[256]; sscanf(line, "mtllib %s", file); this->mtlFilePath = path; this->mtlFilePath.ClearSuffix(); this->mtlFilePath.SetFile(file); } else if(!strncmp("usemtl ", line, 7)) { String newMat; stringArray.clear(); istringstream iss(line); copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter<vector<string> >(stringArray)); newMat = stringArray[1].c_str(); bool inMats = false; for(unsigned int i = 0; i < this->matArray.size(); i++) { if(this->matArray[i].Name == newMat) { inMats = true; idxMat = i; break; } } if(!inMats) { this->matArray.resize(this->matArray.size() + 1); this->matArray[this->matArray.size() - 1].Name = newMat; idxMat = (long)this->matArray.size() - 1; } } else if(!strncmp("g ", line, 2)) { groupIndex++; faceIndex = 0; sscanf(line, "g %s", &groups[groupIndex].groupName); } else if(!strncmp("v ", line, 2)) { sscanf(line, "v %lf %lf %lf", &(verticies[pointIndex].x), &(verticies[pointIndex].y), &(verticies[pointIndex].z)); pointIndex++; } else if(!strncmp("vt", line, 2)) { sscanf(line, "vt %lf %lf", &(this->uvw[texCoordIndex].x), &(this->uvw[texCoordIndex].y)); sscanf(line, "vt %lf %lf %lf", &(this->uvw[texCoordIndex].x), &(this->uvw[texCoordIndex].y), &(this->uvw[texCoordIndex].z)); this->uvw[texCoordIndex].y = 1 - this->uvw[texCoordIndex].y; texCoordIndex++; } else if(!strncmp("f ", line, 2)) { if(idxMat != -1) { groups[groupIndex].polyMatIdx[faceIndex] = idxMat; } //GePrint("size: " + LongToString(sizeof(line))); stringArray.clear(); istringstream iss(line); copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter<vector<string> >(stringArray)); //GePrint("size: " + LongToString(stringArray.size())); subArray.clear(); int numFaces = (int)(stringArray.size() -1); if(numFaces == 3 || numFaces == 4) { for(UInt32 i = 1; i <= numFaces; i++) { subArray = this->Split(stringArray[i], '/'); for(UInt32 j=0; j < subArray.size(); j++) { string vIdx = subArray[j]; if(j==0)//position { groups[groupIndex].faces[faceIndex].vp[i-1] = atol(vIdx.c_str()) -1; } else if(j ==1 && vIdx != "")//tex coord { groups[groupIndex].faces[faceIndex].vt[i-1] = atol(vIdx.c_str()) - 1; } //Normals is not persent in this plugin. } } if(numFaces == 3)// this triangle, non quad. { groups[groupIndex].faces[faceIndex].vp[3] = atol(subArray[0].c_str()) - 1; if(subArray.size() > 1 && subArray[1] != "") { groups[groupIndex].faces[faceIndex].vt[3] = atol(subArray[1].c_str()) - 1; } } faceIndex++; } else// N-Gons { Triangulator* t = createTriangulator(); vector<UInt32> vpIdxs; vector<UInt32> vtIdxs; for(UInt32 j=1; j <= numFaces; j++) { subArray = this->Split(stringArray[j], '/'); UInt32 idx = atol(subArray[0].c_str()) -1; vpIdxs.push_back(idx); if(subArray[1] != "") { vtIdxs.push_back(atol(subArray[1].c_str()) -1); } t->addPoint(verticies[idx].x, verticies[idx].y, verticies[idx].z); } unsigned int tcount; unsigned int *indices = t->triangulate(tcount); for(UInt32 f = 0; f < tcount; f++) { for(UInt32 v=0; v < 4; v++) { UInt32 vv = 2-v; if(v == 3) vv = 0; groups[groupIndex].faces[faceIndex].vp[v] = vpIdxs[indices[f*3+vv]];//vp if(vpIdxs.size() > 0)//vt { groups[groupIndex].faces[faceIndex].vt[v] = vtIdxs[indices[f*3+vv]]; } } faceIndex++; //GePrint("Face3: " + LongToString(groups[groupIndex].faces[faceIndex].vp[0]) + ", " + LongToString(groups[groupIndex].faces[faceIndex].vp[1]) + ", " + LongToString(groups[groupIndex].faces[faceIndex].vp[2])); } releaseTriangulator(t); } } } fsImportObj.close(); return true; }