FBXDocument.h 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2016, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file FBXDocument.h
  34. * @brief FBX DOM
  35. */
  36. #ifndef INCLUDED_AI_FBX_DOCUMENT_H
  37. #define INCLUDED_AI_FBX_DOCUMENT_H
  38. #include <numeric>
  39. #include <stdint.h>
  40. #include <assimp/mesh.h>
  41. #include "FBXProperties.h"
  42. #include "FBXParser.h"
  43. #define _AI_CONCAT(a,b) a ## b
  44. #define AI_CONCAT(a,b) _AI_CONCAT(a,b)
  45. namespace Assimp {
  46. namespace FBX {
  47. class Parser;
  48. class Object;
  49. struct ImportSettings;
  50. class PropertyTable;
  51. class Document;
  52. class Material;
  53. class Geometry;
  54. class Video;
  55. class AnimationCurve;
  56. class AnimationCurveNode;
  57. class AnimationLayer;
  58. class AnimationStack;
  59. class Skin;
  60. class Cluster;
  61. /** Represents a delay-parsed FBX objects. Many objects in the scene
  62. * are not needed by assimp, so it makes no sense to parse them
  63. * upfront. */
  64. class LazyObject
  65. {
  66. public:
  67. LazyObject(uint64_t id, const Element& element, const Document& doc);
  68. ~LazyObject();
  69. public:
  70. const Object* Get(bool dieOnError = false);
  71. template <typename T>
  72. const T* Get(bool dieOnError = false) {
  73. const Object* const ob = Get(dieOnError);
  74. return ob ? dynamic_cast<const T*>(ob) : NULL;
  75. }
  76. uint64_t ID() const {
  77. return id;
  78. }
  79. bool IsBeingConstructed() const {
  80. return (flags & BEING_CONSTRUCTED) != 0;
  81. }
  82. bool FailedToConstruct() const {
  83. return (flags & FAILED_TO_CONSTRUCT) != 0;
  84. }
  85. const Element& GetElement() const {
  86. return element;
  87. }
  88. const Document& GetDocument() const {
  89. return doc;
  90. }
  91. private:
  92. const Document& doc;
  93. const Element& element;
  94. std::unique_ptr<const Object> object;
  95. const uint64_t id;
  96. enum Flags {
  97. BEING_CONSTRUCTED = 0x1,
  98. FAILED_TO_CONSTRUCT = 0x2
  99. };
  100. unsigned int flags;
  101. };
  102. /** Base class for in-memory (DOM) representations of FBX objects */
  103. class Object
  104. {
  105. public:
  106. Object(uint64_t id, const Element& element, const std::string& name);
  107. virtual ~Object();
  108. const Element& SourceElement() const {
  109. return element;
  110. }
  111. const std::string& Name() const {
  112. return name;
  113. }
  114. uint64_t ID() const {
  115. return id;
  116. }
  117. protected:
  118. const Element& element;
  119. const std::string name;
  120. const uint64_t id;
  121. };
  122. /** DOM class for generic FBX NoteAttribute blocks. NoteAttribute's just hold a property table,
  123. * fixed members are added by deriving classes. */
  124. class NodeAttribute : public Object
  125. {
  126. public:
  127. NodeAttribute(uint64_t id, const Element& element, const Document& doc, const std::string& name);
  128. virtual ~NodeAttribute();
  129. const PropertyTable& Props() const {
  130. ai_assert(props.get());
  131. return *props.get();
  132. }
  133. private:
  134. std::shared_ptr<const PropertyTable> props;
  135. };
  136. /** DOM base class for FBX camera settings attached to a node */
  137. class CameraSwitcher : public NodeAttribute
  138. {
  139. public:
  140. CameraSwitcher(uint64_t id, const Element& element, const Document& doc, const std::string& name);
  141. virtual ~CameraSwitcher();
  142. int CameraID() const {
  143. return cameraId;
  144. }
  145. const std::string& CameraName() const {
  146. return cameraName;
  147. }
  148. const std::string& CameraIndexName() const {
  149. return cameraIndexName;
  150. }
  151. private:
  152. int cameraId;
  153. std::string cameraName;
  154. std::string cameraIndexName;
  155. };
  156. #define fbx_stringize(a) #a
  157. #define fbx_simple_property(name, type, default_value) \
  158. type name() const { \
  159. return PropertyGet<type>(Props(), fbx_stringize(name), (default_value)); \
  160. }
  161. // XXX improve logging
  162. #define fbx_simple_enum_property(name, type, default_value) \
  163. type name() const { \
  164. const int ival = PropertyGet<int>(Props(), fbx_stringize(name), static_cast<int>(default_value)); \
  165. if (ival < 0 || ival >= AI_CONCAT(type, _MAX)) { \
  166. ai_assert(static_cast<int>(default_value) >= 0 && static_cast<int>(default_value) < AI_CONCAT(type, _MAX)); \
  167. return static_cast<type>(default_value); \
  168. } \
  169. return static_cast<type>(ival); \
  170. }
  171. /** DOM base class for FBX cameras attached to a node */
  172. class Camera : public NodeAttribute
  173. {
  174. public:
  175. Camera(uint64_t id, const Element& element, const Document& doc, const std::string& name);
  176. virtual ~Camera();
  177. public:
  178. fbx_simple_property(Position, aiVector3D, aiVector3D(0,0,0))
  179. fbx_simple_property(UpVector, aiVector3D, aiVector3D(0,1,0))
  180. fbx_simple_property(InterestPosition, aiVector3D, aiVector3D(0,0,0))
  181. fbx_simple_property(AspectWidth, float, 1.0f)
  182. fbx_simple_property(AspectHeight, float, 1.0f)
  183. fbx_simple_property(FilmWidth, float, 1.0f)
  184. fbx_simple_property(FilmHeight, float, 1.0f)
  185. fbx_simple_property(FilmAspectRatio, float, 1.0f)
  186. fbx_simple_property(ApertureMode, int, 0)
  187. fbx_simple_property(FieldOfView, float, 1.0f)
  188. fbx_simple_property(FocalLength, float, 1.0f)
  189. };
  190. /** DOM base class for FBX null markers attached to a node */
  191. class Null : public NodeAttribute
  192. {
  193. public:
  194. Null(uint64_t id, const Element& element, const Document& doc, const std::string& name);
  195. virtual ~Null();
  196. };
  197. /** DOM base class for FBX limb node markers attached to a node */
  198. class LimbNode : public NodeAttribute
  199. {
  200. public:
  201. LimbNode(uint64_t id, const Element& element, const Document& doc, const std::string& name);
  202. virtual ~LimbNode();
  203. };
  204. /** DOM base class for FBX lights attached to a node */
  205. class Light : public NodeAttribute
  206. {
  207. public:
  208. Light(uint64_t id, const Element& element, const Document& doc, const std::string& name);
  209. virtual ~Light();
  210. public:
  211. enum Type
  212. {
  213. Type_Point,
  214. Type_Directional,
  215. Type_Spot,
  216. Type_Area,
  217. Type_Volume,
  218. Type_MAX // end-of-enum sentinel
  219. };
  220. enum Decay
  221. {
  222. Decay_None,
  223. Decay_Linear,
  224. Decay_Quadratic,
  225. Decay_Cubic,
  226. Decay_MAX // end-of-enum sentinel
  227. };
  228. public:
  229. fbx_simple_property(Color, aiVector3D, aiVector3D(1,1,1))
  230. fbx_simple_enum_property(LightType, Type, 0)
  231. fbx_simple_property(CastLightOnObject, bool, false)
  232. fbx_simple_property(DrawVolumetricLight, bool, true)
  233. fbx_simple_property(DrawGroundProjection, bool, true)
  234. fbx_simple_property(DrawFrontFacingVolumetricLight, bool, false)
  235. fbx_simple_property(Intensity, float, 1.0f)
  236. fbx_simple_property(InnerAngle, float, 0.0f)
  237. fbx_simple_property(OuterAngle, float, 45.0f)
  238. fbx_simple_property(Fog, int, 50)
  239. fbx_simple_enum_property(DecayType, Decay, 0)
  240. fbx_simple_property(DecayStart, int, 0)
  241. fbx_simple_property(FileName, std::string, "")
  242. fbx_simple_property(EnableNearAttenuation, bool, false)
  243. fbx_simple_property(NearAttenuationStart, float, 0.0f)
  244. fbx_simple_property(NearAttenuationEnd, float, 0.0f)
  245. fbx_simple_property(EnableFarAttenuation, bool, false)
  246. fbx_simple_property(FarAttenuationStart, float, 0.0f)
  247. fbx_simple_property(FarAttenuationEnd, float, 0.0f)
  248. fbx_simple_property(CastShadows, bool, true)
  249. fbx_simple_property(ShadowColor, aiVector3D, aiVector3D(0,0,0))
  250. fbx_simple_property(AreaLightShape, int, 0)
  251. fbx_simple_property(LeftBarnDoor, float, 20.0f)
  252. fbx_simple_property(RightBarnDoor, float, 20.0f)
  253. fbx_simple_property(TopBarnDoor, float, 20.0f)
  254. fbx_simple_property(BottomBarnDoor, float, 20.0f)
  255. fbx_simple_property(EnableBarnDoor, bool, true)
  256. };
  257. /** DOM base class for FBX models (even though its semantics are more "node" than "model" */
  258. class Model : public Object
  259. {
  260. public:
  261. Model(uint64_t id, const Element& element, const Document& doc, const std::string& name);
  262. virtual ~Model();
  263. public:
  264. enum RotOrder
  265. {
  266. RotOrder_EulerXYZ = 0,
  267. RotOrder_EulerXZY,
  268. RotOrder_EulerYZX,
  269. RotOrder_EulerYXZ,
  270. RotOrder_EulerZXY,
  271. RotOrder_EulerZYX,
  272. RotOrder_SphericXYZ,
  273. RotOrder_MAX // end-of-enum sentinel
  274. };
  275. enum TransformInheritance
  276. {
  277. TransformInheritance_RrSs = 0,
  278. TransformInheritance_RSrs,
  279. TransformInheritance_Rrs,
  280. TransformInheritance_MAX // end-of-enum sentinel
  281. };
  282. public:
  283. fbx_simple_property(QuaternionInterpolate, int, 0)
  284. fbx_simple_property(RotationOffset, aiVector3D, aiVector3D())
  285. fbx_simple_property(RotationPivot, aiVector3D, aiVector3D())
  286. fbx_simple_property(ScalingOffset, aiVector3D, aiVector3D())
  287. fbx_simple_property(ScalingPivot, aiVector3D, aiVector3D())
  288. fbx_simple_property(TranslationActive, bool, false)
  289. fbx_simple_property(TranslationMin, aiVector3D, aiVector3D())
  290. fbx_simple_property(TranslationMax, aiVector3D, aiVector3D())
  291. fbx_simple_property(TranslationMinX, bool, false)
  292. fbx_simple_property(TranslationMaxX, bool, false)
  293. fbx_simple_property(TranslationMinY, bool, false)
  294. fbx_simple_property(TranslationMaxY, bool, false)
  295. fbx_simple_property(TranslationMinZ, bool, false)
  296. fbx_simple_property(TranslationMaxZ, bool, false)
  297. fbx_simple_enum_property(RotationOrder, RotOrder, 0)
  298. fbx_simple_property(RotationSpaceForLimitOnly, bool, false)
  299. fbx_simple_property(RotationStiffnessX, float, 0.0f)
  300. fbx_simple_property(RotationStiffnessY, float, 0.0f)
  301. fbx_simple_property(RotationStiffnessZ, float, 0.0f)
  302. fbx_simple_property(AxisLen, float, 0.0f)
  303. fbx_simple_property(PreRotation, aiVector3D, aiVector3D())
  304. fbx_simple_property(PostRotation, aiVector3D, aiVector3D())
  305. fbx_simple_property(RotationActive, bool, false)
  306. fbx_simple_property(RotationMin, aiVector3D, aiVector3D())
  307. fbx_simple_property(RotationMax, aiVector3D, aiVector3D())
  308. fbx_simple_property(RotationMinX, bool, false)
  309. fbx_simple_property(RotationMaxX, bool, false)
  310. fbx_simple_property(RotationMinY, bool, false)
  311. fbx_simple_property(RotationMaxY, bool, false)
  312. fbx_simple_property(RotationMinZ, bool, false)
  313. fbx_simple_property(RotationMaxZ, bool, false)
  314. fbx_simple_enum_property(InheritType, TransformInheritance, 0)
  315. fbx_simple_property(ScalingActive, bool, false)
  316. fbx_simple_property(ScalingMin, aiVector3D, aiVector3D())
  317. fbx_simple_property(ScalingMax, aiVector3D, aiVector3D(1.f,1.f,1.f))
  318. fbx_simple_property(ScalingMinX, bool, false)
  319. fbx_simple_property(ScalingMaxX, bool, false)
  320. fbx_simple_property(ScalingMinY, bool, false)
  321. fbx_simple_property(ScalingMaxY, bool, false)
  322. fbx_simple_property(ScalingMinZ, bool, false)
  323. fbx_simple_property(ScalingMaxZ, bool, false)
  324. fbx_simple_property(GeometricTranslation, aiVector3D, aiVector3D())
  325. fbx_simple_property(GeometricRotation, aiVector3D, aiVector3D())
  326. fbx_simple_property(GeometricScaling, aiVector3D, aiVector3D(1.f, 1.f, 1.f))
  327. fbx_simple_property(MinDampRangeX, float, 0.0f)
  328. fbx_simple_property(MinDampRangeY, float, 0.0f)
  329. fbx_simple_property(MinDampRangeZ, float, 0.0f)
  330. fbx_simple_property(MaxDampRangeX, float, 0.0f)
  331. fbx_simple_property(MaxDampRangeY, float, 0.0f)
  332. fbx_simple_property(MaxDampRangeZ, float, 0.0f)
  333. fbx_simple_property(MinDampStrengthX, float, 0.0f)
  334. fbx_simple_property(MinDampStrengthY, float, 0.0f)
  335. fbx_simple_property(MinDampStrengthZ, float, 0.0f)
  336. fbx_simple_property(MaxDampStrengthX, float, 0.0f)
  337. fbx_simple_property(MaxDampStrengthY, float, 0.0f)
  338. fbx_simple_property(MaxDampStrengthZ, float, 0.0f)
  339. fbx_simple_property(PreferredAngleX, float, 0.0f)
  340. fbx_simple_property(PreferredAngleY, float, 0.0f)
  341. fbx_simple_property(PreferredAngleZ, float, 0.0f)
  342. fbx_simple_property(Show, bool, true)
  343. fbx_simple_property(LODBox, bool, false)
  344. fbx_simple_property(Freeze, bool, false)
  345. public:
  346. const std::string& Shading() const {
  347. return shading;
  348. }
  349. const std::string& Culling() const {
  350. return culling;
  351. }
  352. const PropertyTable& Props() const {
  353. ai_assert(props.get());
  354. return *props.get();
  355. }
  356. /** Get material links */
  357. const std::vector<const Material*>& GetMaterials() const {
  358. return materials;
  359. }
  360. /** Get geometry links */
  361. const std::vector<const Geometry*>& GetGeometry() const {
  362. return geometry;
  363. }
  364. /** Get node attachments */
  365. const std::vector<const NodeAttribute*>& GetAttributes() const {
  366. return attributes;
  367. }
  368. /** convenience method to check if the node has a Null node marker */
  369. bool IsNull() const;
  370. private:
  371. void ResolveLinks(const Element& element, const Document& doc);
  372. private:
  373. std::vector<const Material*> materials;
  374. std::vector<const Geometry*> geometry;
  375. std::vector<const NodeAttribute*> attributes;
  376. std::string shading;
  377. std::string culling;
  378. std::shared_ptr<const PropertyTable> props;
  379. };
  380. /** DOM class for generic FBX textures */
  381. class Texture : public Object
  382. {
  383. public:
  384. Texture(uint64_t id, const Element& element, const Document& doc, const std::string& name);
  385. virtual ~Texture();
  386. public:
  387. const std::string& Type() const {
  388. return type;
  389. }
  390. const std::string& FileName() const {
  391. return fileName;
  392. }
  393. const std::string& RelativeFilename() const {
  394. return relativeFileName;
  395. }
  396. const std::string& AlphaSource() const {
  397. return alphaSource;
  398. }
  399. const aiVector2D& UVTranslation() const {
  400. return uvTrans;
  401. }
  402. const aiVector2D& UVScaling() const {
  403. return uvScaling;
  404. }
  405. const PropertyTable& Props() const {
  406. ai_assert(props.get());
  407. return *props.get();
  408. }
  409. // return a 4-tuple
  410. const unsigned int* Crop() const {
  411. return crop;
  412. }
  413. const Video* Media() const {
  414. return media;
  415. }
  416. private:
  417. aiVector2D uvTrans;
  418. aiVector2D uvScaling;
  419. std::string type;
  420. std::string relativeFileName;
  421. std::string fileName;
  422. std::string alphaSource;
  423. std::shared_ptr<const PropertyTable> props;
  424. unsigned int crop[4];
  425. const Video* media;
  426. };
  427. /** DOM class for layered FBX textures */
  428. class LayeredTexture : public Object
  429. {
  430. public:
  431. LayeredTexture(uint64_t id, const Element& element, const Document& doc, const std::string& name);
  432. virtual ~LayeredTexture();
  433. //Can only be called after construction of the layered texture object due to construction flag.
  434. void fillTexture(const Document& doc);
  435. enum BlendMode
  436. {
  437. BlendMode_Translucent,
  438. BlendMode_Additive,
  439. BlendMode_Modulate,
  440. BlendMode_Modulate2,
  441. BlendMode_Over,
  442. BlendMode_Normal,
  443. BlendMode_Dissolve,
  444. BlendMode_Darken,
  445. BlendMode_ColorBurn,
  446. BlendMode_LinearBurn,
  447. BlendMode_DarkerColor,
  448. BlendMode_Lighten,
  449. BlendMode_Screen,
  450. BlendMode_ColorDodge,
  451. BlendMode_LinearDodge,
  452. BlendMode_LighterColor,
  453. BlendMode_SoftLight,
  454. BlendMode_HardLight,
  455. BlendMode_VividLight,
  456. BlendMode_LinearLight,
  457. BlendMode_PinLight,
  458. BlendMode_HardMix,
  459. BlendMode_Difference,
  460. BlendMode_Exclusion,
  461. BlendMode_Subtract,
  462. BlendMode_Divide,
  463. BlendMode_Hue,
  464. BlendMode_Saturation,
  465. BlendMode_Color,
  466. BlendMode_Luminosity,
  467. BlendMode_Overlay,
  468. BlendMode_BlendModeCount
  469. };
  470. const Texture* getTexture(int index=0) const
  471. {
  472. return textures[index];
  473. }
  474. const int textureCount() const {
  475. return static_cast<int>(textures.size());
  476. }
  477. const BlendMode GetBlendMode() const
  478. {
  479. return blendMode;
  480. }
  481. float Alpha()
  482. {
  483. return alpha;
  484. }
  485. private:
  486. std::vector<const Texture*> textures;
  487. BlendMode blendMode;
  488. float alpha;
  489. };
  490. typedef std::fbx_unordered_map<std::string, const Texture*> TextureMap;
  491. typedef std::fbx_unordered_map<std::string, const LayeredTexture*> LayeredTextureMap;
  492. /** DOM class for generic FBX videos */
  493. class Video : public Object
  494. {
  495. public:
  496. Video(uint64_t id, const Element& element, const Document& doc, const std::string& name);
  497. virtual ~Video();
  498. public:
  499. const std::string& Type() const {
  500. return type;
  501. }
  502. const std::string& FileName() const {
  503. return fileName;
  504. }
  505. const std::string& RelativeFilename() const {
  506. return relativeFileName;
  507. }
  508. const PropertyTable& Props() const {
  509. ai_assert(props.get());
  510. return *props.get();
  511. }
  512. const uint8_t* Content() const {
  513. ai_assert(content);
  514. return content;
  515. }
  516. const uint32_t ContentLength() const {
  517. return contentLength;
  518. }
  519. uint8_t* RelinquishContent() {
  520. uint8_t* ptr = content;
  521. content = 0;
  522. return ptr;
  523. }
  524. private:
  525. std::string type;
  526. std::string relativeFileName;
  527. std::string fileName;
  528. std::shared_ptr<const PropertyTable> props;
  529. uint32_t contentLength;
  530. uint8_t* content;
  531. };
  532. /** DOM class for generic FBX materials */
  533. class Material : public Object
  534. {
  535. public:
  536. Material(uint64_t id, const Element& element, const Document& doc, const std::string& name);
  537. virtual ~Material();
  538. const std::string& GetShadingModel() const {
  539. return shading;
  540. }
  541. bool IsMultilayer() const {
  542. return multilayer;
  543. }
  544. const PropertyTable& Props() const {
  545. ai_assert(props.get());
  546. return *props.get();
  547. }
  548. const TextureMap& Textures() const {
  549. return textures;
  550. }
  551. const LayeredTextureMap& LayeredTextures() const {
  552. return layeredTextures;
  553. }
  554. private:
  555. std::string shading;
  556. bool multilayer;
  557. std::shared_ptr<const PropertyTable> props;
  558. TextureMap textures;
  559. LayeredTextureMap layeredTextures;
  560. };
  561. typedef std::vector<int64_t> KeyTimeList;
  562. typedef std::vector<float> KeyValueList;
  563. /** Represents a FBX animation curve (i.e. a 1-dimensional set of keyframes and values therefor) */
  564. class AnimationCurve : public Object
  565. {
  566. public:
  567. AnimationCurve(uint64_t id, const Element& element, const std::string& name, const Document& doc);
  568. virtual ~AnimationCurve();
  569. /** get list of keyframe positions (time).
  570. * Invariant: |GetKeys()| > 0 */
  571. const KeyTimeList& GetKeys() const {
  572. return keys;
  573. }
  574. /** get list of keyframe values.
  575. * Invariant: |GetKeys()| == |GetValues()| && |GetKeys()| > 0*/
  576. const KeyValueList& GetValues() const {
  577. return values;
  578. }
  579. const std::vector<float>& GetAttributes() const {
  580. return attributes;
  581. }
  582. const std::vector<unsigned int>& GetFlags() const {
  583. return flags;
  584. }
  585. private:
  586. KeyTimeList keys;
  587. KeyValueList values;
  588. std::vector<float> attributes;
  589. std::vector<unsigned int> flags;
  590. };
  591. // property-name -> animation curve
  592. typedef std::map<std::string, const AnimationCurve*> AnimationCurveMap;
  593. /** Represents a FBX animation curve (i.e. a mapping from single animation curves to nodes) */
  594. class AnimationCurveNode : public Object
  595. {
  596. public:
  597. /* the optional white list specifies a list of property names for which the caller
  598. wants animations for. If the curve node does not match one of these, std::range_error
  599. will be thrown. */
  600. AnimationCurveNode(uint64_t id, const Element& element, const std::string& name, const Document& doc,
  601. const char* const * target_prop_whitelist = NULL, size_t whitelist_size = 0);
  602. virtual ~AnimationCurveNode();
  603. const PropertyTable& Props() const {
  604. ai_assert(props.get());
  605. return *props.get();
  606. }
  607. const AnimationCurveMap& Curves() const;
  608. /** Object the curve is assigned to, this can be NULL if the
  609. * target object has no DOM representation or could not
  610. * be read for other reasons.*/
  611. const Object* Target() const {
  612. return target;
  613. }
  614. const Model* TargetAsModel() const {
  615. return dynamic_cast<const Model*>(target);
  616. }
  617. const NodeAttribute* TargetAsNodeAttribute() const {
  618. return dynamic_cast<const NodeAttribute*>(target);
  619. }
  620. /** Property of Target() that is being animated*/
  621. const std::string& TargetProperty() const {
  622. return prop;
  623. }
  624. private:
  625. const Object* target;
  626. std::shared_ptr<const PropertyTable> props;
  627. mutable AnimationCurveMap curves;
  628. std::string prop;
  629. const Document& doc;
  630. };
  631. typedef std::vector<const AnimationCurveNode*> AnimationCurveNodeList;
  632. /** Represents a FBX animation layer (i.e. a list of node animations) */
  633. class AnimationLayer : public Object
  634. {
  635. public:
  636. AnimationLayer(uint64_t id, const Element& element, const std::string& name, const Document& doc);
  637. virtual ~AnimationLayer();
  638. const PropertyTable& Props() const {
  639. ai_assert(props.get());
  640. return *props.get();
  641. }
  642. /* the optional white list specifies a list of property names for which the caller
  643. wants animations for. Curves not matching this list will not be added to the
  644. animation layer. */
  645. AnimationCurveNodeList Nodes(const char* const * target_prop_whitelist = NULL, size_t whitelist_size = 0) const;
  646. private:
  647. std::shared_ptr<const PropertyTable> props;
  648. const Document& doc;
  649. };
  650. typedef std::vector<const AnimationLayer*> AnimationLayerList;
  651. /** Represents a FBX animation stack (i.e. a list of animation layers) */
  652. class AnimationStack : public Object
  653. {
  654. public:
  655. AnimationStack(uint64_t id, const Element& element, const std::string& name, const Document& doc);
  656. virtual ~AnimationStack();
  657. public:
  658. fbx_simple_property(LocalStart, int64_t, 0L)
  659. fbx_simple_property(LocalStop, int64_t, 0L)
  660. fbx_simple_property(ReferenceStart, int64_t, 0L)
  661. fbx_simple_property(ReferenceStop, int64_t, 0L)
  662. const PropertyTable& Props() const {
  663. ai_assert(props.get());
  664. return *props.get();
  665. }
  666. const AnimationLayerList& Layers() const {
  667. return layers;
  668. }
  669. private:
  670. std::shared_ptr<const PropertyTable> props;
  671. AnimationLayerList layers;
  672. };
  673. /** DOM class for deformers */
  674. class Deformer : public Object
  675. {
  676. public:
  677. Deformer(uint64_t id, const Element& element, const Document& doc, const std::string& name);
  678. virtual ~Deformer();
  679. const PropertyTable& Props() const {
  680. ai_assert(props.get());
  681. return *props.get();
  682. }
  683. private:
  684. std::shared_ptr<const PropertyTable> props;
  685. };
  686. typedef std::vector<float> WeightArray;
  687. typedef std::vector<unsigned int> WeightIndexArray;
  688. /** DOM class for skin deformer clusters (aka subdeformers) */
  689. class Cluster : public Deformer
  690. {
  691. public:
  692. Cluster(uint64_t id, const Element& element, const Document& doc, const std::string& name);
  693. virtual ~Cluster();
  694. /** get the list of deformer weights associated with this cluster.
  695. * Use #GetIndices() to get the associated vertices. Both arrays
  696. * have the same size (and may also be empty). */
  697. const WeightArray& GetWeights() const {
  698. return weights;
  699. }
  700. /** get indices into the vertex data of the geometry associated
  701. * with this cluster. Use #GetWeights() to get the associated weights.
  702. * Both arrays have the same size (and may also be empty). */
  703. const WeightIndexArray& GetIndices() const {
  704. return indices;
  705. }
  706. /** */
  707. const aiMatrix4x4& Transform() const {
  708. return transform;
  709. }
  710. const aiMatrix4x4& TransformLink() const {
  711. return transformLink;
  712. }
  713. const Model* TargetNode() const {
  714. return node;
  715. }
  716. private:
  717. WeightArray weights;
  718. WeightIndexArray indices;
  719. aiMatrix4x4 transform;
  720. aiMatrix4x4 transformLink;
  721. const Model* node;
  722. };
  723. /** DOM class for skin deformers */
  724. class Skin : public Deformer
  725. {
  726. public:
  727. Skin(uint64_t id, const Element& element, const Document& doc, const std::string& name);
  728. virtual ~Skin();
  729. float DeformAccuracy() const {
  730. return accuracy;
  731. }
  732. const std::vector<const Cluster*>& Clusters() const {
  733. return clusters;
  734. }
  735. private:
  736. float accuracy;
  737. std::vector<const Cluster*> clusters;
  738. };
  739. /** Represents a link between two FBX objects. */
  740. class Connection
  741. {
  742. public:
  743. Connection(uint64_t insertionOrder, uint64_t src, uint64_t dest, const std::string& prop, const Document& doc);
  744. ~Connection();
  745. // note: a connection ensures that the source and dest objects exist, but
  746. // not that they have DOM representations, so the return value of one of
  747. // these functions can still be NULL.
  748. const Object* SourceObject() const;
  749. const Object* DestinationObject() const;
  750. // these, however, are always guaranteed to be valid
  751. LazyObject& LazySourceObject() const;
  752. LazyObject& LazyDestinationObject() const;
  753. /** return the name of the property the connection is attached to.
  754. * this is an empty string for object to object (OO) connections. */
  755. const std::string& PropertyName() const {
  756. return prop;
  757. }
  758. uint64_t InsertionOrder() const {
  759. return insertionOrder;
  760. }
  761. int CompareTo(const Connection* c) const {
  762. ai_assert( NULL != c );
  763. // note: can't subtract because this would overflow uint64_t
  764. if(InsertionOrder() > c->InsertionOrder()) {
  765. return 1;
  766. }
  767. else if(InsertionOrder() < c->InsertionOrder()) {
  768. return -1;
  769. }
  770. return 0;
  771. }
  772. bool Compare(const Connection* c) const {
  773. ai_assert( NULL != c );
  774. return InsertionOrder() < c->InsertionOrder();
  775. }
  776. public:
  777. uint64_t insertionOrder;
  778. const std::string prop;
  779. uint64_t src, dest;
  780. const Document& doc;
  781. };
  782. // XXX again, unique_ptr would be useful. shared_ptr is too
  783. // bloated since the objects have a well-defined single owner
  784. // during their entire lifetime (Document). FBX files have
  785. // up to many thousands of objects (most of which we never use),
  786. // so the memory overhead for them should be kept at a minimum.
  787. typedef std::map<uint64_t, LazyObject*> ObjectMap;
  788. typedef std::fbx_unordered_map<std::string, std::shared_ptr<const PropertyTable> > PropertyTemplateMap;
  789. typedef std::multimap<uint64_t, const Connection*> ConnectionMap;
  790. /** DOM class for global document settings, a single instance per document can
  791. * be accessed via Document.Globals(). */
  792. class FileGlobalSettings
  793. {
  794. public:
  795. FileGlobalSettings(const Document& doc, std::shared_ptr<const PropertyTable> props);
  796. ~FileGlobalSettings();
  797. const PropertyTable& Props() const {
  798. ai_assert(props.get());
  799. return *props.get();
  800. }
  801. const Document& GetDocument() const {
  802. return doc;
  803. }
  804. fbx_simple_property(UpAxis, int, 1)
  805. fbx_simple_property(UpAxisSign, int, 1)
  806. fbx_simple_property(FrontAxis, int, 2)
  807. fbx_simple_property(FrontAxisSign, int, 1)
  808. fbx_simple_property(CoordAxis, int, 0)
  809. fbx_simple_property(CoordAxisSign, int, 1)
  810. fbx_simple_property(OriginalUpAxis, int, 0)
  811. fbx_simple_property(OriginalUpAxisSign, int, 1)
  812. fbx_simple_property(UnitScaleFactor, double, 1)
  813. fbx_simple_property(OriginalUnitScaleFactor, double, 1)
  814. fbx_simple_property(AmbientColor, aiVector3D, aiVector3D(0,0,0))
  815. fbx_simple_property(DefaultCamera, std::string, "")
  816. enum FrameRate {
  817. FrameRate_DEFAULT = 0,
  818. FrameRate_120 = 1,
  819. FrameRate_100 = 2,
  820. FrameRate_60 = 3,
  821. FrameRate_50 = 4,
  822. FrameRate_48 = 5,
  823. FrameRate_30 = 6,
  824. FrameRate_30_DROP = 7,
  825. FrameRate_NTSC_DROP_FRAME = 8,
  826. FrameRate_NTSC_FULL_FRAME = 9,
  827. FrameRate_PAL = 10,
  828. FrameRate_CINEMA = 11,
  829. FrameRate_1000 = 12,
  830. FrameRate_CINEMA_ND = 13,
  831. FrameRate_CUSTOM = 14,
  832. FrameRate_MAX// end-of-enum sentinel
  833. };
  834. fbx_simple_enum_property(TimeMode, FrameRate, FrameRate_DEFAULT)
  835. fbx_simple_property(TimeSpanStart, uint64_t, 0L)
  836. fbx_simple_property(TimeSpanStop, uint64_t, 0L)
  837. fbx_simple_property(CustomFrameRate, float, -1.0f)
  838. private:
  839. std::shared_ptr<const PropertyTable> props;
  840. const Document& doc;
  841. };
  842. /** DOM root for a FBX file */
  843. class Document
  844. {
  845. public:
  846. Document(const Parser& parser, const ImportSettings& settings);
  847. ~Document();
  848. LazyObject* GetObject(uint64_t id) const;
  849. bool IsBinary() const {
  850. return parser.IsBinary();
  851. }
  852. unsigned int FBXVersion() const {
  853. return fbxVersion;
  854. }
  855. const std::string& Creator() const {
  856. return creator;
  857. }
  858. // elements (in this order): Year, Month, Day, Hour, Second, Millisecond
  859. const unsigned int* CreationTimeStamp() const {
  860. return creationTimeStamp;
  861. }
  862. const FileGlobalSettings& GlobalSettings() const {
  863. ai_assert(globals.get());
  864. return *globals.get();
  865. }
  866. const PropertyTemplateMap& Templates() const {
  867. return templates;
  868. }
  869. const ObjectMap& Objects() const {
  870. return objects;
  871. }
  872. const ImportSettings& Settings() const {
  873. return settings;
  874. }
  875. const ConnectionMap& ConnectionsBySource() const {
  876. return src_connections;
  877. }
  878. const ConnectionMap& ConnectionsByDestination() const {
  879. return dest_connections;
  880. }
  881. // note: the implicit rule in all DOM classes is to always resolve
  882. // from destination to source (since the FBX object hierarchy is,
  883. // with very few exceptions, a DAG, this avoids cycles). In all
  884. // cases that may involve back-facing edges in the object graph,
  885. // use LazyObject::IsBeingConstructed() to check.
  886. std::vector<const Connection*> GetConnectionsBySourceSequenced(uint64_t source) const;
  887. std::vector<const Connection*> GetConnectionsByDestinationSequenced(uint64_t dest) const;
  888. std::vector<const Connection*> GetConnectionsBySourceSequenced(uint64_t source, const char* classname) const;
  889. std::vector<const Connection*> GetConnectionsByDestinationSequenced(uint64_t dest, const char* classname) const;
  890. std::vector<const Connection*> GetConnectionsBySourceSequenced(uint64_t source,
  891. const char* const* classnames, size_t count) const;
  892. std::vector<const Connection*> GetConnectionsByDestinationSequenced(uint64_t dest,
  893. const char* const* classnames,
  894. size_t count) const;
  895. const std::vector<const AnimationStack*>& AnimationStacks() const;
  896. private:
  897. std::vector<const Connection*> GetConnectionsSequenced(uint64_t id, const ConnectionMap&) const;
  898. std::vector<const Connection*> GetConnectionsSequenced(uint64_t id, bool is_src,
  899. const ConnectionMap&,
  900. const char* const* classnames,
  901. size_t count) const;
  902. private:
  903. void ReadHeader();
  904. void ReadObjects();
  905. void ReadPropertyTemplates();
  906. void ReadConnections();
  907. void ReadGlobalSettings();
  908. private:
  909. const ImportSettings& settings;
  910. ObjectMap objects;
  911. const Parser& parser;
  912. PropertyTemplateMap templates;
  913. ConnectionMap src_connections;
  914. ConnectionMap dest_connections;
  915. unsigned int fbxVersion;
  916. std::string creator;
  917. unsigned int creationTimeStamp[7];
  918. std::vector<uint64_t> animationStacks;
  919. mutable std::vector<const AnimationStack*> animationStacksResolved;
  920. std::unique_ptr<FileGlobalSettings> globals;
  921. };
  922. } // Namespace FBX
  923. } // Namespace Assimp
  924. #endif // INCLUDED_AI_FBX_DOCUMENT_H