ModelInfo.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include "ModelInfo.h"
  2. #include <M3Datei.h>
  3. #include <Model3D.h>
  4. using namespace Framework;
  5. ModelInfo::ModelInfo()
  6. : ReferenceCounter(),
  7. modelPath(),
  8. texturePaths(),
  9. transparent(false),
  10. size(1.0f)
  11. {}
  12. ModelInfo::ModelInfo(const char* modelPath,
  13. Framework::RCArray<Framework::Text> texturePaths,
  14. bool transparent,
  15. float size)
  16. : ReferenceCounter()
  17. {
  18. setModelPath(modelPath);
  19. for (Framework::Text* t : texturePaths)
  20. {
  21. addTexturePath(*t);
  22. }
  23. setTransparent(transparent);
  24. setSize(size);
  25. }
  26. void ModelInfo::writeTo(Framework::StreamWriter* zWriter) const
  27. {
  28. char len = (char)modelPath.getLength();
  29. zWriter->schreibe(&len, 1);
  30. zWriter->schreibe(modelPath.getText(), (int)len);
  31. short count = (short)texturePaths.getEintragAnzahl();
  32. zWriter->schreibe((char*)&count, 2);
  33. for (Text* t : texturePaths)
  34. {
  35. len = (char)t->getLength();
  36. zWriter->schreibe(&len, 1);
  37. zWriter->schreibe(t->getText(), (int)len);
  38. }
  39. zWriter->schreibe((char*)&transparent, 1);
  40. zWriter->schreibe((char*)&size, 4);
  41. }
  42. void ModelInfo::setModelPath(Framework::Text path)
  43. {
  44. modelPath = path;
  45. }
  46. void ModelInfo::addTexturePath(Framework::Text path)
  47. {
  48. texturePaths.add(new Framework::Text(path));
  49. }
  50. void ModelInfo::setTransparent(bool transparent)
  51. {
  52. this->transparent = transparent;
  53. }
  54. void ModelInfo::setSize(float size)
  55. {
  56. this->size = size;
  57. }
  58. Framework::Text ModelInfo::getModelPath() const
  59. {
  60. return modelPath;
  61. }
  62. Framework::RCArray<Framework::Text> ModelInfo::getTexturePaths() const
  63. {
  64. return texturePaths;
  65. }
  66. bool ModelInfo::isTransparent() const
  67. {
  68. return transparent;
  69. }
  70. float ModelInfo::getSize() const
  71. {
  72. return size;
  73. }
  74. Framework::Vec3<float> ModelInfo::getBoundingBox() const
  75. {
  76. return boundingBox;
  77. }
  78. ModelInfoFactory::ModelInfoFactory()
  79. : ObjectTypeFactory()
  80. {}
  81. ModelInfo* ModelInfoFactory::fromJson(Framework::JSON::JSONObject* zJson) const
  82. {
  83. ModelInfo* result = new ModelInfo();
  84. result->setModelPath(
  85. zJson->asObject()->zValue("modelPath")->asString()->getString());
  86. for (Framework::JSON::JSONValue* v :
  87. *zJson->asObject()->zValue("texturePaths")->asArray())
  88. {
  89. result->addTexturePath(v->asString()->getString());
  90. }
  91. result->setTransparent(
  92. zJson->asObject()->zValue("transparent")->asBool()->getBool());
  93. result->setSize(
  94. (float)zJson->asObject()->zValue("size")->asNumber()->getNumber());
  95. if (zJson->asObject()->hasValue("boundingBox"))
  96. {
  97. Framework::JSON::JSONArray* bbArr
  98. = zJson->asObject()->zValue("boundingBox")->asArray();
  99. result->boundingBox.x
  100. = (float)bbArr->getValue(0)->asNumber()->getNumber();
  101. result->boundingBox.y
  102. = (float)bbArr->getValue(1)->asNumber()->getNumber();
  103. result->boundingBox.z
  104. = (float)bbArr->getValue(2)->asNumber()->getNumber();
  105. }
  106. else
  107. {
  108. Text path = "data/models/";
  109. path += result->getModelPath();
  110. bool loades = 0;
  111. if (path.positionVon(".m3/") > 0)
  112. {
  113. int pos = path.positionVon(".m3/", path.anzahlVon(".m3/") - 1) + 3;
  114. Framework::M3Datei m3File(path.getTeilText(0, pos));
  115. m3File.leseDaten();
  116. Model3DData* data
  117. = m3File.ladeModel(path.getTeilText(pos + 1), 0, new Text());
  118. if (data)
  119. {
  120. auto tmp = data->getMaxPos();
  121. result->boundingBox.x = abs(tmp.x);
  122. result->boundingBox.y = abs(tmp.y);
  123. result->boundingBox.z = abs(tmp.z);
  124. tmp = data->getMinPos();
  125. result->boundingBox.x = max(result->boundingBox.x, abs(tmp.x));
  126. result->boundingBox.y = max(result->boundingBox.y, abs(tmp.y));
  127. result->boundingBox.z = max(result->boundingBox.z, abs(tmp.z));
  128. data->release();
  129. loades = 1;
  130. }
  131. }
  132. if (!loades)
  133. {
  134. result->boundingBox.x = 0.5f;
  135. result->boundingBox.y = 0.5f;
  136. result->boundingBox.z = 0.5f;
  137. }
  138. }
  139. return result;
  140. }
  141. Framework::JSON::JSONObject* ModelInfoFactory::toJsonObject(
  142. ModelInfo* zObject) const
  143. {
  144. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  145. Framework::JSON::JSONArray* arr = new Framework::JSON::JSONArray();
  146. for (Framework::Text* t : zObject->texturePaths)
  147. {
  148. arr->addValue(new Framework::JSON::JSONString(t->getText()));
  149. }
  150. result->addValue("texturePaths", arr);
  151. result->addValue(
  152. "modelPath", new Framework::JSON::JSONString(zObject->modelPath));
  153. result->addValue(
  154. "transparent", new Framework::JSON::JSONBool(zObject->transparent));
  155. result->addValue("size", new Framework::JSON::JSONNumber(zObject->size));
  156. Framework::JSON::JSONArray* bbArr = new Framework::JSON::JSONArray();
  157. bbArr->addValue(new Framework::JSON::JSONNumber(zObject->boundingBox.x));
  158. bbArr->addValue(new Framework::JSON::JSONNumber(zObject->boundingBox.y));
  159. bbArr->addValue(new Framework::JSON::JSONNumber(zObject->boundingBox.z));
  160. result->addValue("boundingBox", bbArr);
  161. return result;
  162. }
  163. JSONObjectValidationBuilder* ModelInfoFactory::addToValidator(
  164. JSONObjectValidationBuilder* builder) const
  165. {
  166. return builder->withRequiredArray("texturePaths")
  167. ->addAcceptedStringInArray()
  168. ->finishString()
  169. ->finishArray()
  170. ->withRequiredString("modelPath")
  171. ->finishString()
  172. ->withRequiredBool("transparent")
  173. ->withDefault(false)
  174. ->finishBool()
  175. ->withRequiredNumber("size")
  176. ->whichIsGreaterThen(0)
  177. ->withDefault(1.0)
  178. ->finishNumber()
  179. ->withRequiredArray("boundingBox")
  180. ->whichIsOptional()
  181. ->withRequiredSize(3)
  182. ->addAcceptedNumberInArray()
  183. ->finishNumber()
  184. ->finishArray();
  185. }