ColladaHelper.h 18 KB

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