FBXDocument.h 37 KB

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