DKGL2 sample codes

ObjImportUtil.cpp 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "ObjImportUtil.h"
  2. #include "tiny_obj_loader.h"
  3. int ObjImportUtil::ObjVertex::Compare(const ObjVertex& Other) const
  4. {
  5. const float pp[] = { this->inPos.x, this->inPos.y, this->inPos.z,
  6. this->inColor.x, this->inColor.y, this->inColor.z,
  7. this->inTexCoord.x, this->inTexCoord.y,
  8. this->inNormal.x, this->inNormal.y, this->inNormal.z };
  9. const float rr[] = { Other.inPos.x, Other.inPos.y, Other.inPos.z,
  10. Other.inColor.x, Other.inColor.y, Other.inColor.z,
  11. Other.inTexCoord.x, Other.inTexCoord.y,
  12. Other.inNormal.x, Other.inNormal.y, Other.inNormal.z };
  13. for (int i = 0; i < std::size(pp); ++i)
  14. {
  15. auto k = pp[i] - rr[i];
  16. if (abs(k) < 0.0001)
  17. continue;
  18. if (k > 0)
  19. return 1;
  20. else if (k < 0)
  21. return -1;
  22. }
  23. return 0;
  24. }
  25. ObjImportUtil::ObjImportedMeshData ObjImportUtil::LoadFromObjFile(
  26. const DKString& inPath
  27. , DKResourcePool& inDKResourcePool)
  28. {
  29. ObjImportedMeshData retData;
  30. DKObject<DKData> objData = inDKResourcePool.LoadResourceData(inPath);
  31. if (objData && objData->Length() > 0)
  32. {
  33. DKLog("Loading %ls... (%lu bytes)\n"
  34. , (const wchar_t*)inPath
  35. , objData->Length());
  36. DKTimer timer;
  37. timer.Reset();
  38. retData.vertices.Reserve(100000);
  39. retData.indices.Reserve(100000);
  40. retData.vertexSize = sizeof(ObjImportUtil::ObjVertex);
  41. tinyobj::attrib_t attrib;
  42. std::vector<tinyobj::shape_t> shapes;
  43. shapes.reserve(1000000);
  44. std::vector<tinyobj::material_t> materials;
  45. std::string err;
  46. if (!tinyobj::LoadObj(&attrib, &shapes, &materials, &err, DKStringU8(inPath))) {
  47. throw std::runtime_error(err);
  48. }
  49. DKMap<ObjVertex, uint32_t> uniqueVertices;
  50. DKLog("Save to Container");
  51. for (const auto& shape : shapes)
  52. {
  53. for (const auto& index : shape.mesh.indices)
  54. {
  55. ObjVertex vertex = {};
  56. vertex.inPos = {
  57. attrib.vertices[3 * index.vertex_index + 0],
  58. attrib.vertices[3 * index.vertex_index + 1],
  59. attrib.vertices[3 * index.vertex_index + 2]
  60. };
  61. if (attrib.texcoords.size())
  62. {
  63. vertex.inTexCoord = {
  64. attrib.texcoords[2 * index.texcoord_index + 0],
  65. 1.0f - attrib.texcoords[2 * index.texcoord_index + 1]
  66. };
  67. }
  68. vertex.inColor = { 1.0f, 1.0f, 1.0f };
  69. if (attrib.normals.size())
  70. {
  71. vertex.inNormal = {
  72. attrib.normals[3 * index.normal_index + 0],
  73. attrib.normals[3 * index.normal_index + 1],
  74. attrib.normals[3 * index.normal_index + 2]
  75. };
  76. }
  77. if (uniqueVertices.Find(vertex) == nullptr)
  78. {
  79. uniqueVertices.Insert(vertex
  80. , static_cast<uint32_t>(retData.vertices.Count()));
  81. retData.vertices.Add(vertex);
  82. }
  83. retData.indices.Add(uniqueVertices.Value(vertex));
  84. retData.aabb.Expand(vertex.inPos);
  85. }
  86. }
  87. }
  88. return retData;
  89. }