main.cpp 3.4 KB

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