TDME2  1.9.200
FBXReader.h
Go to the documentation of this file.
1 #pragma once
2 
3 #define FBXSDK_SHARED
4 #include <fbxsdk.h>
5 #undef isnan
6 
7 #include <vector>
8 
9 #include <tdme/tdme.h>
12 #include <tdme/engine/Color4.h>
15 
16 using std::vector;
17 
25 
26 namespace tdme {
27 namespace engine {
28 namespace fileio {
29 namespace models {
30 
31 /**
32  * FBX reader stream
33  * @author Andreas Drewke
34  */
35 class FBXReaderStream: public FbxStream {
36 public:
37 
38  /**
39  * Constructor
40  * @param fbxManager FBX manager
41  * @param data input data array
42  */
43  inline FBXReaderStream(FbxManager *fbxManager, const vector<uint8_t> *data): data(data), position(0) {
44  readerID = fbxManager->GetIOPluginRegistry()->FindReaderIDByDescription("FBX (*.fbx)");
45  }
46 
47  /**
48  * Destructor
49  */
50  inline ~FBXReaderStream() {
51  Close();
52  }
53 
54  /**
55  * @return state
56  */
57  virtual EState GetState() {
58  return opened?eOpen:eClosed;
59  }
60 
61  /**
62  * Open stream
63  */
64  virtual bool Open(void*) {
65  position = 0LL;
66  opened = true;
67  return true;
68  }
69 
70  /**
71  * Close stream
72  */
73  virtual bool Close() {
74  position = -1LL;
75  opened = false;
76  return true;
77  }
78 
79  /**
80  * Flush
81  */
82  virtual bool Flush() {
83  return true;
84  }
85 
86  /**
87  * Write to stream, which is currently not supported
88  * @param data data
89  * @param size size
90  */
91  virtual int Write(const void* data, int size) {
92  return 0;
93  }
94 
95  /**
96  * Read from stream
97  * @param data data
98  * @param size size
99  */
100  virtual int Read(void* data, int size) const {
101  auto i = 0;
102  for (; i < size; i++) {
103  static_cast<uint8_t*>(data)[i] = (*this->data)[position++];
104  }
105  return i;
106  }
107 
108  /**
109  * @return reader id
110  */
111  virtual int GetReaderID() const {
112  return readerID;
113  }
114 
115  /**
116  * @return writer id
117  */
118  virtual int GetWriterID() const {
119  return -1;
120  }
121 
122  /**
123  * Seek
124  * @param offset offset
125  * @param seekPos seek pos
126  */
127  void Seek(const FbxInt64& offset, const FbxFile::ESeekPos& seekPos) {
128  switch (seekPos) {
129  case FbxFile::eBegin:
130  position = offset;
131  break;
132  case FbxFile::eCurrent:
133  position += offset;
134  break;
135  case FbxFile::eEnd:
136  position = data->size() - offset;
137  break;
138  }
139  }
140 
141  /**
142  * @return position
143  */
144  virtual long GetPosition() const {
145  return position;
146  }
147 
148  /**
149  * Set position
150  * @param position position
151  */
152  virtual void SetPosition(long position) {
153  this->position = position;
154  }
155 
156  /**
157  * @return error or 0 if no error occurred, we dont support errors, lol
158  */
159  virtual int GetError() const {
160  return 0;
161  }
162 
163  /**
164  * Clear errors, which we dont support
165  */
166  virtual void ClearError() {
167  }
168 
169 private:
170  FbxManager *fbxManager { nullptr };
171  bool opened { false };
172  int readerID { -1 };
173  const vector<uint8_t> *data { nullptr };
174  mutable int position { -1LL };
175 };
176 
177 };
178 };
179 };
180 };
181 
182 /**
183  * FBX model reader
184  * @author Andreas Drewke
185  */
187 {
188 public:
189 
190  /**
191  * Reads FBX file
192  * @param pathName path name
193  * @param fileName file name
194  * @param useBC7TextureCompression use BC7 texture compression
195  * @throws tdme::engine::fileio::models::ModelFileIOException
196  * @throws tdme::os::filesystem::FileSystemException
197  * @return model instance
198  */
199  static Model* read(const string& pathName, const string& fileName, bool useBC7TextureCompression = true);
200 
201 private:
203  static constexpr float BLENDER_AMBIENT_FROM_DIFFUSE_SCALE { 0.7f };
204  static constexpr float BLENDER_DIFFUSE_SCALE { 0.8f };
205 
206  /**
207  * Get scene up vector
208  * @param fbxScene fbx scene
209  * @throws tdme::engine::fileio::models::ModelFileIOException
210  */
211  static UpVector* getSceneUpVector(FbxScene* fbxScene);
212 
213  /**
214  * Get scene rotation order
215  * @param fbxScene fbx scene
216  * @throws tdme::engine::fileio::models::ModelFileIOException
217  */
218  static RotationOrder* getSceneRotationOrder(FbxScene* fbxScene);
219 
220  /**
221  * Set up model import rotation maxtrix
222  * @param model model
223  */
224  static void setupModelImportRotationMatrix(Model* model);
225 
226  /**
227  * Set up model import scale maxtrix
228  * @param fbxScene fbx scene
229  * @param model model
230  */
231  static void setupModelScaleRotationMatrix(FbxScene* fbxScene, Model* model);
232 
233  /**
234  * Process FBX scene
235  * @param fbxScene FBX scene
236  * @param model model
237  * @param pathName path name
238  * @param possibleArmatureNodeIds possible armature node ids
239  * @param useBC7TextureCompression use BC7 texture compression
240  */
241  static void processScene(FbxScene* fbxScene, Model* model, const string& pathName, vector<string>& possibleArmatureNodeIds, bool useBC7TextureCompression);
242 
243  /**
244  * Process FBX node
245  * @param fbxNode FBX scene
246  * @param model model
247  * @param parentNode parent node
248  * @param pathName path name
249  * @param possibleArmatureNodeIds possible armature node ids
250  * @param useBC7TextureCompression use BC7 texture compression
251  */
252  static void processNode(FbxNode* fbxNode, Model* model, Node* parentNode, const string& pathName, vector<string>& possibleArmatureNodeIds, bool useBC7TextureCompression);
253 
254  /**
255  * Process FBX mesh node
256  * @param fbxNode FBX node
257  * @param model model
258  * @param parentNode parent node
259  * @param pathName path name
260  * @param useBC7TextureCompression use BC7 texture compression
261  */
262  static Node* processMeshNode(FbxNode* fbxNode, Model* model, Node* parentNode, const string& pathName, bool useBC7TextureCompression);
263 
264  /**
265  * Process FBX skeleton node
266  * @param fbxNode FBX node
267  * @param model model
268  * @param parentNode parent node
269  * @param pathName path name
270  */
271  static Node* processSkeletonNode(FbxNode* fbxNode, Model* model, Node* parentNode, const string& pathName);
272 
273  /**
274  * Process animation
275  * @param fbxNode FBX node
276  * @param fbxStartFrame FBX start frame
277  * @param fbxEndFrame FBX end frame
278  * @param model model
279  * @param frameOffset frame offset
280  */
281  static void processAnimation(FbxNode* fbxNode, const FbxTime& fbxStartFrame, const FbxTime& fbxEndFrame, Model* model, int frameOffset);
282 };
Color 4 definition class.
Definition: Color4.h:18
void Seek(const FbxInt64 &offset, const FbxFile::ESeekPos &seekPos)
Seek.
Definition: FBXReader.h:127
virtual bool Open(void *)
Open stream.
Definition: FBXReader.h:64
virtual bool Close()
Close stream.
Definition: FBXReader.h:73
FBXReaderStream(FbxManager *fbxManager, const vector< uint8_t > *data)
Constructor.
Definition: FBXReader.h:43
virtual void ClearError()
Clear errors, which we dont support.
Definition: FBXReader.h:166
virtual int Read(void *data, int size) const
Read from stream.
Definition: FBXReader.h:100
virtual int Write(const void *data, int size)
Write to stream, which is currently not supported.
Definition: FBXReader.h:91
virtual void SetPosition(long position)
Set position.
Definition: FBXReader.h:152
static void processScene(FbxScene *fbxScene, Model *model, const string &pathName, vector< string > &possibleArmatureNodeIds, bool useBC7TextureCompression)
Process FBX scene.
Definition: FBXReader.cpp:279
static constexpr float BLENDER_AMBIENT_FROM_DIFFUSE_SCALE
Definition: FBXReader.h:203
static void processNode(FbxNode *fbxNode, Model *model, Node *parentNode, const string &pathName, vector< string > &possibleArmatureNodeIds, bool useBC7TextureCompression)
Process FBX node.
Definition: FBXReader.cpp:287
static UpVector * getSceneUpVector(FbxScene *fbxScene)
Get scene up vector.
Definition: FBXReader.cpp:249
static Node * processSkeletonNode(FbxNode *fbxNode, Model *model, Node *parentNode, const string &pathName)
Process FBX skeleton node.
Definition: FBXReader.cpp:958
static constexpr float BLENDER_DIFFUSE_SCALE
Definition: FBXReader.h:204
static Node * processMeshNode(FbxNode *fbxNode, Model *model, Node *parentNode, const string &pathName, bool useBC7TextureCompression)
Process FBX mesh node.
Definition: FBXReader.cpp:351
static STATIC_DLL_IMPEXT const Color4 BLENDER_AMBIENT_NONE
Definition: FBXReader.h:202
static void setupModelScaleRotationMatrix(FbxScene *fbxScene, Model *model)
Set up model import scale maxtrix.
Definition: FBXReader.cpp:274
static void setupModelImportRotationMatrix(Model *model)
Set up model import rotation maxtrix.
Definition: FBXReader.cpp:265
static RotationOrder * getSceneRotationOrder(FbxScene *fbxScene)
Get scene rotation order.
Definition: FBXReader.cpp:228
static void processAnimation(FbxNode *fbxNode, const FbxTime &fbxStartFrame, const FbxTime &fbxEndFrame, Model *model, int frameOffset)
Process animation.
Definition: FBXReader.cpp:962
static Model * read(const string &pathName, const string &fileName, bool useBC7TextureCompression=true)
Reads FBX file.
Definition: FBXReader.cpp:71
Representation of a 3D model.
Definition: Model.h:35
Model node.
Definition: Node.h:32
Represents rotation orders of a model.
Definition: RotationOrder.h:23
Model up vector.
Definition: UpVector.h:20
Definition: fwd-tdme.h:4
#define STATIC_DLL_IMPEXT
Definition: tdme.h:15