ObjFile.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.pos = vertices.get(index.x - 1);
  96. current.tPos = textureCoordinates.get(index.y - 1);
  97. current.normal = normals.get(index.z - 1);
  98. current.knochenId = 0;
  99. int ind = 0;
  100. for (const Vertex3D& other : modelData)
  101. {
  102. if (other.pos == current.pos && other.tPos == current.tPos && other.normal == current.normal)
  103. break;
  104. ind++;
  105. }
  106. if (modelData.getEintragAnzahl() == ind)
  107. modelData.add(current);
  108. indexList[rInd++] = ind;
  109. }
  110. Vertex3D* vertexData = new Vertex3D[modelData.getEintragAnzahl()];
  111. int ind = 0;
  112. for (const Vertex3D& vertex : modelData)
  113. vertexData[ind++] = vertex;
  114. zTarget->setVertecies(vertexData, modelData.getEintragAnzahl());
  115. Polygon3D* polygon = new Polygon3D();
  116. polygon->indexAnz = indices.getEintragAnzahl();
  117. polygon->indexList = indexList;
  118. zTarget->addPolygon(polygon);
  119. }
  120. bool ObjFile::loadModel(const char* name, Model3DData* zTarget)
  121. {
  122. file.open(Datei::Style::lesen);
  123. Text* line = file.leseZeile();
  124. while (line)
  125. {
  126. if (line->positionVon("o ") == 0 && line->positionVon(name) == 2 && line->getLength() == textLength(name) + 3)
  127. {
  128. readModel(zTarget);
  129. line->release();
  130. file.close();
  131. return 1;
  132. }
  133. line->release();
  134. line = file.leseZeile();
  135. }
  136. file.close();
  137. return 0;
  138. }
  139. const RCArray<Text>& ObjFile::getContainedModelNames() const
  140. {
  141. return objectNames;
  142. }