Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

682 рядки
15 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. #ifndef AI_OGRESTRUCTS_H_INC
  34. #define AI_OGRESTRUCTS_H_INC
  35. #ifndef ASSIMP_BUILD_NO_OGRE_IMPORTER
  36. #include "AssimpPCH.h"
  37. #include "MemoryIOWrapper.h"
  38. /** @note Parts of this implementation, for example enums, deserialization constants and logic
  39. has been copied directly with minor modifications from the MIT licensed Ogre3D code base.
  40. See more from https://bitbucket.org/sinbad/ogre. */
  41. namespace Assimp
  42. {
  43. namespace Ogre
  44. {
  45. // Forward decl
  46. class Mesh;
  47. class MeshXml;
  48. class SubMesh;
  49. class SubMeshXml;
  50. class Skeleton;
  51. #define OGRE_SAFE_DELETE(p) delete p; p=0;
  52. // Typedefs
  53. typedef Assimp::MemoryIOStream MemoryStream;
  54. typedef boost::shared_ptr<MemoryStream> MemoryStreamPtr;
  55. typedef std::map<uint16_t, MemoryStreamPtr> VertexBufferBindings;
  56. // Ogre Vertex Element
  57. class VertexElement
  58. {
  59. public:
  60. /// Vertex element semantics, used to identify the meaning of vertex buffer contents
  61. enum Semantic {
  62. /// Position, 3 reals per vertex
  63. VES_POSITION = 1,
  64. /// Blending weights
  65. VES_BLEND_WEIGHTS = 2,
  66. /// Blending indices
  67. VES_BLEND_INDICES = 3,
  68. /// Normal, 3 reals per vertex
  69. VES_NORMAL = 4,
  70. /// Diffuse colours
  71. VES_DIFFUSE = 5,
  72. /// Specular colours
  73. VES_SPECULAR = 6,
  74. /// Texture coordinates
  75. VES_TEXTURE_COORDINATES = 7,
  76. /// Binormal (Y axis if normal is Z)
  77. VES_BINORMAL = 8,
  78. /// Tangent (X axis if normal is Z)
  79. VES_TANGENT = 9,
  80. /// The number of VertexElementSemantic elements (note - the first value VES_POSITION is 1)
  81. VES_COUNT = 9
  82. };
  83. /// Vertex element type, used to identify the base types of the vertex contents
  84. enum Type
  85. {
  86. VET_FLOAT1 = 0,
  87. VET_FLOAT2 = 1,
  88. VET_FLOAT3 = 2,
  89. VET_FLOAT4 = 3,
  90. /// alias to more specific colour type - use the current rendersystem's colour packing
  91. VET_COLOUR = 4,
  92. VET_SHORT1 = 5,
  93. VET_SHORT2 = 6,
  94. VET_SHORT3 = 7,
  95. VET_SHORT4 = 8,
  96. VET_UBYTE4 = 9,
  97. /// D3D style compact colour
  98. VET_COLOUR_ARGB = 10,
  99. /// GL style compact colour
  100. VET_COLOUR_ABGR = 11,
  101. VET_DOUBLE1 = 12,
  102. VET_DOUBLE2 = 13,
  103. VET_DOUBLE3 = 14,
  104. VET_DOUBLE4 = 15,
  105. VET_USHORT1 = 16,
  106. VET_USHORT2 = 17,
  107. VET_USHORT3 = 18,
  108. VET_USHORT4 = 19,
  109. VET_INT1 = 20,
  110. VET_INT2 = 21,
  111. VET_INT3 = 22,
  112. VET_INT4 = 23,
  113. VET_UINT1 = 24,
  114. VET_UINT2 = 25,
  115. VET_UINT3 = 26,
  116. VET_UINT4 = 27
  117. };
  118. VertexElement();
  119. /// Size of the vertex element in bytes.
  120. size_t Size() const;
  121. /// Count of components in this element, eg. VET_FLOAT3 return 3.
  122. size_t ComponentCount() const;
  123. /// Type as string.
  124. std::string TypeToString();
  125. /// Semantic as string.
  126. std::string SemanticToString();
  127. static size_t TypeSize(Type type);
  128. static size_t ComponentCount(Type type);
  129. static std::string TypeToString(Type type);
  130. static std::string SemanticToString(Semantic semantic);
  131. uint16_t index;
  132. uint16_t source;
  133. uint16_t offset;
  134. Type type;
  135. Semantic semantic;
  136. };
  137. typedef std::vector<VertexElement> VertexElementList;
  138. /// Ogre Vertex Bone Assignment
  139. struct VertexBoneAssignment
  140. {
  141. uint32_t vertexIndex;
  142. uint16_t boneIndex;
  143. float weight;
  144. };
  145. typedef std::vector<VertexBoneAssignment> VertexBoneAssignmentList;
  146. typedef std::map<uint32_t, VertexBoneAssignmentList > VertexBoneAssignmentsMap;
  147. typedef std::map<uint16_t, std::vector<aiVertexWeight> > AssimpVertexBoneWeightList;
  148. // Ogre Vertex Data interface, inherited by the binary and XML implementations.
  149. class IVertexData
  150. {
  151. public:
  152. IVertexData();
  153. /// Returns if bone assignments are available.
  154. bool HasBoneAssignments() const;
  155. /// Add vertex mapping from old to new index.
  156. void AddVertexMapping(uint32_t oldIndex, uint32_t newIndex);
  157. /// Returns re-mapped bone assignments.
  158. /** @note Uses mappings added via AddVertexMapping. */
  159. AssimpVertexBoneWeightList AssimpBoneWeights(size_t vertices);
  160. /// Returns a set of bone indexes that are referenced by bone assignments (weights).
  161. std::set<uint16_t> ReferencedBonesByWeights() const;
  162. /// Vertex count.
  163. uint32_t count;
  164. /// Bone assignments.
  165. VertexBoneAssignmentList boneAssignments;
  166. private:
  167. void BoneAssignmentsForVertex(uint32_t currentIndex, uint32_t newIndex, VertexBoneAssignmentList &dest) const;
  168. std::map<uint32_t, std::vector<uint32_t> > vertexIndexMapping;
  169. VertexBoneAssignmentsMap boneAssignmentsMap;
  170. };
  171. // Ogre Vertex Data
  172. class VertexData : public IVertexData
  173. {
  174. public:
  175. VertexData();
  176. ~VertexData();
  177. /// Releases all memory that this data structure owns.
  178. void Reset();
  179. /// Get vertex size for @c source.
  180. uint32_t VertexSize(uint16_t source) const;
  181. /// Get vertex buffer for @c source.
  182. MemoryStream *VertexBuffer(uint16_t source);
  183. /// Get vertex element for @c semantic for @c index.
  184. VertexElement *GetVertexElement(VertexElement::Semantic semantic, uint16_t index = 0);
  185. /// Vertex elements.
  186. VertexElementList vertexElements;
  187. /// Vertex buffers mapped to bind index.
  188. VertexBufferBindings vertexBindings;
  189. };
  190. // Ogre Index Data
  191. class IndexData
  192. {
  193. public:
  194. IndexData();
  195. ~IndexData();
  196. /// Releases all memory that this data structure owns.
  197. void Reset();
  198. /// Index size in bytes.
  199. size_t IndexSize() const;
  200. /// Face size in bytes.
  201. size_t FaceSize() const;
  202. /// Index count.
  203. uint32_t count;
  204. /// Face count.
  205. uint32_t faceCount;
  206. /// If has 32-bit indexes.
  207. bool is32bit;
  208. /// Index buffer.
  209. MemoryStreamPtr buffer;
  210. };
  211. /// Ogre Pose
  212. class Pose
  213. {
  214. public:
  215. struct Vertex
  216. {
  217. uint32_t index;
  218. aiVector3D offset;
  219. aiVector3D normal;
  220. };
  221. typedef std::map<uint32_t, Vertex> PoseVertexMap;
  222. Pose() : target(0), hasNormals(false) {}
  223. /// Name.
  224. std::string name;
  225. /// Target.
  226. uint16_t target;
  227. /// Does vertices map have normals.
  228. bool hasNormals;
  229. /// Vertex offset and normals.
  230. PoseVertexMap vertices;
  231. };
  232. typedef std::vector<Pose*> PoseList;
  233. /// Ogre Pose Key Frame Ref
  234. struct PoseRef
  235. {
  236. uint16_t index;
  237. float influence;
  238. };
  239. typedef std::vector<PoseRef> PoseRefList;
  240. /// Ogre Pose Key Frame
  241. struct PoseKeyFrame
  242. {
  243. /// Time position in the animation.
  244. float timePos;
  245. PoseRefList references;
  246. };
  247. typedef std::vector<PoseKeyFrame> PoseKeyFrameList;
  248. /// Ogre Morph Key Frame
  249. struct MorphKeyFrame
  250. {
  251. /// Time position in the animation.
  252. float timePos;
  253. MemoryStreamPtr buffer;
  254. };
  255. typedef std::vector<MorphKeyFrame> MorphKeyFrameList;
  256. /// Ogre animation key frame
  257. struct TransformKeyFrame
  258. {
  259. TransformKeyFrame();
  260. aiMatrix4x4 Transform();
  261. float timePos;
  262. aiQuaternion rotation;
  263. aiVector3D position;
  264. aiVector3D scale;
  265. };
  266. typedef std::vector<TransformKeyFrame> TransformKeyFrameList;
  267. /// Ogre Animation Track
  268. struct VertexAnimationTrack
  269. {
  270. enum Type
  271. {
  272. /// No animation
  273. VAT_NONE = 0,
  274. /// Morph animation is made up of many interpolated snapshot keyframes
  275. VAT_MORPH = 1,
  276. /// Pose animation is made up of a single delta pose keyframe
  277. VAT_POSE = 2,
  278. /// Keyframe that has its on pos, rot and scale for a time position
  279. VAT_TRANSFORM = 3
  280. };
  281. VertexAnimationTrack();
  282. /// Convert to Assimp node animation.
  283. aiNodeAnim *ConvertToAssimpAnimationNode(Skeleton *skeleton);
  284. // Animation type.
  285. Type type;
  286. /// Vertex data target.
  287. /** 0 == shared geometry
  288. >0 == submesh index + 1 */
  289. uint16_t target;
  290. /// Only valid for VAT_TRANSFORM.
  291. std::string boneName;
  292. /// Only one of these will contain key frames, depending on the type enum.
  293. PoseKeyFrameList poseKeyFrames;
  294. MorphKeyFrameList morphKeyFrames;
  295. TransformKeyFrameList transformKeyFrames;
  296. };
  297. typedef std::vector<VertexAnimationTrack> VertexAnimationTrackList;
  298. /// Ogre Animation
  299. class Animation
  300. {
  301. public:
  302. Animation(Skeleton *parent);
  303. Animation(Mesh *parent);
  304. /// Returns the associated vertex data for a track in this animation.
  305. /** @note Only valid to call when parent Mesh is set. */
  306. VertexData *AssociatedVertexData(VertexAnimationTrack *track) const;
  307. /// Convert to Assimp animation.
  308. aiAnimation *ConvertToAssimpAnimation();
  309. /// Parent mesh.
  310. /** @note Set only when animation is read from a mesh. */
  311. Mesh *parentMesh;
  312. /// Parent skeleton.
  313. /** @note Set only when animation is read from a skeleton. */
  314. Skeleton *parentSkeleton;
  315. /// Animation name.
  316. std::string name;
  317. /// Base animation name.
  318. std::string baseName;
  319. /// Length in seconds.
  320. float length;
  321. /// Base animation key time.
  322. float baseTime;
  323. /// Animation tracks.
  324. VertexAnimationTrackList tracks;
  325. };
  326. typedef std::vector<Animation*> AnimationList;
  327. /// Ogre Bone
  328. class Bone
  329. {
  330. public:
  331. Bone();
  332. /// Returns if this bone is parented.
  333. bool IsParented() const;
  334. /// Parent index as uint16_t. Internally int32_t as -1 means unparented.
  335. uint16_t ParentId() const;
  336. /// Add child bone.
  337. void AddChild(Bone *bone);
  338. /// Calculates the world matrix for bone and its children.
  339. void CalculateWorldMatrixAndDefaultPose(Skeleton *skeleton);
  340. /// Convert to Assimp node (animation nodes).
  341. aiNode *ConvertToAssimpNode(Skeleton *parent, aiNode *parentNode = 0);
  342. /// Convert to Assimp bone (mesh bones).
  343. aiBone *ConvertToAssimpBone(Skeleton *parent, const std::vector<aiVertexWeight> &boneWeights);
  344. uint16_t id;
  345. std::string name;
  346. Bone *parent;
  347. int32_t parentId;
  348. std::vector<uint16_t> children;
  349. aiVector3D position;
  350. aiQuaternion rotation;
  351. aiVector3D scale;
  352. aiMatrix4x4 worldMatrix;
  353. aiMatrix4x4 defaultPose;
  354. };
  355. typedef std::vector<Bone*> BoneList;
  356. /// Ogre Skeleton
  357. class Skeleton
  358. {
  359. public:
  360. enum BlendMode
  361. {
  362. /// Animations are applied by calculating a weighted average of all animations
  363. ANIMBLEND_AVERAGE = 0,
  364. /// Animations are applied by calculating a weighted cumulative total
  365. ANIMBLEND_CUMULATIVE = 1
  366. };
  367. Skeleton();
  368. ~Skeleton();
  369. /// Releases all memory that this data structure owns.
  370. void Reset();
  371. /// Returns unparented root bones.
  372. BoneList RootBones() const;
  373. /// Returns number of unparented root bones.
  374. size_t NumRootBones() const;
  375. /// Get bone by name.
  376. Bone *BoneByName(const std::string &name) const;
  377. /// Get bone by id.
  378. Bone *BoneById(uint16_t id) const;
  379. BoneList bones;
  380. AnimationList animations;
  381. /// @todo Take blend mode into account, but where?
  382. BlendMode blendMode;
  383. };
  384. /// Ogre Sub Mesh interface, inherited by the binary and XML implementations.
  385. class ISubMesh
  386. {
  387. public:
  388. /// @note Full list of Ogre types, not all of them are supported and exposed to Assimp.
  389. enum OperationType
  390. {
  391. /// A list of points, 1 vertex per point
  392. OT_POINT_LIST = 1,
  393. /// A list of lines, 2 vertices per line
  394. OT_LINE_LIST = 2,
  395. /// A strip of connected lines, 1 vertex per line plus 1 start vertex
  396. OT_LINE_STRIP = 3,
  397. /// A list of triangles, 3 vertices per triangle
  398. OT_TRIANGLE_LIST = 4,
  399. /// A strip of triangles, 3 vertices for the first triangle, and 1 per triangle after that
  400. OT_TRIANGLE_STRIP = 5,
  401. /// A fan of triangles, 3 vertices for the first triangle, and 1 per triangle after that
  402. OT_TRIANGLE_FAN = 6
  403. };
  404. ISubMesh();
  405. /// SubMesh index.
  406. unsigned int index;
  407. /// SubMesh name.
  408. std::string name;
  409. /// Material used by this submesh.
  410. std::string materialRef;
  411. /// Texture alias information.
  412. std::string textureAliasName;
  413. std::string textureAliasRef;
  414. /// Assimp scene material index used by this submesh.
  415. /** -1 if no material or material could not be imported. */
  416. int materialIndex;
  417. /// If submesh uses shared geometry from parent mesh.
  418. bool usesSharedVertexData;
  419. /// Operation type.
  420. OperationType operationType;
  421. };
  422. /// Ogre SubMesh
  423. class SubMesh : public ISubMesh
  424. {
  425. public:
  426. SubMesh();
  427. ~SubMesh();
  428. /// Releases all memory that this data structure owns.
  429. /** @note Vertex and index data contains shared ptrs
  430. that are freed automatically. In practice the ref count
  431. should be 0 after this reset. */
  432. void Reset();
  433. /// Covert to Assimp mesh.
  434. aiMesh *ConvertToAssimpMesh(Mesh *parent);
  435. /// Vertex data.
  436. VertexData *vertexData;
  437. /// Index data.
  438. IndexData *indexData;
  439. };
  440. typedef std::vector<SubMesh*> SubMeshList;
  441. /// Ogre Mesh
  442. class Mesh
  443. {
  444. public:
  445. Mesh();
  446. ~Mesh();
  447. /// Releases all memory that this data structure owns.
  448. void Reset();
  449. /// Returns number of subMeshes.
  450. size_t NumSubMeshes() const;
  451. /// Returns submesh for @c index.
  452. SubMesh *GetSubMesh(uint16_t index) const;
  453. /// Convert mesh to Assimp scene.
  454. void ConvertToAssimpScene(aiScene* dest);
  455. /// Mesh has skeletal animations.
  456. bool hasSkeletalAnimations;
  457. /// Skeleton reference.
  458. std::string skeletonRef;
  459. /// Skeleton.
  460. Skeleton *skeleton;
  461. /// Vertex data
  462. VertexData *sharedVertexData;
  463. /// Sub meshes.
  464. SubMeshList subMeshes;
  465. /// Animations
  466. AnimationList animations;
  467. /// Poses
  468. PoseList poses;
  469. };
  470. /// Ogre XML Vertex Data
  471. class VertexDataXml : public IVertexData
  472. {
  473. public:
  474. VertexDataXml();
  475. bool HasPositions() const;
  476. bool HasNormals() const;
  477. bool HasTangents() const;
  478. bool HasUvs() const;
  479. size_t NumUvs() const;
  480. std::vector<aiVector3D> positions;
  481. std::vector<aiVector3D> normals;
  482. std::vector<aiVector3D> tangents;
  483. std::vector<std::vector<aiVector3D> > uvs;
  484. };
  485. /// Ogre XML Index Data
  486. class IndexDataXml
  487. {
  488. public:
  489. IndexDataXml() : faceCount(0) {}
  490. /// Face count.
  491. uint32_t faceCount;
  492. std::vector<aiFace> faces;
  493. };
  494. /// Ogre XML SubMesh
  495. class SubMeshXml : public ISubMesh
  496. {
  497. public:
  498. SubMeshXml();
  499. ~SubMeshXml();
  500. /// Releases all memory that this data structure owns.
  501. void Reset();
  502. aiMesh *ConvertToAssimpMesh(MeshXml *parent);
  503. IndexDataXml *indexData;
  504. VertexDataXml *vertexData;
  505. };
  506. typedef std::vector<SubMeshXml*> SubMeshXmlList;
  507. /// Ogre XML Mesh
  508. class MeshXml
  509. {
  510. public:
  511. MeshXml();
  512. ~MeshXml();
  513. /// Releases all memory that this data structure owns.
  514. void Reset();
  515. /// Returns number of subMeshes.
  516. size_t NumSubMeshes() const;
  517. /// Returns submesh for @c index.
  518. SubMeshXml *GetSubMesh(uint16_t index) const;
  519. /// Convert mesh to Assimp scene.
  520. void ConvertToAssimpScene(aiScene* dest);
  521. /// Skeleton reference.
  522. std::string skeletonRef;
  523. /// Skeleton.
  524. Skeleton *skeleton;
  525. /// Vertex data
  526. VertexDataXml *sharedVertexData;
  527. /// Sub meshes.
  528. SubMeshXmlList subMeshes;
  529. };
  530. } // Ogre
  531. } // Assimp
  532. #endif // ASSIMP_BUILD_NO_OGRE_IMPORTER
  533. #endif // AI_OGRESTRUCTS_H_INC