TDME2 1.9.121
SceneWriter.cpp
Go to the documentation of this file.
2
3#include <iomanip>
4#include <iostream>
5#include <sstream>
6#include <string>
7
8#include <tdme/tdme.h>
21#include <tdme/math/Vector3.h>
22#include <tdme/math/Vector4.h>
25
26#include <ext/rapidjson/document.h>
27#include <ext/rapidjson/stringbuffer.h>
28#include <ext/rapidjson/writer.h>
29
30using std::ostringstream;
31using std::string;
32
50
51using rapidjson::Document;
52using rapidjson::StringBuffer;
53using rapidjson::Value;
54using rapidjson::Writer;
55
56void SceneWriter::write(const string& pathName, const string& fileName, Scene* scene)
57{
58 scene->setFileName((pathName.empty() == false?pathName + "/":"") + fileName);
59 auto sceneLibrary = scene->getLibrary();
60 Document jDocument;
61 jDocument.SetObject();
62 auto& jAllocator = jDocument.GetAllocator();
63 jDocument.AddMember("version", Value("1.99", jAllocator), jAllocator);
64 jDocument.AddMember("ro", Value(scene->getRotationOrder()->getName(), jAllocator), jAllocator);
65 Value jLights;
66 jLights.SetArray();
67 for (auto i = 0; i < scene->getLightCount(); i++) {
68 auto light = scene->getLightAt(i);
69 Value jLight;
70 jLight.SetObject();
71 jLight.AddMember("id", Value(i), jAllocator);
72 jLight.AddMember("ar", Value(light->getAmbient().getRed()), jAllocator);
73 jLight.AddMember("ag", Value(light->getAmbient().getGreen()), jAllocator);
74 jLight.AddMember("ab", Value(light->getAmbient().getBlue()), jAllocator);
75 jLight.AddMember("aa", Value(light->getAmbient().getAlpha()), jAllocator);
76 jLight.AddMember("dr", Value(light->getDiffuse().getRed()), jAllocator);
77 jLight.AddMember("dg", Value(light->getDiffuse().getGreen()), jAllocator);
78 jLight.AddMember("db", Value(light->getDiffuse().getBlue()), jAllocator);
79 jLight.AddMember("da", Value(light->getDiffuse().getAlpha()), jAllocator);
80 jLight.AddMember("sr", Value(light->getSpecular().getRed()), jAllocator);
81 jLight.AddMember("sg", Value(light->getSpecular().getGreen()), jAllocator);
82 jLight.AddMember("sb", Value(light->getSpecular().getBlue()), jAllocator);
83 jLight.AddMember("sa", Value(light->getSpecular().getAlpha()), jAllocator);
84 jLight.AddMember("px", Value(light->getPosition().getX()), jAllocator);
85 jLight.AddMember("py", Value(light->getPosition().getY()), jAllocator);
86 jLight.AddMember("pz", Value(light->getPosition().getZ()), jAllocator);
87 jLight.AddMember("pw", Value(light->getPosition().getW()), jAllocator);
88 jLight.AddMember("sdx", Value(light->getSpotDirection().getX()), jAllocator);
89 jLight.AddMember("sdy", Value(light->getSpotDirection().getY()), jAllocator);
90 jLight.AddMember("sdz", Value(light->getSpotDirection().getZ()), jAllocator);
91 jLight.AddMember("se", Value(light->getSpotExponent()), jAllocator);
92 jLight.AddMember("sco", Value(light->getSpotCutOff()), jAllocator);
93 jLight.AddMember("ca", Value(light->getConstantAttenuation()), jAllocator);
94 jLight.AddMember("la", Value(light->getLinearAttenuation()), jAllocator);
95 jLight.AddMember("qa", Value(light->getQuadraticAttenuation()), jAllocator);
96 jLight.AddMember("e", Value(light->isEnabled()), jAllocator);
97 jLights.PushBack(jLight, jAllocator);
98 }
99 jDocument.AddMember("lights", jLights, jAllocator);
100 Value jSceneLibrary;
101 jSceneLibrary.SetArray();
102 for (auto i = 0; i < sceneLibrary->getPrototypeCount(); i++) {
103 auto prototype = sceneLibrary->getPrototypeAt(i);
104 Value jPrototype;
105 jPrototype.SetObject();
106 if (prototype->isEmbedded() == true) {
107 Value jEmbeddedPrototype;
108 jEmbeddedPrototype.SetObject();
109 PrototypeWriter::write(
110 jDocument,
111 jEmbeddedPrototype,
112 prototype
113 );
114 jPrototype.AddMember("type", Value(prototype->getType()->getName(), jAllocator), jAllocator);
115 jPrototype.AddMember("entity", jEmbeddedPrototype, jAllocator);
116 }
117 jPrototype.AddMember("id", Value().SetInt(prototype->getId()), jAllocator);
118 jPrototype.AddMember("e", Value(prototype->isEmbedded()), jAllocator);
119 jPrototype.AddMember("pf", Value(prototype->getFileName(), jAllocator), jAllocator);
120 jSceneLibrary.PushBack(jPrototype, jAllocator);
121 }
122 jDocument.AddMember("models", jSceneLibrary, jAllocator);
123 Value jSceneProperties;
124 jSceneProperties.SetArray();
125 for (auto i = 0; i < scene->getPropertyCount(); i++) {
126 auto sceneProperty = scene->getPropertyByIndex(i);
127 Value jSceneProperty;
128 jSceneProperty.SetObject();
129 jSceneProperty.AddMember("name", Value(sceneProperty->getName(), jAllocator), jAllocator);
130 jSceneProperty.AddMember("value", Value(sceneProperty->getValue(), jAllocator), jAllocator);
131 jSceneProperties.PushBack(jSceneProperty, jAllocator);
132 }
133 jDocument.AddMember("properties", jSceneProperties, jAllocator);
134 Value jObjects;
135 jObjects.SetArray();
136 for (auto i = 0; i < scene->getEntityCount(); i++) {
137 auto sceneEntity = scene->getEntityAt(i);
138 Value jObject;
139 jObject.SetObject();
140 auto& transformations = sceneEntity->getTransformations();
141 auto& translation = transformations.getTranslation();
142 auto& scale = transformations.getScale();
143 auto& rotationAroundXAxis = transformations.getRotation(scene->getRotationOrder()->getAxisXIndex());
144 auto& rotationAroundYAxis = transformations.getRotation(scene->getRotationOrder()->getAxisYIndex());
145 auto& rotationAroundZAxis = transformations.getRotation(scene->getRotationOrder()->getAxisZIndex());
146 jObject.AddMember("id", Value(sceneEntity->getId(), jAllocator), jAllocator);
147 jObject.AddMember("descr", Value(sceneEntity->getDescription(), jAllocator), jAllocator);;
148 jObject.AddMember("mid", Value(sceneEntity->getPrototype()->getId()), jAllocator);
149 jObject.AddMember("tx", Value(translation.getX()), jAllocator);
150 jObject.AddMember("ty", Value(translation.getY()), jAllocator);
151 jObject.AddMember("tz", Value(translation.getZ()), jAllocator);
152 jObject.AddMember("sx", Value(scale.getX()), jAllocator);
153 jObject.AddMember("sy", Value(scale.getY()), jAllocator);
154 jObject.AddMember("sz", Value(scale.getZ()), jAllocator);
155 jObject.AddMember("rx", Value(rotationAroundXAxis.getAngle()), jAllocator);
156 jObject.AddMember("ry", Value(rotationAroundYAxis.getAngle()), jAllocator);
157 jObject.AddMember("rz", Value(rotationAroundZAxis.getAngle()), jAllocator);
158 jObject.AddMember("r", Value(sceneEntity->getReflectionEnvironmentMappingId(), jAllocator), jAllocator);
159 Value jEntityProperties;
160 jEntityProperties.SetArray();
161 for (auto i = 0; i < sceneEntity->getPropertyCount(); i++) {
162 auto sceneEntityProperty = sceneEntity->getPropertyByIndex(i);
163 Value jSceneEntityProperty;
164 jSceneEntityProperty.SetObject();
165 jSceneEntityProperty.AddMember("name", Value(sceneEntityProperty->getName(), jAllocator), jAllocator);
166 jSceneEntityProperty.AddMember("value", Value(sceneEntityProperty->getValue(), jAllocator), jAllocator);
167 jEntityProperties.PushBack(jSceneEntityProperty, jAllocator);
168 }
169 jObject.AddMember("properties", jEntityProperties, jAllocator);
170 jObjects.PushBack(jObject, jAllocator);
171 }
172 jDocument.AddMember("objects", jObjects, jAllocator);
173 jDocument.AddMember("objects_eidx", Value(scene->getEntityIdx()), jAllocator);
174
175 Value jSky;
176 jSky.SetObject();
177 jSky.AddMember("file", Value(scene->getSkyModelFileName(), jAllocator), jAllocator);
178 jSky.AddMember("sx", Value(scene->getSkyModelScale().getX()), jAllocator);
179 jSky.AddMember("sy", Value(scene->getSkyModelScale().getY()), jAllocator);
180 jSky.AddMember("sz", Value(scene->getSkyModelScale().getZ()), jAllocator);
181 jDocument.AddMember("sky", jSky, jAllocator);
182
183 StringBuffer strbuf;
184 Writer<StringBuffer> writer(strbuf);
185 jDocument.Accept(writer);
186
187 FileSystem::getInstance()->setContentFromString(pathName, fileName, strbuf.GetString());
188}
Rotation representation.
Definition: Rotation.h:18
Transformations which contain scale, rotations and translation.
const Vector3 & getTranslation() const
Color 4 definition.
Definition: Color4.h:20
Represents rotation orders of a model.
Definition: RotationOrder.h:23
BaseProperty * getPropertyByIndex(int idx)
Get property by index.
Base property model class.
Definition: BaseProperty.h:16
Prototype definition.
Definition: Prototype.h:49
Scene entity definition.
Definition: SceneEntity.h:24
Transformations & getTransformations()
Definition: SceneEntity.h:78
Scene prototype library definition.
Definition: SceneLibrary.h:27
Scene light definition.
Definition: SceneLight.h:21
Scene definition.
Definition: Scene.h:41
const Vector3 & getSkyModelScale()
Definition: Scene.h:321
void setFileName(const string &fileName)
Set up scene file name including relative path.
Definition: Scene.h:109
SceneLibrary * getLibrary()
Definition: Scene.h:168
RotationOrder * getRotationOrder()
Definition: Scene.h:116
SceneLight * getLightAt(int i)
Get light at given index.
Definition: Scene.h:140
SceneEntity * getEntityAt(int idx)
Returns entity at given index.
Definition: Scene.h:287
const string & getSkyModelFileName()
Definition: Scene.h:294
3D vector 3 class
Definition: Vector3.h:22
float getY() const
Definition: Vector3.h:119
float getX() const
Definition: Vector3.h:103
float getZ() const
Definition: Vector3.h:136
3D vector 4 class
Definition: Vector4.h:19
File system singleton class.
Definition: FileSystem.h:14
const string & getName() const
Definition: Enum.h:30