Advertisement
adwas33

Untitled

May 3rd, 2022
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. bool loadOBJ(
  2. const char* path,
  3. std::vector < glm::vec3 >& out_vertices,
  4. std::vector < glm::vec2 >& out_uvs,
  5. std::vector < glm::vec3 >& out_normals
  6. )
  7. {
  8. std::vector< unsigned int > vertexIndices, uvIndices, normalIndices;
  9. std::vector< glm::vec3 > temp_vertices;
  10. std::vector< glm::vec2 > temp_uvs;
  11. std::vector< glm::vec3 > temp_normals;
  12. FILE* file = fopen("D:\\GrafikaKomputerowa\\GKPomocOlka\\Grass_block.obj","r");
  13. if (file == NULL) {
  14. printf("Impossible to open the file !\n");
  15. return false;
  16. }
  17. while (1) {
  18.  
  19. char lineHeader[128];
  20. // read the first word of the line
  21. int res = fscanf(file, "%s", lineHeader);
  22. if (res == EOF)
  23. break; // EOF = End Of File. Quit the loop.
  24.  
  25. // else : parse lineHeader
  26. if (strcmp(lineHeader, "v") == 0) {
  27. glm::vec3 vertex;
  28. fscanf(file, "%f %f %f\n", &vertex.x, &vertex.y, &vertex.z);
  29. temp_vertices.push_back(vertex);
  30. }
  31. else if (strcmp(lineHeader, "vt") == 0) {
  32. glm::vec2 uv;
  33. fscanf(file, "%f %f\n", &uv.x, &uv.y);
  34. temp_uvs.push_back(uv);
  35. }
  36. else if (strcmp(lineHeader, "vn") == 0) {
  37. glm::vec3 normal;
  38. fscanf(file, "%f %f %f\n", &normal.x, &normal.y, &normal.z);
  39. temp_normals.push_back(normal);
  40. }
  41. else if (strcmp(lineHeader, "f") == 0) {
  42. std::string vertex1, vertex2, vertex3;
  43. unsigned int vertexIndex[3], uvIndex[3], normalIndex[3];
  44. int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &vertexIndex[0], &uvIndex[0], &normalIndex[0], &vertexIndex[1], &uvIndex[1], &normalIndex[1], &vertexIndex[2], &uvIndex[2], &normalIndex[2]);
  45. if (matches != 9) {
  46. printf("File can't be read by our simple parser : ( Try exporting with other options\n");
  47. return false;
  48. }
  49. vertexIndices.push_back(vertexIndex[0]);
  50. vertexIndices.push_back(vertexIndex[1]);
  51. vertexIndices.push_back(vertexIndex[2]);
  52. uvIndices.push_back(uvIndex[0]);
  53. uvIndices.push_back(uvIndex[1]);
  54. uvIndices.push_back(uvIndex[2]);
  55. normalIndices.push_back(normalIndex[0]);
  56. normalIndices.push_back(normalIndex[1]);
  57. normalIndices.push_back(normalIndex[2]);
  58. }
  59. // For each vertex of each triangle
  60. for (unsigned int i = 0; i < vertexIndices.size(); i++) {
  61. unsigned int vertexIndex = vertexIndices[i];
  62. glm::vec3 vertex = temp_vertices[vertexIndex - 1];
  63. out_vertices.push_back(vertex);
  64. }
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement