void ObjLoader::ReadObjFileData(FILE* objFile) { char buffer[256]; while (true) { int read = fscanf_s(objFile, "%s", buffer, sizeof(buffer)); if (read == EOF) break; //if first part of the line is "v" if (strcmp(buffer, "v") == 0) { //printf("Found a Vertex:\n"); glm::vec3 vert; fscanf_s(objFile, "%f %f %f\n", &vert.x, &vert.y, &vert.z); objFileVerts.push_back(vert); } //if first part of the line is "vn" else if (strcmp(buffer, "vn") == 0) { //printf("Found a Vertex Normal:\n"); glm::vec3 normal; fscanf_s(objFile, "%f %f %f\n", &normal.x, &normal.y, &normal.z); objFileNormals.push_back(normal); } //if first part of the line is "f" else if (strcmp(buffer, "f") == 0) { //printf("Found f:\n"); fscanf_s(objFile, "%256[^\n]", buffer, sizeof(buffer)); std::string s(buffer); std::stringstream stream(s); std::string split; std::vector<std::string> splits; while (stream >> split) splits.push_back(split); std::vector<FaceVertexData> tmpFaceVerts; for (int i = 0; i < splits.size(); i++) tmpFaceVerts.push_back(ExtractFaceVertexData(splits[i])); int i = 0; do { faceVerts.push_back(tmpFaceVerts[0]); faceVerts.push_back(tmpFaceVerts[i + 1]); faceVerts.push_back(tmpFaceVerts[i + 2]); i++; } while (i < tmpFaceVerts.size() - 2); } else { } } }
void ObjLoader::ReadObjFileData(FILE* objFile) { char buffer[256]; while (true) { int read = fscanf_s(objFile, "%s", buffer, sizeof(buffer)); if (read == EOF) break; // if the line contains a vertex if (strcmp(buffer, "v") == 0) { glm::vec3 vert; fscanf_s(objFile, "%f %f %f\n", &vert.x, &vert.y, &vert.z); objFileVerts.push_back(vert); } // if the line contains a vertex normal else if (strcmp(buffer, "vn") == 0) { glm::vec3 normal; fscanf_s(objFile, "%f %f %f\n", &normal.x, &normal.y, &normal.z); objFileNormals.push_back(normal); } // if the line contains a vertex texture coordinate else if (strcmp(buffer, "vt") == 0) { glm::vec2 texCoord; fscanf_s(objFile, "%f %f\n", &texCoord.x, &texCoord.y); texCoord.y = 1 - texCoord.y; objFileTexCoords.push_back(texCoord); } // if the line contains a face else if (strcmp(buffer, "f") == 0) { //read the line, turn it into a std::string and put that in a stringstream fscanf_s(objFile, "%256[^\n]", buffer, sizeof(buffer)); std::string s(buffer); std::stringstream stream(s); /* find each "word" in the stringstream (i.e. something like "1/2/3" * and store it as 'split', also add each split to the 'splits' std::vec */ std::string split; std::vector<std::string> splits; while (stream >> split) splits.push_back(split); /* now just send each split to the function which works out what they contain * and store what comes back in the faceVerts std::vector */ for (int i = 0; i < splits.size(); i++) vertices.push_back(ExtractFaceVertexData(splits[i])); } }