MeshPrimitive_tinyobj.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #ifndef GUL_MESH_PRIMITIVE_TINY_OBJ_H
  2. #define GUL_MESH_PRIMITIVE_TINY_OBJ_H
  3. #include "MeshPrimitive2.h"
  4. #include <glm/glm.hpp>
  5. #include <tiny_obj_loader.h>
  6. #include <filesystem>
  7. namespace gul
  8. {
  9. inline MeshPrimitive MeshPrimitive_TinyObjLoad(std::filesystem::path const &inputFile,
  10. std::string *error,
  11. std::string *warning)
  12. {
  13. std::string inputfile = inputFile.generic_string();//"cornell_box.obj";
  14. tinyobj::ObjReaderConfig reader_config;
  15. reader_config.mtl_search_path = "./"; // Path to material files
  16. tinyobj::ObjReader reader;
  17. if (!reader.ParseFromFile(inputfile, reader_config)) {
  18. if (!reader.Error().empty()) {
  19. *error = reader.Error();
  20. }
  21. return {};
  22. exit(1);
  23. }
  24. if (!reader.Warning().empty()) {
  25. *warning = reader.Warning();
  26. //std::cout << "TinyObjReader: " << reader.Warning();
  27. }
  28. auto& attrib = reader.GetAttrib();
  29. auto& shapes = reader.GetShapes();
  30. auto& materials = reader.GetMaterials();
  31. (void)materials;
  32. MeshPrimitive output;
  33. uint32_t _index=0;
  34. // Loop over shapes
  35. for (size_t s = 0; s < shapes.size(); s++)
  36. {
  37. auto & _shape = shapes[s];
  38. // Loop over faces(polygon)
  39. size_t index_offset = 0;
  40. for (size_t f = 0; f < _shape.mesh.num_face_vertices.size(); f++)
  41. {
  42. size_t fv = size_t(_shape.mesh.num_face_vertices[f]);
  43. // Loop over vertices in the face.
  44. for (size_t v = 0; v < fv; v++)
  45. {
  46. // access to vertex
  47. tinyobj::index_t idx = _shape.mesh.indices[index_offset + v];
  48. tinyobj::real_t vx = attrib.vertices[3*size_t(idx.vertex_index)+0];
  49. tinyobj::real_t vy = attrib.vertices[3*size_t(idx.vertex_index)+1];
  50. tinyobj::real_t vz = attrib.vertices[3*size_t(idx.vertex_index)+2];
  51. output.POSITION.push_back(vx);
  52. output.POSITION.push_back(vy);
  53. output.POSITION.push_back(vz);
  54. (void)vx;
  55. (void)vy;
  56. (void)vz;
  57. // Check if `normal_index` is zero or positive. negative = no normal data
  58. if (idx.normal_index >= 0) {
  59. tinyobj::real_t nx = attrib.normals[3*size_t(idx.normal_index)+0];
  60. tinyobj::real_t ny = attrib.normals[3*size_t(idx.normal_index)+1];
  61. tinyobj::real_t nz = attrib.normals[3*size_t(idx.normal_index)+2];
  62. output.NORMAL.push_back(nx);
  63. output.NORMAL.push_back(ny);
  64. output.NORMAL.push_back(nz);
  65. }
  66. // Check if `texcoord_index` is zero or positive. negative = no texcoord data
  67. if (idx.texcoord_index >= 0) {
  68. tinyobj::real_t tx = attrib.texcoords[2*size_t(idx.texcoord_index)+0];
  69. tinyobj::real_t ty = attrib.texcoords[2*size_t(idx.texcoord_index)+1];
  70. output.TEXCOORD_0.push_back(tx);
  71. output.TEXCOORD_0.push_back(ty);
  72. }
  73. // Optional: vertex colors
  74. // tinyobj::real_t red = attrib.colors[3*size_t(idx.vertex_index)+0];
  75. // tinyobj::real_t green = attrib.colors[3*size_t(idx.vertex_index)+1];
  76. // tinyobj::real_t blue = attrib.colors[3*size_t(idx.vertex_index)+2];
  77. }
  78. index_offset += fv;
  79. output.INDEX.push_back(_index++);
  80. output.INDEX.push_back(_index++);
  81. output.INDEX.push_back(_index++);
  82. // per-face material
  83. //_shape.mesh.material_ids[f];
  84. }
  85. }
  86. return output;
  87. }
  88. }
  89. #endif