| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- #include "ModelInfo.h"
- #include <M3Datei.h>
- #include <Model3D.h>
- using namespace Framework;
- ModelInfo::ModelInfo()
- : ReferenceCounter(),
- modelPath(),
- texturePaths(),
- transparent(false),
- size(1.0f)
- {}
- ModelInfo::ModelInfo(const char* modelPath,
- Framework::RCArray<Framework::Text> texturePaths,
- bool transparent,
- float size)
- : ReferenceCounter()
- {
- setModelPath(modelPath);
- for (Framework::Text* t : texturePaths)
- {
- addTexturePath(*t);
- }
- setTransparent(transparent);
- setSize(size);
- }
- void ModelInfo::writeTo(Framework::StreamWriter* zWriter) const
- {
- char len = (char)modelPath.getLength();
- zWriter->schreibe(&len, 1);
- zWriter->schreibe(modelPath.getText(), (int)len);
- short count = (short)texturePaths.getEintragAnzahl();
- zWriter->schreibe((char*)&count, 2);
- for (Text* t : texturePaths)
- {
- len = (char)t->getLength();
- zWriter->schreibe(&len, 1);
- zWriter->schreibe(t->getText(), (int)len);
- }
- zWriter->schreibe((char*)&transparent, 1);
- zWriter->schreibe((char*)&size, 4);
- }
- void ModelInfo::setModelPath(Framework::Text path)
- {
- modelPath = path;
- bool loades = 0;
- if (path.positionVon(".m3/") > 0)
- {
- int pos = path.positionVon(".m3/", path.anzahlVon(".m3/") - 1) + 3;
- Framework::M3Datei m3File(path.getTeilText(0, pos));
- m3File.leseDaten();
- Model3DData* data
- = m3File.ladeModel(path.getTeilText(pos + 1), 0, new Text());
- if (data)
- {
- auto tmp = data->getMaxPos();
- boundingBox.x = abs(tmp.x);
- boundingBox.y = abs(tmp.y);
- boundingBox.z = abs(tmp.z);
- tmp = data->getMinPos();
- boundingBox.x = max(boundingBox.x, abs(tmp.x));
- boundingBox.y = max(boundingBox.y, abs(tmp.y));
- boundingBox.z = max(boundingBox.z, abs(tmp.z));
- data->release();
- loades = 1;
- }
- }
- if (!loades)
- {
- boundingBox.x = 0.5f;
- boundingBox.y = 0.5f;
- boundingBox.z = 0.5f;
- }
- }
- void ModelInfo::addTexturePath(Framework::Text path)
- {
- texturePaths.add(new Framework::Text(path));
- }
- void ModelInfo::setTransparent(bool transparent)
- {
- this->transparent = transparent;
- }
- void ModelInfo::setSize(float size)
- {
- this->size = size;
- }
- Framework::Text ModelInfo::getModelPath() const
- {
- return modelPath;
- }
- Framework::RCArray<Framework::Text> ModelInfo::getTexturePaths() const
- {
- return texturePaths;
- }
- bool ModelInfo::isTransparent() const
- {
- return transparent;
- }
- float ModelInfo::getSize() const
- {
- return size;
- }
- Framework::Vec3<float> ModelInfo::getBoundingBox() const
- {
- return boundingBox;
- }
- ModelInfoFactory::ModelInfoFactory()
- : ObjectTypeFactory()
- {}
- ModelInfo* ModelInfoFactory::fromJson(Framework::JSON::JSONObject* zJson) const
- {
- ModelInfo* result = new ModelInfo();
- result->setModelPath(
- zJson->asObject()->zValue("modelPath")->asString()->getString());
- for (Framework::JSON::JSONValue* v :
- *zJson->asObject()->zValue("texturePaths")->asArray())
- {
- result->addTexturePath(v->asString()->getString());
- }
- result->setTransparent(
- zJson->asObject()->zValue("transparent")->asBool()->getBool());
- result->setSize(
- (float)zJson->asObject()->zValue("size")->asNumber()->getNumber());
- if (zJson->asObject()->hasValue("boundingBox"))
- {
- Framework::JSON::JSONArray* bbArr
- = zJson->asObject()->zValue("boundingBox")->asArray();
- result->boundingBox.x
- = (float)bbArr->getValue(0)->asNumber()->getNumber();
- result->boundingBox.y
- = (float)bbArr->getValue(1)->asNumber()->getNumber();
- result->boundingBox.z
- = (float)bbArr->getValue(2)->asNumber()->getNumber();
- }
- return result;
- }
- Framework::JSON::JSONObject* ModelInfoFactory::toJsonObject(
- ModelInfo* zObject) const
- {
- Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
- Framework::JSON::JSONArray* arr = new Framework::JSON::JSONArray();
- for (Framework::Text* t : zObject->texturePaths)
- {
- arr->addValue(new Framework::JSON::JSONString(t->getText()));
- }
- result->addValue("texturePaths", arr);
- result->addValue(
- "modelPath", new Framework::JSON::JSONString(zObject->modelPath));
- result->addValue(
- "transparent", new Framework::JSON::JSONBool(zObject->transparent));
- result->addValue("size", new Framework::JSON::JSONNumber(zObject->size));
- Framework::JSON::JSONArray* bbArr = new Framework::JSON::JSONArray();
- bbArr->addValue(new Framework::JSON::JSONNumber(zObject->boundingBox.x));
- bbArr->addValue(new Framework::JSON::JSONNumber(zObject->boundingBox.y));
- bbArr->addValue(new Framework::JSON::JSONNumber(zObject->boundingBox.z));
- result->addValue("boundingBox", bbArr);
- return result;
- }
- JSONObjectValidationBuilder* ModelInfoFactory::addToValidator(
- JSONObjectValidationBuilder* builder) const
- {
- return builder->withRequiredArray("texturePaths")
- ->addAcceptedStringInArray()
- ->finishString()
- ->finishArray()
- ->withRequiredString("modelPath")
- ->finishString()
- ->withRequiredBool("transparent")
- ->withDefault(false)
- ->finishBool()
- ->withRequiredNumber("size")
- ->whichIsGreaterThen(0)
- ->withDefault(1.0)
- ->finishNumber()
- ->withRequiredArray("boundingBox")
- ->whichIsOptional()
- ->withRequiredSize(3)
- ->addAcceptedNumberInArray()
- ->finishNumber()
- ->finishArray();
- }
|