You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

605 line
15 KiB

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