ColladaHelper.h 14 KB

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