ModelInfo.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. bool loades = 0;
  46. if (path.positionVon(".m3/") > 0)
  47. {
  48. int pos = path.positionVon(".m3/", path.anzahlVon(".m3/") - 1) + 3;
  49. Framework::M3Datei m3File(path.getTeilText(0, pos));
  50. m3File.leseDaten();
  51. Model3DData* data
  52. = m3File.ladeModel(path.getTeilText(pos + 1), 0, new Text());
  53. if (data)
  54. {
  55. auto tmp = data->getMaxPos();
  56. boundingBox.x = abs(tmp.x);
  57. boundingBox.y = abs(tmp.y);
  58. boundingBox.z = abs(tmp.z);
  59. tmp = data->getMinPos();
  60. boundingBox.x = max(boundingBox.x, abs(tmp.x));
  61. boundingBox.y = max(boundingBox.y, abs(tmp.y));
  62. boundingBox.z = max(boundingBox.z, abs(tmp.z));
  63. data->release();
  64. loades = 1;
  65. }
  66. }
  67. if (!loades)
  68. {
  69. boundingBox.x = 0.5f;
  70. boundingBox.y = 0.5f;
  71. boundingBox.z = 0.5f;
  72. }
  73. }
  74. void ModelInfo::addTexturePath(Framework::Text path)
  75. {
  76. texturePaths.add(new Framework::Text(path));
  77. }
  78. void ModelInfo::setTransparent(bool transparent)
  79. {
  80. this->transparent = transparent;
  81. }
  82. void ModelInfo::setSize(float size)
  83. {
  84. this->size = size;
  85. }
  86. Framework::Text ModelInfo::getModelPath() const
  87. {
  88. return modelPath;
  89. }
  90. Framework::RCArray<Framework::Text> ModelInfo::getTexturePaths() const
  91. {
  92. return texturePaths;
  93. }
  94. bool ModelInfo::isTransparent() const
  95. {
  96. return transparent;
  97. }
  98. float ModelInfo::getSize() const
  99. {
  100. return size;
  101. }
  102. Framework::Vec3<float> ModelInfo::getBoundingBox() const
  103. {
  104. return boundingBox;
  105. }
  106. ModelInfoFactory::ModelInfoFactory()
  107. : ObjectTypeFactory()
  108. {}
  109. ModelInfo* ModelInfoFactory::fromJson(Framework::JSON::JSONObject* zJson) const
  110. {
  111. ModelInfo* result = new ModelInfo();
  112. result->setModelPath(
  113. zJson->asObject()->zValue("modelPath")->asString()->getString());
  114. for (Framework::JSON::JSONValue* v :
  115. *zJson->asObject()->zValue("texturePaths")->asArray())
  116. {
  117. result->addTexturePath(v->asString()->getString());
  118. }
  119. result->setTransparent(
  120. zJson->asObject()->zValue("transparent")->asBool()->getBool());
  121. result->setSize(
  122. (float)zJson->asObject()->zValue("size")->asNumber()->getNumber());
  123. if (zJson->asObject()->hasValue("boundingBox"))
  124. {
  125. Framework::JSON::JSONArray* bbArr
  126. = zJson->asObject()->zValue("boundingBox")->asArray();
  127. result->boundingBox.x
  128. = (float)bbArr->getValue(0)->asNumber()->getNumber();
  129. result->boundingBox.y
  130. = (float)bbArr->getValue(1)->asNumber()->getNumber();
  131. result->boundingBox.z
  132. = (float)bbArr->getValue(2)->asNumber()->getNumber();
  133. }
  134. return result;
  135. }
  136. Framework::JSON::JSONObject* ModelInfoFactory::toJsonObject(
  137. ModelInfo* zObject) const
  138. {
  139. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  140. Framework::JSON::JSONArray* arr = new Framework::JSON::JSONArray();
  141. for (Framework::Text* t : zObject->texturePaths)
  142. {
  143. arr->addValue(new Framework::JSON::JSONString(t->getText()));
  144. }
  145. result->addValue("texturePaths", arr);
  146. result->addValue(
  147. "modelPath", new Framework::JSON::JSONString(zObject->modelPath));
  148. result->addValue(
  149. "transparent", new Framework::JSON::JSONBool(zObject->transparent));
  150. result->addValue("size", new Framework::JSON::JSONNumber(zObject->size));
  151. Framework::JSON::JSONArray* bbArr = new Framework::JSON::JSONArray();
  152. bbArr->addValue(new Framework::JSON::JSONNumber(zObject->boundingBox.x));
  153. bbArr->addValue(new Framework::JSON::JSONNumber(zObject->boundingBox.y));
  154. bbArr->addValue(new Framework::JSON::JSONNumber(zObject->boundingBox.z));
  155. result->addValue("boundingBox", bbArr);
  156. return result;
  157. }
  158. JSONObjectValidationBuilder* ModelInfoFactory::addToValidator(
  159. JSONObjectValidationBuilder* builder) const
  160. {
  161. return builder->withRequiredArray("texturePaths")
  162. ->addAcceptedStringInArray()
  163. ->finishString()
  164. ->finishArray()
  165. ->withRequiredString("modelPath")
  166. ->finishString()
  167. ->withRequiredBool("transparent")
  168. ->withDefault(false)
  169. ->finishBool()
  170. ->withRequiredNumber("size")
  171. ->whichIsGreaterThen(0)
  172. ->withDefault(1.0)
  173. ->finishNumber()
  174. ->withRequiredArray("boundingBox")
  175. ->whichIsOptional()
  176. ->withRequiredSize(3)
  177. ->addAcceptedNumberInArray()
  178. ->finishNumber()
  179. ->finishArray();
  180. }