Guest User

Untitled

a guest
Oct 20th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. #include "OBJ.h"
  2.  
  3. namespace roxlu {
  4.  
  5. bool OBJ::import(const string& filepath) {
  6. ifstream ifs;
  7. ifs.open(filepath.c_str());
  8. if(!ifs.is_open()) {
  9. printf("Cannot open: %s\n", filepath.c_str());
  10. return false;
  11. }
  12.  
  13. Triangle tri;
  14. Vec3 v;
  15. OBJ::Object object;
  16. string line;
  17. bool object_added = false;
  18. bool found_object = false;
  19. while(getline(ifs, line)) {
  20. if(line.at(0) == '#') {
  21. continue;
  22. }
  23.  
  24. int space = line.find(" ");
  25. if(space == string::npos) {
  26. continue;
  27. }
  28.  
  29. string cmd = line.substr(0, space);
  30.  
  31. // object
  32. if(cmd == "o") {
  33. if(found_object) {
  34. object_added = true;
  35. objects[object.name] = object;
  36. }
  37. found_object = true;
  38. object.name = line.substr(space+1, line.size()-1);
  39. }
  40. // vertex
  41. else if(found_object && cmd == "v") {
  42. stringstream ss;
  43. ss << line.substr(space+1, line.size()-1);
  44. ss >> v.x >> v.y >> v.z;
  45. object.vertices.addVertex(v);
  46. }
  47. // normal
  48. else if(found_object && cmd == "vn") {
  49. stringstream ss;
  50. ss << line.substr(space+1, line.size()-1);
  51. ss >> v.x >> v.y >> v.z;
  52. object.vertices.addNormal(v);
  53. }
  54. // face
  55. else if(found_object && cmd == "f") {
  56. stringstream ss;
  57. ss << line.substr(space+1, line.size()-1);
  58. string face;
  59.  
  60. ss >> face;
  61. extractFace(face, tri.va, tri.na);
  62.  
  63.  
  64. ss >> face;
  65. extractFace(face, tri.vb, tri.nb);
  66.  
  67. ss >> face;
  68. extractFace(face, tri.vc, tri.nc);
  69.  
  70. object.vertices.addTriangle(tri);
  71. Vec3 vv = object.vertices.vertices[tri.va];
  72.  
  73. }
  74. }
  75. if(!object_added) {
  76. objects[object.name] = object;
  77. }
  78. }
  79.  
  80. bool OBJ::extractFace(string info, int& vertexIndex, int& normalIndex) {
  81. stringstream fss;
  82. fss << info;
  83. string fv;
  84.  
  85. // get vertex index
  86. if(getline(fss, fv, '/')) {
  87. if(fv.size()) {
  88. stringstream ofss;
  89. ofss << fv;
  90. ofss >> vertexIndex;
  91. --vertexIndex;
  92. }
  93. }
  94.  
  95. // get texcoord index
  96. if(getline(fss, fv, '/')) {
  97. if(fv.size()) {
  98. // ofss << fv;
  99. // ofss >> vertexIndex;
  100. }
  101. }
  102.  
  103. // get normal index
  104. if(getline(fss, fv, '/')) {
  105. if(fv.size()) {
  106. stringstream ofss;
  107. ofss << fv;
  108. ofss >> normalIndex;
  109. --normalIndex;
  110. }
  111. }
  112. }
  113.  
  114. };
Add Comment
Please, Sign In to add comment