TDME2  1.9.200
GLTFReader.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 
5 #include <ext/tinygltf/tiny_gltf.h>
6 
7 #include <tdme/tdme.h>
11 #include <tdme/math/Matrix4x4.h>
13 
16 
17 using std::map;
18 using std::string;
19 using std::vector;
20 
26 
27 /**
28  * GLTF model reader
29  * @author Andreas Drewke
30  */
32 {
33 public:
34 
35  /**
36  * Reads GLTF file
37  * @param pathName path name
38  * @param fileName file name
39  * @param useBC7TextureCompression use BC7 texture compression
40  * @throws tdme::engine::fileio::models::ModelFileIOException
41  * @throws tdme::os::filesystem::FileSystemException
42  * @return model instance
43  */
44  static Model* read(const string& pathName, const string& fileName, bool useBC7TextureCompression = true);
45 
46 private:
47 
48  /**
49  * @return component byte size
50  */
51  inline static size_t getComponentTypeByteSize(int type) {
52  switch (type) {
53  case TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE:
54  case TINYGLTF_COMPONENT_TYPE_BYTE:
55  return sizeof(char);
56  case TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT:
57  case TINYGLTF_COMPONENT_TYPE_SHORT:
58  return sizeof(short);
59  case TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT:
60  case TINYGLTF_COMPONENT_TYPE_INT:
61  return sizeof(int);
62  case TINYGLTF_COMPONENT_TYPE_FLOAT:
63  return sizeof(float);
64  case TINYGLTF_COMPONENT_TYPE_DOUBLE:
65  return sizeof(double);
66  default:
67  return 0;
68  }
69  }
70 
71  /**
72  * @return component type string
73  */
74  inline static string getComponentTypeString(int type) {
75  switch (type) {
76  case TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE:
77  return "uint8_t";
78  case TINYGLTF_COMPONENT_TYPE_BYTE:
79  return "int8_t";
80  case TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT:
81  return "uint16_t";
82  case TINYGLTF_COMPONENT_TYPE_SHORT:
83  return "int16_t";
84  case TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT:
85  return "uint32_t";
86  case TINYGLTF_COMPONENT_TYPE_INT:
87  return "int32_t";
88  case TINYGLTF_COMPONENT_TYPE_FLOAT:
89  return "float";
90  case TINYGLTF_COMPONENT_TYPE_DOUBLE:
91  return "double";
92  default:
93  return "unknown";
94  }
95  }
96 
97  /**
98  * @return type string
99  */
100  inline static string getTypeString(int type) {
101  switch (type) {
102  case TINYGLTF_TYPE_VEC2:
103  return "Vector2";
104  case TINYGLTF_TYPE_VEC3:
105  return "Vector3";
106  case TINYGLTF_TYPE_VEC4:
107  return "Vector4";
108  case TINYGLTF_TYPE_MAT2:
109  return "Matrix2x2";
110  case TINYGLTF_TYPE_MAT3:
111  return "Matrix3x3";
112  case TINYGLTF_TYPE_MAT4:
113  return "Matrix4x4";
114  case TINYGLTF_TYPE_SCALAR:
115  return "Scalar";
116  case TINYGLTF_TYPE_VECTOR:
117  return "Vector";
118  case TINYGLTF_TYPE_MATRIX:
119  return "Matrix";
120  default:
121  return 0;
122  }
123  }
124 
125  /**
126  * Interpolate key frames to our internal 30fps format
127  * @param frameTimeCount frame time count
128  * @param frameTimes frameTimes
129  * @param keyFrameMatrices key frame matrices
130  * @param interpolatedMatrixCount interpolated matrix count
131  * @param interpolatedMatrices interpolated matrices
132  * @param frameStartIdx frame start idx
133  */
134  static void interpolateKeyFrames(int frameTimeCount, const float* frameTimes, const vector<Matrix4x4>& keyFrameMatrices, int interpolatedMatrixCount, vector<Matrix4x4>& interpolatedMatrices, int frameStartIdx);
135 
136  /**
137  * Parse GLTF node
138  * @param pathName model path name
139  * @param fileName model file name
140  * @param gltfModel GLTF mode
141  * @param gltfNodeIdx GLTF node index
142  * @param model TDME model
143  * @param parentNode TDME parent node
144  * @param anonymousNodeIdx anonymous node index
145  * @param useBC7TextureCompression use BC7 texture compression
146  * @return node
147  */
148  static Node* parseNode(const string& pathName, const string& fileName, tinygltf::Model& gltfModel, int gltfNodeIdx, Model* model, Node* parentNode, int& anonymousNodeIdx, bool useBC7TextureCompression);
149 
150  /**
151  * Parse GLTF node children into TDME node
152  * @param pathName model path name
153  * @param fileName model file name
154  * @param gltfModel GLTF model
155  * @param gltfNodeChildrenIdx GLTF node children indices
156  * @param parentNode TDME parent node
157  * @param anonymousNodeIdx anonymous node index
158  * @param useBC7TextureCompression use BC7 texture compression
159  */
160  static void parseNodeChildren(const string& pathName, const string& fileName, tinygltf::Model& gltfModel, const vector<int>& gltfNodeChildrenIdx, Node* parentNode, int& anonymousNodeIdx, bool useBC7TextureCompression);
161 
162  /**
163  * Determine texture file name
164  * @param pathName model path name
165  * @param fileName model file name
166  * @param imageName image name
167  * @return file name
168  */
169  inline static const string determineTextureFileName(const string& pathName, const string& fileName, const string& imageName) {
170  return pathName + "/" + fileName + "-" + imageName + ".png";
171  }
172 
173  /**
174  * Compute tangents and bitangents
175  * @param node node
176  */
177  static void computeTangentsAndBitangents(Node* node);
178 
179  /**
180  * Get node scale matrix
181  * @param gltfModel GLTF model
182  * @param nodeId node id
183  * @return scale matrix of given node
184  */
185  static const Matrix4x4 getNodeScaleMatrix(const tinygltf::Model& gltfModel, const string& nodeId);
186 
187  /**
188  * Get node rotation matrix
189  * @param gltfModel GLTF model
190  * @param nodeId node id
191  * @return rotation matrix of given node
192  */
193  static const Matrix4x4 getNodeRotationMatrix(const tinygltf::Model& gltfModel, const string& nodeId);
194 
195  /**
196  * Get node translation matrix
197  * @param gltfModel GLTF model
198  * @param nodeId node id
199  * @return translation matrix of given node
200  */
201  static const Matrix4x4 getNodeTranslationMatrix(const tinygltf::Model& gltfModel, const string& nodeId);
202 
203 };
static void computeTangentsAndBitangents(Node *node)
Compute tangents and bitangents.
Definition: GLTFReader.cpp:985
static string getComponentTypeString(int type)
Definition: GLTFReader.h:74
static string getTypeString(int type)
Definition: GLTFReader.h:100
static const Matrix4x4 getNodeScaleMatrix(const tinygltf::Model &gltfModel, const string &nodeId)
Get node scale matrix.
Definition: GLTFReader.cpp:992
static size_t getComponentTypeByteSize(int type)
Definition: GLTFReader.h:51
static Node * parseNode(const string &pathName, const string &fileName, tinygltf::Model &gltfModel, int gltfNodeIdx, Model *model, Node *parentNode, int &anonymousNodeIdx, bool useBC7TextureCompression)
Parse GLTF node.
Definition: GLTFReader.cpp:336
static void parseNodeChildren(const string &pathName, const string &fileName, tinygltf::Model &gltfModel, const vector< int > &gltfNodeChildrenIdx, Node *parentNode, int &anonymousNodeIdx, bool useBC7TextureCompression)
Parse GLTF node children into TDME node.
Definition: GLTFReader.cpp:972
static const string determineTextureFileName(const string &pathName, const string &fileName, const string &imageName)
Determine texture file name.
Definition: GLTFReader.h:169
static const Matrix4x4 getNodeTranslationMatrix(const tinygltf::Model &gltfModel, const string &nodeId)
Get node translation matrix.
static const Matrix4x4 getNodeRotationMatrix(const tinygltf::Model &gltfModel, const string &nodeId)
Get node rotation matrix.
static void interpolateKeyFrames(int frameTimeCount, const float *frameTimes, const vector< Matrix4x4 > &keyFrameMatrices, int interpolatedMatrixCount, vector< Matrix4x4 > &interpolatedMatrices, int frameStartIdx)
Interpolate key frames to our internal 30fps format.
Definition: GLTFReader.cpp:309
static Model * read(const string &pathName, const string &fileName, bool useBC7TextureCompression=true)
Reads GLTF file.
Definition: GLTFReader.cpp:85
Representation of a 3D model.
Definition: Model.h:35
Model node.
Definition: Node.h:32
Matrix4x4 class representing matrix4x4 mathematical structure and operations for 3d space.
Definition: Matrix4x4.h:23