TDME2  1.9.200
TMWriter.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <array>
4 #include <string>
5 #include <unordered_map>
6 #include <vector>
7 
8 #include <tdme/tdme.h>
12 #include <tdme/engine/fwd-tdme.h>
13 #include <tdme/math/fwd-tdme.h>
16 
19 
20 using std::array;
21 using std::string;
22 using std::unordered_map;
23 using std::vector;
24 
39 
40 namespace tdme {
41 namespace engine {
42 namespace fileio {
43 namespace models {
44 
45 /**
46  * TM writer output stream
47  * @author Andreas Drewke
48  */
50 private:
51  vector<uint8_t>* data;
52 public:
53 
54  /**
55  * Constructor
56  * @param data data vector to write TM to
57  */
58  inline TMWriterOutputStream(vector<uint8_t>* data) {
59  this->data = data;
60  }
61 
62  /**
63  * Get data
64  * @return data
65  */
66  inline vector<uint8_t>* getData() {
67  return data;
68  }
69 
70  /**
71  * Writes a boolean to output stream
72  * @throws model file IO exception
73  * @param b boolean
74  */
75  inline void writeBoolean(bool b) {
76  writeByte(b == true?1:0);
77  }
78 
79  /**
80  * Writes a byte to output stream
81  * @throws model file IO exception
82  * @param b byte
83  */
84  inline void writeByte(uint8_t b) {
85  data->push_back(b);
86  }
87 
88  /**
89  * Writes a integer to output stream
90  * @throws model file IO exception
91  * @param i int
92  */
93  inline void writeInt(int32_t i) {
94  writeByte((i >> 24) & 0xFF);
95  writeByte((i >> 16) & 0xFF);
96  writeByte((i >> 8) & 0xFF);
97  writeByte((i >> 0) & 0xFF);
98  }
99 
100  /**
101  * Writes a float to output stream
102  * @param f float
103  * @throws model file IO exception
104  */
105  inline void writeFloat(float f) {
106  int value = *((int*)&f);
107  writeInt(value);
108  }
109 
110  /**
111  * Writes a string to output stream
112  * @param s string
113  * @throws model file IO exception
114  */
115  inline void writeString(const string& s) {
116  if (s.size() == 0) {
117  writeBoolean(false);
118  } else {
119  writeBoolean(true);
120  writeInt(s.size());
121  for (auto i = 0; i < s.size(); i++) {
122  writeByte(static_cast<uint8_t>(s[i]));
123  }
124  }
125  }
126 
127  /**
128  * Writes a float array to output stream
129  * @param f float array
130  * @throws model file IO exception
131  */
132  inline void writeFloatArray(const array<float,2>& f) {
133  writeInt(f.size());
134  for (auto i = 0; i < f.size(); i++) {
135  writeFloat(f[i]);
136  }
137  }
138 
139  /**
140  * Writes a float array to output stream
141  * @param f float array
142  * @throws model file IO exception
143  */
144  inline void writeFloatArray(const array<float,3>& f) {
145  writeInt(f.size());
146  for (auto i = 0; i < f.size(); i++) {
147  writeFloat(f[i]);
148  }
149  }
150 
151  /**
152  * Writes a float array to output stream
153  * @param f float array
154  * @throws model file IO exception
155  */
156  inline void writeFloatArray(const array<float,4>& f) {
157  writeInt(f.size());
158  for (auto i = 0; i < f.size(); i++) {
159  writeFloat(f[i]);
160  }
161  }
162 
163  /**
164  * Writes a float array to output stream
165  * @param f float array
166  * @throws model file IO exception
167  */
168  inline void writeFloatArray(const array<float,9>& f) {
169  writeInt(f.size());
170  for (auto i = 0; i < f.size(); i++) {
171  writeFloat(f[i]);
172  }
173  }
174 
175  /**
176  * Writes a float array to output stream
177  * @param f float array
178  * @throws model file IO exception
179  */
180  inline void writeFloatArray(const array<float,16>& f) {
181  writeInt(f.size());
182  for (auto i = 0; i < f.size(); i++) {
183  writeFloat(f[i]);
184  }
185  }
186 
187  /**
188  * Writes a float array to output stream
189  * @param f float array
190  * @throws model file IO exception
191  */
192  inline void writeFloatArray(const vector<float>& f) {
193  writeInt(f.size());
194  for (auto i = 0; i < f.size(); i++) {
195  writeFloat(f[i]);
196  }
197  }
198 
199  /**
200  * Writes a uint8_t array to output stream, note that no size information is given in this case
201  * @param d uint8_t array
202  * @throws model file IO exception
203  */
204  inline void writeUInt8tArray(const vector<uint8_t>& d) {
205  for (auto i = 0; i < d.size(); i++) {
206  writeByte(d[i]);
207  }
208  }
209 
210 };
211 
212 };
213 };
214 };
215 };
216 
217 /**
218  * TDME model writer
219  * @author Andreas Drewke
220  */
222 {
223 public:
224 
225  /**
226  * TDME model format writer
227  * @param model model
228  * @param pathName path name
229  * @param fileName file name
230  * @param useBC7TextureCompression use BC7 texture compression
231  * @throws tdme::os::filesystem::FileSystemException
232  * @throws tdme::engine::fileio::models::ModelFileIOException
233  */
234  static void write(Model* model, const string& pathName, const string& fileName, bool useBC7TextureCompression = true);
235 
236  /**
237  * TDME model format writer
238  * @param model model
239  * @param data data to write TM to
240  * @param useBC7TextureCompression use BC7 texture compression
241  */
242  static void write(Model* model, vector<uint8_t>& data, bool useBC7TextureCompression = true);
243 
244 private:
245 
246  /**
247  * Write embedded textures
248  * @param os output stream
249  * @param m model
250  * @param useBC7TextureCompression use BC7 texture compression
251  * @throws model file IO exception
252  */
253  static void writeEmbeddedTextures(TMWriterOutputStream* os, Model* m, bool useBC7TextureCompression);
254 
255  /**
256  * Write material
257  * @param os output stream
258  * @param m material
259  * @throws model file IO exception
260  */
261  static void writeMaterial(TMWriterOutputStream* os, Material* m);
262 
263  /**
264  * Write animation setup
265  * @param os output stream
266  * @param animationSetup animation setup
267  * @throws model file IO exception
268  */
269  static void writeAnimationSetup(TMWriterOutputStream* os, AnimationSetup* animationSetup);
270 
271  /**
272  * Write vertices to output stream
273  * @param os output stream
274  * @param v vertices
275  * @throws model file IO exception
276  */
277  static void writeVertices(TMWriterOutputStream* os, const vector<Vector3>& v);
278 
279  /**
280  * Write texture coordinates to output stream
281  * @param os output stream
282  * @param tc texture coordinates
283  * @throws model file IO exception
284  */
285  static void writeTextureCoordinates(TMWriterOutputStream* os, const vector<Vector2>& tc);
286 
287  /**
288  * Write indices to output stream
289  * @param os output stream
290  * @param indices indices
291  * @throws model file IO exception
292  */
293  static void writeIndices(TMWriterOutputStream* os, const array<int32_t, 3>& indices);
294 
295  /**
296  * Write animation to output stream
297  * @param os output stream
298  * @param a animation
299  * @throws model file IO exception
300  */
301  static void writeAnimation(TMWriterOutputStream* os, Animation* a);
302 
303  /**
304  * Write faces entities to output stream
305  * @param os output stream
306  * @param facesEntities faces entities
307  * @throws model file IO exception
308  */
309  static void writeFacesEntities(TMWriterOutputStream* os, const vector<FacesEntity>& facesEntities);
310 
311  /**
312  * Write skinning joint
313  * @param os output stream
314  * @param joint joint
315  * @throws model file IO exception
316  */
317  static void writeSkinningJoint(TMWriterOutputStream* os, const Joint& joint);
318 
319  /**
320  * Write skinning joint weight
321  * @param os output stream
322  * @param jointWeight joint
323  * @throws model file IO exception
324  */
325  static void writeSkinningJointWeight(TMWriterOutputStream* os, const JointWeight& jointWeight);
326 
327  /**
328  * Write skinning to output stream
329  * @param os output stream
330  * @param skinning skinning
331  * @throws model file IO exception
332  */
333  static void writeSkinning(TMWriterOutputStream* os, Skinning* skinning);
334 
335  /**
336  * Write sub nodes
337  * @param os output stream
338  * @param subNodes sub nodes
339  * @throws model file IO exception
340  */
341  static void writeSubNodes(TMWriterOutputStream* os, const unordered_map<string, Node*>& subNodes);
342 
343  /**
344  * Write node to output stream
345  * @param os output stream
346  * @param g node
347  * @throws model file IO exception
348  */
349  static void writeNode(TMWriterOutputStream* os, Node* g);
350 
351  /**
352  * Write thumbnail to output stream
353  * @param os output stream
354  * @param model model
355  * @throws model file IO exception
356  */
357  static void writeThumbnail(TMWriterOutputStream* os, Model* model);
358 
359 };
Texture entity.
Definition: Texture.h:24
void writeFloatArray(const array< float, 3 > &f)
Writes a float array to output stream.
Definition: TMWriter.h:144
vector< uint8_t > * getData()
Get data.
Definition: TMWriter.h:66
void writeString(const string &s)
Writes a string to output stream.
Definition: TMWriter.h:115
void writeFloatArray(const array< float, 9 > &f)
Writes a float array to output stream.
Definition: TMWriter.h:168
void writeFloatArray(const array< float, 4 > &f)
Writes a float array to output stream.
Definition: TMWriter.h:156
void writeUInt8tArray(const vector< uint8_t > &d)
Writes a uint8_t array to output stream, note that no size information is given in this case.
Definition: TMWriter.h:204
void writeByte(uint8_t b)
Writes a byte to output stream.
Definition: TMWriter.h:84
void writeInt(int32_t i)
Writes a integer to output stream.
Definition: TMWriter.h:93
void writeFloat(float f)
Writes a float to output stream.
Definition: TMWriter.h:105
void writeFloatArray(const array< float, 2 > &f)
Writes a float array to output stream.
Definition: TMWriter.h:132
void writeBoolean(bool b)
Writes a boolean to output stream.
Definition: TMWriter.h:75
void writeFloatArray(const vector< float > &f)
Writes a float array to output stream.
Definition: TMWriter.h:192
TMWriterOutputStream(vector< uint8_t > *data)
Constructor.
Definition: TMWriter.h:58
void writeFloatArray(const array< float, 16 > &f)
Writes a float array to output stream.
Definition: TMWriter.h:180
static void writeAnimationSetup(TMWriterOutputStream *os, AnimationSetup *animationSetup)
Write animation setup.
Definition: TMWriter.cpp:227
static void writeAnimation(TMWriterOutputStream *os, Animation *a)
Write animation to output stream.
Definition: TMWriter.cpp:271
static void writeTextureCoordinates(TMWriterOutputStream *os, const vector< Vector2 > &tc)
Write texture coordinates to output stream.
Definition: TMWriter.cpp:249
static void write(Model *model, const string &pathName, const string &fileName, bool useBC7TextureCompression=true)
TDME model format writer.
Definition: TMWriter.cpp:84
static void writeFacesEntities(TMWriterOutputStream *os, const vector< FacesEntity > &facesEntities)
Write faces entities to output stream.
Definition: TMWriter.cpp:284
static void writeSkinning(TMWriterOutputStream *os, Skinning *skinning)
Write skinning to output stream.
Definition: TMWriter.cpp:320
static void writeThumbnail(TMWriterOutputStream *os, Model *model)
Write thumbnail to output stream.
Definition: TMWriter.cpp:366
static void writeVertices(TMWriterOutputStream *os, const vector< Vector3 > &v)
Write vertices to output stream.
Definition: TMWriter.cpp:236
static void writeMaterial(TMWriterOutputStream *os, Material *m)
Write material.
Definition: TMWriter.cpp:179
static void writeEmbeddedTextures(TMWriterOutputStream *os, Model *m, bool useBC7TextureCompression)
Write embedded textures.
Definition: TMWriter.cpp:120
static void writeSkinningJoint(TMWriterOutputStream *os, const Joint &joint)
Write skinning joint.
Definition: TMWriter.cpp:308
static void writeNode(TMWriterOutputStream *os, Node *g)
Write node to output stream.
Definition: TMWriter.cpp:349
static void writeSubNodes(TMWriterOutputStream *os, const unordered_map< string, Node * > &subNodes)
Write sub nodes.
Definition: TMWriter.cpp:341
static void writeIndices(TMWriterOutputStream *os, const array< int32_t, 3 > &indices)
Write indices to output stream.
Definition: TMWriter.cpp:262
static void writeSkinningJointWeight(TMWriterOutputStream *os, const JointWeight &jointWeight)
Write skinning joint weight.
Definition: TMWriter.cpp:314
Node faces entity A node can have multiple entities containing faces and a applied material.
Definition: FacesEntity.h:23
Joint / Bone.
Definition: Joint.h:19
Represents a material.
Definition: Material.h:23
Representation of a 3D model.
Definition: Model.h:35
Model node.
Definition: Node.h:32
Skinning definition for nodes.
Definition: Skinning.h:25
Vector2 class representing vector2 mathematical structure and operations with x, y components.
Definition: Vector2.h:20
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20
Definition: fwd-tdme.h:4