Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- GLuint model::loadOBJ(const char *path, std::vector<glm::vec3> &outVertices, std::vector<glm::vec2> &outUVs, std::vector<glm::vec3> &outNormals) {
- std::cout << "Loading OBJ file " << path << std::endl;
- std::vector<float> vertexIndices;
- std::vector<float> uvIndices;
- std::vector<float> normalIndices;
- std::vector<glm::vec3> tempVertices;
- std::vector<glm::vec2> tempUVs;
- std::vector<glm::vec3> tempNormals;
- std::ifstream file(path);
- if(!file.is_open()) {
- std::cout << "Unable to open OBJ file " << path << std::endl;
- return -1;
- }
- char line[256];
- while(file.getline(line, 256)) {
- char lineHeader[128];
- int res = sscanf(line, "%s", lineHeader);
- if(res == EOF)
- break;
- if(strcmp(lineHeader, "v") == 0) {
- glm::vec3 vertex;
- sscanf(line, "v %f %f %f\n", &(vertex.x), &(vertex.y), &(vertex.z));
- std::cout << "Adding vertex... (" << vertex.x << ", " << vertex.y << ", " << vertex.y << ")" << std::endl;
- tempVertices.push_back(vertex);
- } else if(strcmp(lineHeader, "vt") == 0) {
- std::cout << line << std::endl;
- glm::vec2 uv;
- sscanf(line, "vt %f %f\n", &(uv.x), &(uv.y));
- tempUVs.push_back(uv);
- } else if(strcmp(lineHeader, "vn") == 0) {
- std::cout << line << std::endl;
- glm::vec3 normal;
- sscanf(line, "vn %f %f %f\n", &(normal.x), &(normal.y), &(normal.z));
- tempNormals.push_back(normal);
- } else if(strcmp(lineHeader, "f") == 0) {
- std::string vertex1, vertex2, vertex3;
- unsigned int vertexIndex[3], uvIndex[3], normalIndex[3];
- std::cout << line << std::endl;
- int matches = sscanf(line, "f %d//%d %d//%d %d//%d\n", &vertexIndex[0], &normalIndex[0],
- &vertexIndex[1], &normalIndex[1], &vertexIndex[2], &normalIndex[2]);
- if(matches != 6) {
- std::cout << "Unable to parse OBJ file " << path << std::endl << "Matches: " << matches << std::endl;
- return -1;
- }
- vertexIndices.push_back(vertexIndex[0]);
- vertexIndices.push_back(vertexIndex[1]);
- vertexIndices.push_back(vertexIndex[2]);
- uvIndices.push_back(uvIndex[0]);
- uvIndices.push_back(uvIndex[1]);
- uvIndices.push_back(uvIndex[2]);
- normalIndices.push_back(normalIndex[0]);
- normalIndices.push_back(normalIndex[1]);
- normalIndices.push_back(normalIndex[2]);
- }
- }
- for(unsigned int i = 0; i < vertexIndices.size(); i++) {
- unsigned int vertexIndex = vertexIndices[i];
- glm::vec3 vertex = tempVertices[vertexIndex - 1];
- outVertices.push_back(vertex);
- }
- std::cout << "Finished vertex stuff" << std::endl;
- /*
- for(unsigned int i = 0; i < uvIndices.size(); i++) {
- unsigned int uvIndex = uvIndices[i];
- glm::vec2 uv = tempUVs[uvIndex - 1];
- outUVs.push_back(uv);
- } */
- std::cout << "Finished UV stuff" << std::endl;
- for(unsigned int i = 0; i < normalIndices.size(); i++) {
- unsigned int normalIndex = normalIndices[i];
- glm::vec3 normal = tempNormals[normalIndex - 1];
- outNormals.push_back(normal);
- }
- std::cout << "Finished normal stuff" << std::endl;
- std::cout << "Loaded OBJ file " << path << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment