ColladaHelper.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. /** Helper structures for the Collada loader */
  2. /*
  3. Open Asset Import Library (assimp)
  4. ----------------------------------------------------------------------
  5. Copyright (c) 2006-2019, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the
  9. following conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ----------------------------------------------------------------------
  33. */
  34. #ifndef AI_COLLADAHELPER_H_INC
  35. #define AI_COLLADAHELPER_H_INC
  36. #include <map>
  37. #include <vector>
  38. #include <stdint.h>
  39. #include <assimp/light.h>
  40. #include <assimp/mesh.h>
  41. #include <assimp/material.h>
  42. struct aiMaterial;
  43. namespace Assimp {
  44. namespace Collada {
  45. /** Collada file versions which evolved during the years ... */
  46. enum FormatVersion
  47. {
  48. FV_1_5_n,
  49. FV_1_4_n,
  50. FV_1_3_n
  51. };
  52. /** Transformation types that can be applied to a node */
  53. enum TransformType
  54. {
  55. TF_LOOKAT,
  56. TF_ROTATE,
  57. TF_TRANSLATE,
  58. TF_SCALE,
  59. TF_SKEW,
  60. TF_MATRIX
  61. };
  62. /** Different types of input data to a vertex or face */
  63. enum InputType
  64. {
  65. IT_Invalid,
  66. IT_Vertex, // special type for per-index data referring to the <vertices> element carrying the per-vertex data.
  67. IT_Position,
  68. IT_Normal,
  69. IT_Texcoord,
  70. IT_Color,
  71. IT_Tangent,
  72. IT_Bitangent
  73. };
  74. /** Supported controller types */
  75. enum ControllerType
  76. {
  77. Skin,
  78. Morph
  79. };
  80. /** Supported morph methods */
  81. enum MorphMethod
  82. {
  83. Normalized,
  84. Relative
  85. };
  86. /** Contains all data for one of the different transformation types */
  87. struct Transform
  88. {
  89. std::string mID; ///< SID of the transform step, by which anim channels address their target node
  90. TransformType mType;
  91. ai_real f[16]; ///< Interpretation of data depends on the type of the transformation
  92. };
  93. /** A collada camera. */
  94. struct Camera
  95. {
  96. Camera()
  97. : mOrtho (false)
  98. , mHorFov (10e10f)
  99. , mVerFov (10e10f)
  100. , mAspect (10e10f)
  101. , mZNear (0.1f)
  102. , mZFar (1000.f)
  103. {}
  104. // Name of camera
  105. std::string mName;
  106. // True if it is an orthografic camera
  107. bool mOrtho;
  108. //! Horizontal field of view in degrees
  109. ai_real mHorFov;
  110. //! Vertical field of view in degrees
  111. ai_real mVerFov;
  112. //! Screen aspect
  113. ai_real mAspect;
  114. //! Near& far z
  115. ai_real mZNear, mZFar;
  116. };
  117. #define ASSIMP_COLLADA_LIGHT_ANGLE_NOT_SET 1e9f
  118. /** A collada light source. */
  119. struct Light
  120. {
  121. Light()
  122. : mType (aiLightSource_UNDEFINED)
  123. , mAttConstant (1.f)
  124. , mAttLinear (0.f)
  125. , mAttQuadratic (0.f)
  126. , mFalloffAngle (180.f)
  127. , mFalloffExponent (0.f)
  128. , mPenumbraAngle (ASSIMP_COLLADA_LIGHT_ANGLE_NOT_SET)
  129. , mOuterAngle (ASSIMP_COLLADA_LIGHT_ANGLE_NOT_SET)
  130. , mIntensity (1.f)
  131. {}
  132. //! Type of the light source aiLightSourceType + ambient
  133. unsigned int mType;
  134. //! Color of the light
  135. aiColor3D mColor;
  136. //! Light attenuation
  137. ai_real mAttConstant,mAttLinear,mAttQuadratic;
  138. //! Spot light falloff
  139. ai_real mFalloffAngle;
  140. ai_real mFalloffExponent;
  141. // -----------------------------------------------------
  142. // FCOLLADA extension from here
  143. //! ... related stuff from maja and max extensions
  144. ai_real mPenumbraAngle;
  145. ai_real mOuterAngle;
  146. //! Common light intensity
  147. ai_real mIntensity;
  148. };
  149. /** Short vertex index description */
  150. struct InputSemanticMapEntry
  151. {
  152. InputSemanticMapEntry()
  153. : mSet(0)
  154. , mType(IT_Invalid)
  155. {}
  156. //! Index of set, optional
  157. unsigned int mSet;
  158. //! Type of referenced vertex input
  159. InputType mType;
  160. };
  161. /** Table to map from effect to vertex input semantics */
  162. struct SemanticMappingTable
  163. {
  164. //! Name of material
  165. std::string mMatName;
  166. //! List of semantic map commands, grouped by effect semantic name
  167. std::map<std::string, InputSemanticMapEntry> mMap;
  168. //! For std::find
  169. bool operator == (const std::string& s) const {
  170. return s == mMatName;
  171. }
  172. };
  173. /** A reference to a mesh inside a node, including materials assigned to the various subgroups.
  174. * The ID refers to either a mesh or a controller which specifies the mesh
  175. */
  176. struct MeshInstance
  177. {
  178. ///< ID of the mesh or controller to be instanced
  179. std::string mMeshOrController;
  180. ///< Map of materials by the subgroup ID they're applied to
  181. std::map<std::string, SemanticMappingTable> mMaterials;
  182. };
  183. /** A reference to a camera inside a node*/
  184. struct CameraInstance
  185. {
  186. ///< ID of the camera
  187. std::string mCamera;
  188. };
  189. /** A reference to a light inside a node*/
  190. struct LightInstance
  191. {
  192. ///< ID of the camera
  193. std::string mLight;
  194. };
  195. /** A reference to a node inside a node*/
  196. struct NodeInstance
  197. {
  198. ///< ID of the node
  199. std::string mNode;
  200. };
  201. /** A node in a scene hierarchy */
  202. struct Node
  203. {
  204. std::string mName;
  205. std::string mID;
  206. std::string mSID;
  207. Node* mParent;
  208. std::vector<Node*> mChildren;
  209. /** Operations in order to calculate the resulting transformation to parent. */
  210. std::vector<Transform> mTransforms;
  211. /** Meshes at this node */
  212. std::vector<MeshInstance> mMeshes;
  213. /** Lights at this node */
  214. std::vector<LightInstance> mLights;
  215. /** Cameras at this node */
  216. std::vector<CameraInstance> mCameras;
  217. /** Node instances at this node */
  218. std::vector<NodeInstance> mNodeInstances;
  219. /** Root-nodes: Name of primary camera, if any */
  220. std::string mPrimaryCamera;
  221. //! Constructor. Begin with a zero parent
  222. Node()
  223. : mParent( nullptr ){
  224. // empty
  225. }
  226. //! Destructor: delete all children subsequently
  227. ~Node() {
  228. for( std::vector<Node*>::iterator it = mChildren.begin(); it != mChildren.end(); ++it)
  229. delete *it;
  230. }
  231. };
  232. /** Data source array: either floats or strings */
  233. struct Data
  234. {
  235. bool mIsStringArray;
  236. std::vector<ai_real> mValues;
  237. std::vector<std::string> mStrings;
  238. };
  239. /** Accessor to a data array */
  240. struct Accessor
  241. {
  242. size_t mCount; // in number of objects
  243. size_t mSize; // size of an object, in elements (floats or strings, mostly 1)
  244. size_t mOffset; // in number of values
  245. size_t mStride; // Stride in number of values
  246. std::vector<std::string> mParams; // names of the data streams in the accessors. Empty string tells to ignore.
  247. size_t mSubOffset[4]; // Suboffset inside the object for the common 4 elements. For a vector, that's XYZ, for a color RGBA and so on.
  248. // For example, SubOffset[0] denotes which of the values inside the object is the vector X component.
  249. std::string mSource; // URL of the source array
  250. mutable const Data* mData; // Pointer to the source array, if resolved. NULL else
  251. Accessor()
  252. {
  253. mCount = 0; mSize = 0; mOffset = 0; mStride = 0; mData = NULL;
  254. mSubOffset[0] = mSubOffset[1] = mSubOffset[2] = mSubOffset[3] = 0;
  255. }
  256. };
  257. /** A single face in a mesh */
  258. struct Face
  259. {
  260. std::vector<size_t> mIndices;
  261. };
  262. /** An input channel for mesh data, referring to a single accessor */
  263. struct InputChannel
  264. {
  265. InputType mType; // Type of the data
  266. size_t mIndex; // Optional index, if multiple sets of the same data type are given
  267. size_t mOffset; // Index offset in the indices array of per-face indices. Don't ask, can't explain that any better.
  268. std::string mAccessor; // ID of the accessor where to read the actual values from.
  269. mutable const Accessor* mResolved; // Pointer to the accessor, if resolved. NULL else
  270. InputChannel() { mType = IT_Invalid; mIndex = 0; mOffset = 0; mResolved = NULL; }
  271. };
  272. /** Subset of a mesh with a certain material */
  273. struct SubMesh
  274. {
  275. std::string mMaterial; ///< subgroup identifier
  276. size_t mNumFaces; ///< number of faces in this submesh
  277. };
  278. /** Contains data for a single mesh */
  279. struct Mesh
  280. {
  281. Mesh()
  282. {
  283. for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS;++i)
  284. mNumUVComponents[i] = 2;
  285. }
  286. std::string mName;
  287. // just to check if there's some sophisticated addressing involved...
  288. // which we don't support, and therefore should warn about.
  289. std::string mVertexID;
  290. // Vertex data addressed by vertex indices
  291. std::vector<InputChannel> mPerVertexData;
  292. // actual mesh data, assembled on encounter of a <p> element. Verbose format, not indexed
  293. std::vector<aiVector3D> mPositions;
  294. std::vector<aiVector3D> mNormals;
  295. std::vector<aiVector3D> mTangents;
  296. std::vector<aiVector3D> mBitangents;
  297. std::vector<aiVector3D> mTexCoords[AI_MAX_NUMBER_OF_TEXTURECOORDS];
  298. std::vector<aiColor4D> mColors[AI_MAX_NUMBER_OF_COLOR_SETS];
  299. unsigned int mNumUVComponents[AI_MAX_NUMBER_OF_TEXTURECOORDS];
  300. // Faces. Stored are only the number of vertices for each face.
  301. // 1 == point, 2 == line, 3 == triangle, 4+ == poly
  302. std::vector<size_t> mFaceSize;
  303. // Position indices for all faces in the sequence given in mFaceSize -
  304. // necessary for bone weight assignment
  305. std::vector<size_t> mFacePosIndices;
  306. // Submeshes in this mesh, each with a given material
  307. std::vector<SubMesh> mSubMeshes;
  308. };
  309. /** Which type of primitives the ReadPrimitives() function is going to read */
  310. enum PrimitiveType
  311. {
  312. Prim_Invalid,
  313. Prim_Lines,
  314. Prim_LineStrip,
  315. Prim_Triangles,
  316. Prim_TriStrips,
  317. Prim_TriFans,
  318. Prim_Polylist,
  319. Prim_Polygon
  320. };
  321. /** A skeleton controller to deform a mesh with the use of joints */
  322. struct Controller
  323. {
  324. // controller type
  325. ControllerType mType;
  326. // Morphing method if type is Morph
  327. MorphMethod mMethod;
  328. // the URL of the mesh deformed by the controller.
  329. std::string mMeshId;
  330. // accessor URL of the joint names
  331. std::string mJointNameSource;
  332. ///< The bind shape matrix, as array of floats. I'm not sure what this matrix actually describes, but it can't be ignored in all cases
  333. ai_real mBindShapeMatrix[16];
  334. // accessor URL of the joint inverse bind matrices
  335. std::string mJointOffsetMatrixSource;
  336. // input channel: joint names.
  337. InputChannel mWeightInputJoints;
  338. // input channel: joint weights
  339. InputChannel mWeightInputWeights;
  340. // Number of weights per vertex.
  341. std::vector<size_t> mWeightCounts;
  342. // JointIndex-WeightIndex pairs for all vertices
  343. std::vector< std::pair<size_t, size_t> > mWeights;
  344. std::string mMorphTarget;
  345. std::string mMorphWeight;
  346. };
  347. /** A collada material. Pretty much the only member is a reference to an effect. */
  348. struct Material
  349. {
  350. std::string mName;
  351. std::string mEffect;
  352. };
  353. /** Type of the effect param */
  354. enum ParamType
  355. {
  356. Param_Sampler,
  357. Param_Surface
  358. };
  359. /** A param for an effect. Might be of several types, but they all just refer to each other, so I summarize them */
  360. struct EffectParam
  361. {
  362. ParamType mType;
  363. std::string mReference; // to which other thing the param is referring to.
  364. };
  365. /** Shading type supported by the standard effect spec of Collada */
  366. enum ShadeType
  367. {
  368. Shade_Invalid,
  369. Shade_Constant,
  370. Shade_Lambert,
  371. Shade_Phong,
  372. Shade_Blinn
  373. };
  374. /** Represents a texture sampler in collada */
  375. struct Sampler
  376. {
  377. Sampler()
  378. : mWrapU (true)
  379. , mWrapV (true)
  380. , mMirrorU ()
  381. , mMirrorV ()
  382. , mOp (aiTextureOp_Multiply)
  383. , mUVId (UINT_MAX)
  384. , mWeighting (1.f)
  385. , mMixWithPrevious (1.f)
  386. {}
  387. /** Name of image reference
  388. */
  389. std::string mName;
  390. /** Wrap U?
  391. */
  392. bool mWrapU;
  393. /** Wrap V?
  394. */
  395. bool mWrapV;
  396. /** Mirror U?
  397. */
  398. bool mMirrorU;
  399. /** Mirror V?
  400. */
  401. bool mMirrorV;
  402. /** Blend mode
  403. */
  404. aiTextureOp mOp;
  405. /** UV transformation
  406. */
  407. aiUVTransform mTransform;
  408. /** Name of source UV channel
  409. */
  410. std::string mUVChannel;
  411. /** Resolved UV channel index or UINT_MAX if not known
  412. */
  413. unsigned int mUVId;
  414. // OKINO/MAX3D extensions from here
  415. // -------------------------------------------------------
  416. /** Weighting factor
  417. */
  418. ai_real mWeighting;
  419. /** Mixing factor from OKINO
  420. */
  421. ai_real mMixWithPrevious;
  422. };
  423. /** A collada effect. Can contain about anything according to the Collada spec,
  424. but we limit our version to a reasonable subset. */
  425. struct Effect
  426. {
  427. // Shading mode
  428. ShadeType mShadeType;
  429. // Colors
  430. aiColor4D mEmissive, mAmbient, mDiffuse, mSpecular,
  431. mTransparent, mReflective;
  432. // Textures
  433. Sampler mTexEmissive, mTexAmbient, mTexDiffuse, mTexSpecular,
  434. mTexTransparent, mTexBump, mTexReflective;
  435. // Scalar factory
  436. ai_real mShininess, mRefractIndex, mReflectivity;
  437. ai_real mTransparency;
  438. bool mHasTransparency;
  439. bool mRGBTransparency;
  440. bool mInvertTransparency;
  441. // local params referring to each other by their SID
  442. typedef std::map<std::string, Collada::EffectParam> ParamLibrary;
  443. ParamLibrary mParams;
  444. // MAX3D extensions
  445. // ---------------------------------------------------------
  446. // Double-sided?
  447. bool mDoubleSided, mWireframe, mFaceted;
  448. Effect()
  449. : mShadeType (Shade_Phong)
  450. , mEmissive ( 0, 0, 0, 1)
  451. , mAmbient ( 0.1f, 0.1f, 0.1f, 1)
  452. , mDiffuse ( 0.6f, 0.6f, 0.6f, 1)
  453. , mSpecular ( 0.4f, 0.4f, 0.4f, 1)
  454. , mTransparent ( 0, 0, 0, 1)
  455. , mShininess (10.0f)
  456. , mRefractIndex (1.f)
  457. , mReflectivity (0.f)
  458. , mTransparency (1.f)
  459. , mHasTransparency (false)
  460. , mRGBTransparency(false)
  461. , mInvertTransparency(false)
  462. , mDoubleSided (false)
  463. , mWireframe (false)
  464. , mFaceted (false)
  465. {
  466. }
  467. };
  468. /** An image, meaning texture */
  469. struct Image
  470. {
  471. std::string mFileName;
  472. /** If image file name is zero, embedded image data
  473. */
  474. std::vector<uint8_t> mImageData;
  475. /** If image file name is zero, file format of
  476. * embedded image data.
  477. */
  478. std::string mEmbeddedFormat;
  479. };
  480. /** An animation channel. */
  481. struct AnimationChannel
  482. {
  483. /** URL of the data to animate. Could be about anything, but we support only the
  484. * "NodeID/TransformID.SubElement" notation
  485. */
  486. std::string mTarget;
  487. /** Source URL of the time values. Collada calls them "input". Meh. */
  488. std::string mSourceTimes;
  489. /** Source URL of the value values. Collada calls them "output". */
  490. std::string mSourceValues;
  491. /** Source URL of the IN_TANGENT semantic values. */
  492. std::string mInTanValues;
  493. /** Source URL of the OUT_TANGENT semantic values. */
  494. std::string mOutTanValues;
  495. /** Source URL of the INTERPOLATION semantic values. */
  496. std::string mInterpolationValues;
  497. };
  498. /** An animation. Container for 0-x animation channels or 0-x animations */
  499. struct Animation
  500. {
  501. /** Anim name */
  502. std::string mName;
  503. /** the animation channels, if any */
  504. std::vector<AnimationChannel> mChannels;
  505. /** the sub-animations, if any */
  506. std::vector<Animation*> mSubAnims;
  507. /** Destructor */
  508. ~Animation()
  509. {
  510. for( std::vector<Animation*>::iterator it = mSubAnims.begin(); it != mSubAnims.end(); ++it)
  511. delete *it;
  512. }
  513. /** Collect all channels in the animation hierarchy into a single channel list. */
  514. void CollectChannelsRecursively(std::vector<AnimationChannel> &channels)
  515. {
  516. channels.insert(channels.end(), mChannels.begin(), mChannels.end());
  517. for (std::vector<Animation*>::iterator it = mSubAnims.begin(); it != mSubAnims.end(); ++it)
  518. {
  519. Animation *pAnim = (*it);
  520. pAnim->CollectChannelsRecursively(channels);
  521. }
  522. }
  523. /** Combine all single-channel animations' channel into the same (parent) animation channel list. */
  524. void CombineSingleChannelAnimations()
  525. {
  526. CombineSingleChannelAnimationsRecursively(this);
  527. }
  528. void CombineSingleChannelAnimationsRecursively(Animation *pParent)
  529. {
  530. for (std::vector<Animation*>::iterator it = pParent->mSubAnims.begin(); it != pParent->mSubAnims.end();)
  531. {
  532. Animation *anim = *it;
  533. CombineSingleChannelAnimationsRecursively(anim);
  534. if (anim->mChannels.size() == 1)
  535. {
  536. pParent->mChannels.push_back(anim->mChannels[0]);
  537. it = pParent->mSubAnims.erase(it);
  538. delete anim;
  539. }
  540. else
  541. {
  542. ++it;
  543. }
  544. }
  545. }
  546. };
  547. /** Description of a collada animation channel which has been determined to affect the current node */
  548. struct ChannelEntry
  549. {
  550. const Collada::AnimationChannel* mChannel; ///> the source channel
  551. std::string mTargetId;
  552. std::string mTransformId; // the ID of the transformation step of the node which is influenced
  553. size_t mTransformIndex; // Index into the node's transform chain to apply the channel to
  554. size_t mSubElement; // starting index inside the transform data
  555. // resolved data references
  556. const Collada::Accessor* mTimeAccessor; ///> Collada accessor to the time values
  557. const Collada::Data* mTimeData; ///> Source data array for the time values
  558. const Collada::Accessor* mValueAccessor; ///> Collada accessor to the key value values
  559. const Collada::Data* mValueData; ///> Source datat array for the key value values
  560. ChannelEntry()
  561. : mChannel()
  562. , mTransformIndex()
  563. , mSubElement()
  564. , mTimeAccessor()
  565. , mTimeData()
  566. , mValueAccessor()
  567. , mValueData()
  568. {}
  569. };
  570. } // end of namespace Collada
  571. } // end of namespace Assimp
  572. #endif // AI_COLLADAHELPER_H_INC