Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

1394 linhas
34 KiB

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