TDME2  1.9.200
TMReader.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <tdme/tdme.h>
4 
5 #include <array>
6 #include <string>
7 #include <unordered_map>
8 #include <vector>
9 
10 #include <tdme/tdme.h>
14 #include <tdme/engine/Texture.h>
15 #include <tdme/math/fwd-tdme.h>
18 
21 
22 using std::array;
23 using std::string;
24 using std::unordered_map;
25 using std::vector;
26 
38 
39 namespace tdme {
40 namespace engine {
41 namespace fileio {
42 namespace models {
43 
44 /**
45  * TM reader input stream
46  * @author Andreas Drewke
47  */
49 private:
50  const vector<uint8_t>* data;
51  int position;
52 public:
53  /**
54  * Constructor
55  * @param data input data array
56  */
57  inline TMReaderInputStream(const vector<uint8_t>* data): data(data), position(0) {
58  }
59 
60  /**
61  * Reads a boolean from input stream
62  * @throws model file IO exception
63  * @return boolean
64  */
65  inline bool readBoolean() {
66  return readByte() == 1;
67  }
68 
69  /**
70  * Reads a byte from input stream
71  * @throws model file IO exception
72  * @throws tdme::engine::fileio::models::ModelFileIOException
73  * @return byte
74  */
75  inline int8_t readByte() {
76  if (position == data->size()) {
77  throw ModelFileIOException("Unexpected end of stream");
78  }
79  return (*data)[position++];
80  }
81 
82  /**
83  * Reads a integer from input stream
84  * @throws tdme::engine::fileio::models::ModelFileIOException
85  * @return int
86  */
87  inline int32_t readInt() {
88  int32_t value =
89  ((static_cast<int32_t>(readByte()) & 0xFF) << 24) +
90  ((static_cast<int32_t>(readByte()) & 0xFF) << 16) +
91  ((static_cast<int32_t>(readByte()) & 0xFF) << 8) +
92  ((static_cast<int32_t>(readByte()) & 0xFF) << 0);
93  return value;
94  }
95 
96  /**
97  * Reads a float from input stream
98  * @throws tdme::engine::fileio::models::ModelFileIOException
99  * @return float
100  */
101  inline float readFloat() {
102  int32_t value =
103  ((static_cast<int32_t>(readByte()) & 0xFF) << 24) +
104  ((static_cast<int32_t>(readByte()) & 0xFF) << 16) +
105  ((static_cast<int32_t>(readByte()) & 0xFF) << 8) +
106  ((static_cast<int32_t>(readByte()) & 0xFF) << 0);
107  float* floatValue = (float*)&value;
108  return *floatValue;
109  }
110 
111  /**
112  * Reads a string from input stream
113  * @throws tdme::engine::fileio::models::ModelFileIOException
114  * @return string
115  */
116  inline const string readString() {
117  if (readBoolean() == false) {
118  return "";
119  } else {
120  auto l = readInt();
121  string s;
122  for (auto i = 0; i < l; i++) {
123  s+= static_cast<char>(readByte());
124  }
125  return s;
126  }
127  }
128 
129  /**
130  * Reads a float array from input stream
131  * @throws tdme::engine::fileio::models::ModelFileIOException
132  * @return float array
133  */
134  inline void readFloatArray(array<float, 16>& data) {
135  auto length = readInt();
136  if (length != data.size()) {
137  throw ModelFileIOException("Wrong float array size");
138  }
139  for (auto i = 0; i < data.size(); i++) {
140  (data)[i] = readFloat();
141  }
142  }
143 
144  /**
145  * Reads a float array from input stream
146  * @throws tdme::engine::fileio::models::ModelFileIOException
147  * @return float array
148  */
149  inline void readFloatArray(array<float, 9>& data) {
150  auto length = readInt();
151  if (length != data.size()) {
152  throw ModelFileIOException("Wrong float array size");
153  }
154  for (auto i = 0; i < data.size(); i++) {
155  (data)[i] = readFloat();
156  }
157  }
158 
159  /**
160  * Reads a float array from input stream
161  * @throws tdme::engine::fileio::models::ModelFileIOException
162  * @return float array
163  */
164  inline void readFloatArray(array<float, 4>& data) {
165  auto length = readInt();
166  if (length != data.size()) {
167  throw ModelFileIOException("Wrong float array size");
168  }
169  for (auto i = 0; i < data.size(); i++) {
170  (data)[i] = readFloat();
171  }
172  }
173 
174  /**
175  * Reads a float array from input stream
176  * @throws tdme::engine::fileio::models::ModelFileIOException
177  * @return float array
178  */
179  inline void readFloatArray(array<float, 3>& data) {
180  auto length = readInt();
181  if (length != data.size()) {
182  throw ModelFileIOException("Wrong float array size");
183  }
184  for (auto i = 0; i < data.size(); i++) {
185  data[i] = readFloat();
186  }
187  }
188 
189  /**
190  * Reads a float array from input stream
191  * @throws tdme::engine::fileio::models::ModelFileIOException
192  * @return float array
193  */
194  inline void readFloatArray(array<float, 2>& data) {
195  auto length = readInt();
196  if (length != data.size()) {
197  throw ModelFileIOException("Wrong float array size");
198  }
199  for (auto i = 0; i < data.size(); i++) {
200  data[i] = readFloat();
201  }
202  }
203 
204  /**
205  * Reads a float array from input stream
206  * @throws tdme::engine::fileio::models::ModelFileIOException
207  * @return float array
208  */
209  inline const vector<float> readFloatVector() {
210  vector<float> f;
211  f.resize(readInt());
212  for (auto i = 0; i < f.size(); i++) {
213  f[i] = readFloat();
214  }
215  return f;
216  }
217 
218 };
219 
220 };
221 };
222 };
223 };
224 
225 /**
226  * TDME model reader
227  * @author Andreas Drewke
228  */
230 {
231 public:
232 
233  /**
234  * TDME model format reader
235  * @param pathName path name
236  * @param fileName file name
237  * @param useBC7TextureCompression use BC7 texture compression
238  * @throws tdme::os::filesystem::FileSystemException
239  * @throws tdme::engine::fileio::models::ModelFileIOException
240  * @return model
241  */
242  static Model* read(const string& pathName, const string& fileName, bool useBC7TextureCompression = true);
243 
244  /**
245  * TDME model format reader
246  * @param data data vector to read TM from
247  * @param pathName path name file was read from
248  * @param fileName file name was read from
249  * @param useBC7TextureCompression use BC7 texture compression
250  * @throws tdme::engine::fileio::models::ModelFileIOException
251  * @return model
252  */
253  static Model* read(const vector<uint8_t>& data, const string& pathName = string(), const string& fileName = string(), bool useBC7TextureCompression = true);
254 
255 private:
256  /**
257  * Get texture path
258  * @param modelPathName model path name
259  * @param texturePathName texture path name
260  * @param textureFileName texture file name
261  */
262  static const string getTexturePath(const string& modelPathName, const string& texturePathName, const string& textureFileName);
263 
264  /**
265  * Read material
266  * @param embeddedTextures embedded textures
267  * @param fileName file name
268  * @return material or nullptr
269  */
270  inline static Texture* getEmbeddedTexture(const unordered_map<string, Texture*>& embeddedTextures, const string& fileName) {
271  auto embeddedTextureIt = embeddedTextures.find(fileName);
272  if (embeddedTextureIt == embeddedTextures.end()) return nullptr;
273  auto embeddedTexture = embeddedTextureIt->second;
274  embeddedTexture->acquireReference();
275  return embeddedTexture;
276  }
277 
278  /**
279  * Read material
280  * @param pathName path name
281  * @param is input stream
282  * @param embeddedTextures embedded textures
283  * @param useBC7TextureCompression use BC7 texture compression
284  * @param version version
285  * @throws tdme::engine::fileio::models::ModelFileIOException
286  * @return material
287  */
288  static void readEmbeddedTextures(TMReaderInputStream* is, unordered_map<string, Texture*>& embeddedTextures, const array<uint8_t, 3>& version);
289 
290  /**
291  * Read material
292  * @param pathName path name
293  * @param is input stream
294  * @param model model
295  * @param embeddedTextures embedded textures
296  * @param version version
297  * @throws tdme::engine::fileio::models::ModelFileIOException
298  * @return material
299  */
300  static Material* readMaterial(const string& pathName, TMReaderInputStream* is, Model* model, const unordered_map<string, Texture*>& embeddedTextures, bool useBC7TextureCompression, const array<uint8_t, 3>& version);
301 
302  /**
303  * Read animation setup
304  * @param is input stream
305  * @param model model
306  * @param version version
307  * @throws tdme::engine::fileio::models::ModelFileIOException
308  */
309  static void readAnimationSetup(TMReaderInputStream* is, Model* model, const array<uint8_t, 3>& version);
310 
311  /**
312  * Read vertices from input stream
313  * @param is input stream
314  * @throws tdme::engine::fileio::models::ModelFileIOException
315  * @return vector3 array
316  */
317  static const vector<Vector3> readVertices(TMReaderInputStream* is);
318 
319  /**
320  * Read texture coordinates from input stream
321  * @param is input stream
322  * @throws tdme::engine::fileio::models::ModelFileIOException
323  * @return texture coordinates array
324  */
325  static const vector<Vector2> readTextureCoordinates(TMReaderInputStream* is);
326 
327  /**
328  * Read indices from input stream
329  * @param is input stream
330  * @param indices indices
331  * @throws tdme::engine::fileio::models::ModelFileIOException
332  * @return if having indices
333  */
334  static bool readIndices(TMReaderInputStream* is, array<int32_t, 3>* indices);
335 
336  /**
337  * Read animation from input stream into node
338  * @param is input stream
339  * @param g node
340  * @throws tdme::engine::fileio::models::ModelFileIOException
341  * @return Animation
342  */
344 
345  /**
346  * Read faces entities from input stream
347  * @param is input stream
348  * @param g node
349  * @throws tdme::engine::fileio::models::ModelFileIOException
350  */
351  static void readFacesEntities(TMReaderInputStream* is, Node* g);
352 
353  /**
354  * Read skinning joint
355  * @param is input stream
356  * @throws tdme::engine::fileio::models::ModelFileIOException
357  * @return joint
358  */
360 
361  /**
362  * Read skinning joint weight
363  * @param is input stream
364  * @throws tdme::engine::fileio::models::ModelFileIOException
365  * @return joint weight
366  */
368 
369  /**
370  * Read skinning from input stream
371  * @param is input stream
372  * @param g node
373  * @throws tdme::engine::fileio::models::ModelFileIOException
374  */
375  static void readSkinning(TMReaderInputStream* is, Node* g);
376 
377  /**
378  * Read sub nodes
379  * @param is input stream
380  * @param model model
381  * @param parentNode parent node
382  * @param subNodes sub nodes
383  * @throws IOException
384  * @throws tdme::engine::fileio::models::ModelFileIOException
385  * @return node
386  */
387  static void readSubNodes(TMReaderInputStream* is, Model* model, Node* parentNode, unordered_map<string, Node*>& subNodes);
388 
389  /**
390  * Write node to output stream
391  * @param is input stream
392  * @param model model
393  * @param parentNode parent node
394  * @throws tdme::engine::fileio::models::ModelFileIOException
395  * @return node
396  */
397  static Node* readNode(TMReaderInputStream* is, Model* model, Node* parentNode);
398 };
Texture entity.
Definition: Texture.h:24
bool readBoolean()
Reads a boolean from input stream.
Definition: TMReader.h:65
void readFloatArray(array< float, 16 > &data)
Reads a float array from input stream.
Definition: TMReader.h:134
const vector< float > readFloatVector()
Reads a float array from input stream.
Definition: TMReader.h:209
int32_t readInt()
Reads a integer from input stream.
Definition: TMReader.h:87
void readFloatArray(array< float, 4 > &data)
Reads a float array from input stream.
Definition: TMReader.h:164
const string readString()
Reads a string from input stream.
Definition: TMReader.h:116
void readFloatArray(array< float, 9 > &data)
Reads a float array from input stream.
Definition: TMReader.h:149
void readFloatArray(array< float, 3 > &data)
Reads a float array from input stream.
Definition: TMReader.h:179
TMReaderInputStream(const vector< uint8_t > *data)
Constructor.
Definition: TMReader.h:57
float readFloat()
Reads a float from input stream.
Definition: TMReader.h:101
int8_t readByte()
Reads a byte from input stream.
Definition: TMReader.h:75
void readFloatArray(array< float, 2 > &data)
Reads a float array from input stream.
Definition: TMReader.h:194
static void readSubNodes(TMReaderInputStream *is, Model *model, Node *parentNode, unordered_map< string, Node * > &subNodes)
Read sub nodes.
Definition: TMReader.cpp:762
static Joint readSkinningJoint(TMReaderInputStream *is)
Read skinning joint.
Definition: TMReader.cpp:721
static Material * readMaterial(const string &pathName, TMReaderInputStream *is, Model *model, const unordered_map< string, Texture * > &embeddedTextures, bool useBC7TextureCompression, const array< uint8_t, 3 > &version)
Read material.
Definition: TMReader.cpp:353
static const string getTexturePath(const string &modelPathName, const string &texturePathName, const string &textureFileName)
Get texture path.
Definition: TMReader.cpp:185
static const vector< Vector3 > readVertices(TMReaderInputStream *is)
Read vertices from input stream.
Definition: TMReader.cpp:608
static const vector< Vector2 > readTextureCoordinates(TMReaderInputStream *is)
Read texture coordinates from input stream.
Definition: TMReader.cpp:622
static Animation * readAnimation(TMReaderInputStream *is, Node *g)
Read animation from input stream into node.
Definition: TMReader.cpp:652
static void readFacesEntities(TMReaderInputStream *is, Node *g)
Read faces entities from input stream.
Definition: TMReader.cpp:672
static void readAnimationSetup(TMReaderInputStream *is, Model *model, const array< uint8_t, 3 > &version)
Read animation setup.
Definition: TMReader.cpp:582
static void readSkinning(TMReaderInputStream *is, Node *g)
Read skinning from input stream.
Definition: TMReader.cpp:738
static bool readIndices(TMReaderInputStream *is, array< int32_t, 3 > *indices)
Read indices from input stream.
Definition: TMReader.cpp:636
static void readEmbeddedTextures(TMReaderInputStream *is, unordered_map< string, Texture * > &embeddedTextures, const array< uint8_t, 3 > &version)
Read material.
Definition: TMReader.cpp:199
static JointWeight readSkinningJointWeight(TMReaderInputStream *is)
Read skinning joint weight.
Definition: TMReader.cpp:730
static Texture * getEmbeddedTexture(const unordered_map< string, Texture * > &embeddedTextures, const string &fileName)
Read material.
Definition: TMReader.h:270
static Model * read(const string &pathName, const string &fileName, bool useBC7TextureCompression=true)
TDME model format reader.
Definition: TMReader.cpp:69
static Node * readNode(TMReaderInputStream *is, Model *model, Node *parentNode)
Write node to output stream.
Definition: TMReader.cpp:772
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
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
virtual void acquireReference()
Acquires a reference, incrementing the counter.
Definition: Reference.h:31
Definition: fwd-tdme.h:4