main.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "tiny_obj_loader.h"
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cassert>
  5. #include <iostream>
  6. static bool
  7. TestLoadObj(
  8. const char* fileName,
  9. bool verbose)
  10. {
  11. const char* prefix[] = {"./data/", "../data/", "../../data/", "../../../data/", "../../../../data/"};
  12. char fullPath[1024];
  13. int index = -1;
  14. {
  15. int numPrefixes = sizeof(prefix) / sizeof(char*);
  16. for (int i = 0; i < numPrefixes; i++)
  17. {
  18. sprintf(fullPath, "%s%s", prefix[i], fileName);
  19. FILE* f;
  20. f = fopen(fullPath, "r");
  21. if (f)
  22. {
  23. index = i;
  24. fclose(f);
  25. break;
  26. }
  27. }
  28. }
  29. if (index < 0)
  30. {
  31. printf("file not found %s\n", fileName);
  32. return false;
  33. }
  34. std::cout << "Loading " << fullPath << std::endl;
  35. std::vector<bt_tinyobj::shape_t> shapes;
  36. std::string err = bt_tinyobj::LoadObj(shapes, fullPath, prefix[index]);
  37. if (!err.empty())
  38. {
  39. std::cerr << err << std::endl;
  40. return false;
  41. }
  42. std::cout << "# of shapes : " << shapes.size() << std::endl;
  43. if (verbose)
  44. {
  45. for (size_t i = 0; i < shapes.size(); i++)
  46. {
  47. printf("shape[%ld].name = %s\n", i, shapes[i].name.c_str());
  48. printf("shape[%ld].indices: %ld\n", i, shapes[i].mesh.indices.size());
  49. assert((shapes[i].mesh.indices.size() % 3) == 0);
  50. for (size_t f = 0; f < shapes[i].mesh.indices.size(); f++)
  51. {
  52. printf(" idx[%ld] = %d\n", f, shapes[i].mesh.indices[f]);
  53. }
  54. printf("shape[%ld].vertices: %ld\n", i, shapes[i].mesh.positions.size());
  55. assert((shapes[i].mesh.positions.size() % 3) == 0);
  56. for (size_t v = 0; v < shapes[i].mesh.positions.size() / 3; v++)
  57. {
  58. printf(" v[%ld] = (%f, %f, %f)\n", v,
  59. shapes[i].mesh.positions[3 * v + 0],
  60. shapes[i].mesh.positions[3 * v + 1],
  61. shapes[i].mesh.positions[3 * v + 2]);
  62. }
  63. printf("shape[%ld].material.name = %s\n", i, shapes[i].material.name.c_str());
  64. printf(" material.Ka = (%f, %f ,%f)\n", shapes[i].material.ambient[0], shapes[i].material.ambient[1], shapes[i].material.ambient[2]);
  65. printf(" material.Kd = (%f, %f ,%f)\n", shapes[i].material.diffuse[0], shapes[i].material.diffuse[1], shapes[i].material.diffuse[2]);
  66. printf(" material.Ks = (%f, %f ,%f)\n", shapes[i].material.specular[0], shapes[i].material.specular[1], shapes[i].material.specular[2]);
  67. printf(" material.Tr = (%f, %f ,%f)\n", shapes[i].material.transmittance[0], shapes[i].material.transmittance[1], shapes[i].material.transmittance[2]);
  68. printf(" material.Ke = (%f, %f ,%f)\n", shapes[i].material.emission[0], shapes[i].material.emission[1], shapes[i].material.emission[2]);
  69. printf(" material.Ns = %f\n", shapes[i].material.shininess);
  70. printf(" material.map_Ka = %s\n", shapes[i].material.ambient_texname.c_str());
  71. printf(" material.map_Kd = %s\n", shapes[i].material.diffuse_texname.c_str());
  72. printf(" material.map_Ks = %s\n", shapes[i].material.specular_texname.c_str());
  73. printf(" material.map_Ns = %s\n", shapes[i].material.normal_texname.c_str());
  74. std::map<std::string, std::string>::iterator it(shapes[i].material.unknown_parameter.begin());
  75. std::map<std::string, std::string>::iterator itEnd(shapes[i].material.unknown_parameter.end());
  76. for (; it != itEnd; it++)
  77. {
  78. printf(" material.%s = %s\n", it->first.c_str(), it->second.c_str());
  79. }
  80. printf("\n");
  81. }
  82. }
  83. return true;
  84. }
  85. int main(int argc, char** argv)
  86. {
  87. // assert(true == TestLoadObj("cornell_box.obj",true));
  88. // assert(true == TestLoadObj("cube.obj",true));
  89. assert(true == TestLoadObj("samurai_monastry.obj", false));
  90. assert(true == TestLoadObj("teddy2_VHACD_CHs.obj", true));
  91. return 0;
  92. }