ObjFile.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include "ObjFile.h"
  2. #include "Vec2.h"
  3. #include "Model3D.h"
  4. using namespace Framework;
  5. ObjFile::ObjFile(const char* path)
  6. : ReferenceCounter()
  7. {
  8. file.setDatei(path);
  9. file.open(Datei::Style::lesen);
  10. Text* line = file.leseZeile();
  11. while (line)
  12. {
  13. if (line->positionVon("o ") == 0)
  14. objectNames.add(line->getTeilText(2));
  15. line->release();
  16. line = file.leseZeile();
  17. }
  18. file.close();
  19. }
  20. Vec3<int> ObjFile::parseIndex(Text* s)
  21. {
  22. Text* x = s->getTeilText(0, s->positionVon("/"));
  23. Text* y = s->getTeilText(s->positionVon("/") + 1, s->positionVon("/", 1));
  24. Text* z = s->getTeilText(s->positionVon("/", 1) + 1);
  25. Vec3<int> result((int)*x, (int)*y, (int)*z);
  26. x->release();
  27. y->release();
  28. z->release();
  29. s->release();
  30. return result;
  31. }
  32. void ObjFile::readModel(Model3DData* zTarget)
  33. {
  34. Array<Vec3<float>> vertices;
  35. Array<Vec2<float>> textureCoordinates;
  36. Array<Vec3<float>> normals;
  37. Array<Vec3<int>> indices;
  38. // read model info
  39. Text* line = file.leseZeile();
  40. while (line)
  41. {
  42. if (line->positionVon("o ") == 0)
  43. {
  44. line->release();
  45. break;;
  46. }
  47. if (line->positionVon("v ") == 0)
  48. {
  49. Text* x = line->getTeilText(2, line->positionVon(" ", 1));
  50. Text* y = line->getTeilText(line->positionVon(" ", 1) + 1, line->positionVon(" ", 2));
  51. Text* z = line->getTeilText(line->positionVon(" ", 2) + 1);
  52. vertices.add(Vec3<float>((float)*x, (float)*y, (float)*z));
  53. x->release();
  54. y->release();
  55. z->release();
  56. }
  57. if (line->positionVon("vt ") == 0)
  58. {
  59. Text* x = line->getTeilText(3, line->positionVon(" ", 1));
  60. Text* y = line->getTeilText(line->positionVon(" ", 1) + 1);
  61. textureCoordinates.add(Vec2<float>((float)*x, (float)*y));
  62. x->release();
  63. y->release();
  64. }
  65. if (line->positionVon("vn ") == 0)
  66. {
  67. Text* x = line->getTeilText(3, line->positionVon(" ", 1));
  68. Text* y = line->getTeilText(line->positionVon(" ", 1) + 1, line->positionVon(" ", 2));
  69. Text* z = line->getTeilText(line->positionVon(" ", 2) + 1);
  70. normals.add(Vec3<float>((float)*x, (float)*y, (float)*z));
  71. x->release();
  72. y->release();
  73. z->release();
  74. }
  75. if (line->positionVon("f ") == 0)
  76. {
  77. // only triangulated meshes ares supported
  78. Text* p1 = line->getTeilText(2, line->positionVon(" ", 1));
  79. Text* p2 = line->getTeilText(line->positionVon(" ", 1) + 1, line->positionVon(" ", 2));
  80. Text* p3 = line->getTeilText(line->positionVon(" ", 2) + 1);
  81. indices.add(parseIndex(p1));
  82. indices.add(parseIndex(p2));
  83. indices.add(parseIndex(p3));
  84. }
  85. line->release();
  86. line = file.leseZeile();
  87. }
  88. // compute vertex data and index buffer
  89. Array<Vertex3D> modelData;
  90. int* indexList = new int[indices.getEintragAnzahl()];
  91. int rInd = 0;
  92. for (Vec3<int> index : indices)
  93. {
  94. Vertex3D current;
  95. current.id = 0;
  96. current.pos = vertices.get(index.x - 1);
  97. current.tPos = textureCoordinates.get(index.y - 1);
  98. current.normal = normals.get(index.z - 1);
  99. current.knochenId = 0;
  100. int ind = 0;
  101. for (const Vertex3D& other : modelData)
  102. {
  103. if (other.pos == current.pos && other.tPos == current.tPos && other.normal == current.normal)
  104. break;
  105. ind++;
  106. }
  107. if (modelData.getEintragAnzahl() == ind)
  108. modelData.add(current);
  109. indexList[rInd++] = ind;
  110. }
  111. Vertex3D* vertexData = new Vertex3D[modelData.getEintragAnzahl()];
  112. int ind = 0;
  113. for (const Vertex3D& vertex : modelData)
  114. vertexData[ind++] = vertex;
  115. zTarget->setVertecies(vertexData, modelData.getEintragAnzahl());
  116. Polygon3D* polygon = new Polygon3D();
  117. polygon->indexAnz = indices.getEintragAnzahl();
  118. polygon->indexList = indexList;
  119. zTarget->addPolygon(polygon);
  120. }
  121. bool ObjFile::loadModel(const char* name, Model3DData* zTarget)
  122. {
  123. file.open(Datei::Style::lesen);
  124. Text* line = file.leseZeile();
  125. while (line)
  126. {
  127. if (line->positionVon("o ") == 0 && line->positionVon(name) == 2 && line->getLength() == textLength(name) + 3)
  128. {
  129. readModel(zTarget);
  130. line->release();
  131. file.close();
  132. return 1;
  133. }
  134. line->release();
  135. line = file.leseZeile();
  136. }
  137. file.close();
  138. return 0;
  139. }
  140. const RCArray<Text>& ObjFile::getContainedModelNames() const
  141. {
  142. return objectNames;
  143. }