OgreBinarySerializer.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2016, 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_OGREBINARYSERIALIZER_H_INC
  34. #define AI_OGREBINARYSERIALIZER_H_INC
  35. #ifndef ASSIMP_BUILD_NO_OGRE_IMPORTER
  36. #include "OgreStructs.h"
  37. #include "StreamReader.h"
  38. namespace Assimp
  39. {
  40. namespace Ogre
  41. {
  42. typedef Assimp::StreamReaderLE MemoryStreamReader;
  43. typedef boost::shared_ptr<MemoryStreamReader> MemoryStreamReaderPtr;
  44. class OgreBinarySerializer
  45. {
  46. public:
  47. /// Imports mesh and returns the result.
  48. /** @note Fatal unrecoverable errors will throw a DeadlyImportError. */
  49. static Mesh *ImportMesh(MemoryStreamReader *reader);
  50. /// Imports skeleton to @c mesh into Mesh::skeleton.
  51. /** If mesh does not have a skeleton reference or the skeleton file
  52. cannot be found it is not a fatal DeadlyImportError.
  53. @return If skeleton import was successful. */
  54. static bool ImportSkeleton(Assimp::IOSystem *pIOHandler, Mesh *mesh);
  55. static bool ImportSkeleton(Assimp::IOSystem *pIOHandler, MeshXml *mesh);
  56. private:
  57. enum AssetMode
  58. {
  59. AM_Mesh,
  60. AM_Skeleton
  61. };
  62. OgreBinarySerializer(MemoryStreamReader *reader, AssetMode mode) :
  63. m_currentLen(0),
  64. m_reader(reader),
  65. assetMode(mode)
  66. {
  67. }
  68. static MemoryStreamReaderPtr OpenReader(Assimp::IOSystem *pIOHandler, const std::string &filename);
  69. // Header
  70. uint16_t ReadHeader(bool readLen = true);
  71. void RollbackHeader();
  72. // Mesh
  73. void ReadMesh(Mesh *mesh);
  74. void ReadMeshLodInfo(Mesh *mesh);
  75. void ReadMeshSkeletonLink(Mesh *mesh);
  76. void ReadMeshBounds(Mesh *mesh);
  77. void ReadMeshExtremes(Mesh *mesh);
  78. void ReadSubMesh(Mesh *mesh);
  79. void ReadSubMeshNames(Mesh *mesh);
  80. void ReadSubMeshOperation(SubMesh *submesh);
  81. void ReadSubMeshTextureAlias(SubMesh *submesh);
  82. void ReadBoneAssignment(VertexData *dest);
  83. void ReadGeometry(VertexData *dest);
  84. void ReadGeometryVertexDeclaration(VertexData *dest);
  85. void ReadGeometryVertexElement(VertexData *dest);
  86. void ReadGeometryVertexBuffer(VertexData *dest);
  87. void ReadEdgeList(Mesh *mesh);
  88. void ReadPoses(Mesh *mesh);
  89. void ReadPoseVertices(Pose *pose);
  90. void ReadAnimations(Mesh *mesh);
  91. void ReadAnimation(Animation *anim);
  92. void ReadAnimationKeyFrames(Animation *anim, VertexAnimationTrack *track);
  93. void NormalizeBoneWeights(VertexData *vertexData) const;
  94. // Skeleton
  95. void ReadSkeleton(Skeleton *skeleton);
  96. void ReadBone(Skeleton *skeleton);
  97. void ReadBoneParent(Skeleton *skeleton);
  98. void ReadSkeletonAnimation(Skeleton *skeleton);
  99. void ReadSkeletonAnimationTrack(Skeleton *skeleton, Animation *dest);
  100. void ReadSkeletonAnimationKeyFrame(VertexAnimationTrack *dest);
  101. void ReadSkeletonAnimationLink(Skeleton *skeleton);
  102. // Reader utils
  103. bool AtEnd() const;
  104. template<typename T>
  105. inline T Read();
  106. void ReadBytes(char *dest, size_t numBytes);
  107. void ReadBytes(uint8_t *dest, size_t numBytes);
  108. void ReadBytes(void *dest, size_t numBytes);
  109. uint8_t *ReadBytes(size_t numBytes);
  110. void ReadVector(aiVector3D &vec);
  111. void ReadQuaternion(aiQuaternion &quat);
  112. std::string ReadString(size_t len);
  113. std::string ReadLine();
  114. void SkipBytes(size_t numBytes);
  115. uint32_t m_currentLen;
  116. MemoryStreamReader *m_reader;
  117. AssetMode assetMode;
  118. };
  119. enum MeshChunkId
  120. {
  121. M_HEADER = 0x1000,
  122. // char* version : Version number check
  123. M_MESH = 0x3000,
  124. // bool skeletallyAnimated // important flag which affects h/w buffer policies
  125. // Optional M_GEOMETRY chunk
  126. M_SUBMESH = 0x4000,
  127. // char* materialName
  128. // bool useSharedVertices
  129. // unsigned int indexCount
  130. // bool indexes32Bit
  131. // unsigned int* faceVertexIndices (indexCount)
  132. // OR
  133. // unsigned short* faceVertexIndices (indexCount)
  134. // M_GEOMETRY chunk (Optional: present only if useSharedVertices = false)
  135. M_SUBMESH_OPERATION = 0x4010, // optional, trilist assumed if missing
  136. // unsigned short operationType
  137. M_SUBMESH_BONE_ASSIGNMENT = 0x4100,
  138. // Optional bone weights (repeating section)
  139. // unsigned int vertexIndex;
  140. // unsigned short boneIndex;
  141. // float weight;
  142. // Optional chunk that matches a texture name to an alias
  143. // a texture alias is sent to the submesh material to use this texture name
  144. // instead of the one in the texture unit with a matching alias name
  145. M_SUBMESH_TEXTURE_ALIAS = 0x4200, // Repeating section
  146. // char* aliasName;
  147. // char* textureName;
  148. M_GEOMETRY = 0x5000, // NB this chunk is embedded within M_MESH and M_SUBMESH
  149. // unsigned int vertexCount
  150. M_GEOMETRY_VERTEX_DECLARATION = 0x5100,
  151. M_GEOMETRY_VERTEX_ELEMENT = 0x5110, // Repeating section
  152. // unsigned short source; // buffer bind source
  153. // unsigned short type; // VertexElementType
  154. // unsigned short semantic; // VertexElementSemantic
  155. // unsigned short offset; // start offset in buffer in bytes
  156. // unsigned short index; // index of the semantic (for colours and texture coords)
  157. M_GEOMETRY_VERTEX_BUFFER = 0x5200, // Repeating section
  158. // unsigned short bindIndex; // Index to bind this buffer to
  159. // unsigned short vertexSize; // Per-vertex size, must agree with declaration at this index
  160. M_GEOMETRY_VERTEX_BUFFER_DATA = 0x5210,
  161. // raw buffer data
  162. M_MESH_SKELETON_LINK = 0x6000,
  163. // Optional link to skeleton
  164. // char* skeletonName : name of .skeleton to use
  165. M_MESH_BONE_ASSIGNMENT = 0x7000,
  166. // Optional bone weights (repeating section)
  167. // unsigned int vertexIndex;
  168. // unsigned short boneIndex;
  169. // float weight;
  170. M_MESH_LOD = 0x8000,
  171. // Optional LOD information
  172. // string strategyName;
  173. // unsigned short numLevels;
  174. // bool manual; (true for manual alternate meshes, false for generated)
  175. M_MESH_LOD_USAGE = 0x8100,
  176. // Repeating section, ordered in increasing depth
  177. // NB LOD 0 (full detail from 0 depth) is omitted
  178. // LOD value - this is a distance, a pixel count etc, based on strategy
  179. // float lodValue;
  180. M_MESH_LOD_MANUAL = 0x8110,
  181. // Required if M_MESH_LOD section manual = true
  182. // String manualMeshName;
  183. M_MESH_LOD_GENERATED = 0x8120,
  184. // Required if M_MESH_LOD section manual = false
  185. // Repeating section (1 per submesh)
  186. // unsigned int indexCount;
  187. // bool indexes32Bit
  188. // unsigned short* faceIndexes; (indexCount)
  189. // OR
  190. // unsigned int* faceIndexes; (indexCount)
  191. M_MESH_BOUNDS = 0x9000,
  192. // float minx, miny, minz
  193. // float maxx, maxy, maxz
  194. // float radius
  195. // Added By DrEvil
  196. // optional chunk that contains a table of submesh indexes and the names of
  197. // the sub-meshes.
  198. M_SUBMESH_NAME_TABLE = 0xA000,
  199. // Subchunks of the name table. Each chunk contains an index & string
  200. M_SUBMESH_NAME_TABLE_ELEMENT = 0xA100,
  201. // short index
  202. // char* name
  203. // Optional chunk which stores precomputed edge data
  204. M_EDGE_LISTS = 0xB000,
  205. // Each LOD has a separate edge list
  206. M_EDGE_LIST_LOD = 0xB100,
  207. // unsigned short lodIndex
  208. // bool isManual // If manual, no edge data here, loaded from manual mesh
  209. // bool isClosed
  210. // unsigned long numTriangles
  211. // unsigned long numEdgeGroups
  212. // Triangle* triangleList
  213. // unsigned long indexSet
  214. // unsigned long vertexSet
  215. // unsigned long vertIndex[3]
  216. // unsigned long sharedVertIndex[3]
  217. // float normal[4]
  218. M_EDGE_GROUP = 0xB110,
  219. // unsigned long vertexSet
  220. // unsigned long triStart
  221. // unsigned long triCount
  222. // unsigned long numEdges
  223. // Edge* edgeList
  224. // unsigned long triIndex[2]
  225. // unsigned long vertIndex[2]
  226. // unsigned long sharedVertIndex[2]
  227. // bool degenerate
  228. // Optional poses section, referred to by pose keyframes
  229. M_POSES = 0xC000,
  230. M_POSE = 0xC100,
  231. // char* name (may be blank)
  232. // unsigned short target // 0 for shared geometry,
  233. // 1+ for submesh index + 1
  234. // bool includesNormals [1.8+]
  235. M_POSE_VERTEX = 0xC111,
  236. // unsigned long vertexIndex
  237. // float xoffset, yoffset, zoffset
  238. // float xnormal, ynormal, znormal (optional, 1.8+)
  239. // Optional vertex animation chunk
  240. M_ANIMATIONS = 0xD000,
  241. M_ANIMATION = 0xD100,
  242. // char* name
  243. // float length
  244. M_ANIMATION_BASEINFO = 0xD105,
  245. // [Optional] base keyframe information (pose animation only)
  246. // char* baseAnimationName (blank for self)
  247. // float baseKeyFrameTime
  248. M_ANIMATION_TRACK = 0xD110,
  249. // unsigned short type // 1 == morph, 2 == pose
  250. // unsigned short target // 0 for shared geometry,
  251. // 1+ for submesh index + 1
  252. M_ANIMATION_MORPH_KEYFRAME = 0xD111,
  253. // float time
  254. // bool includesNormals [1.8+]
  255. // float x,y,z // repeat by number of vertices in original geometry
  256. M_ANIMATION_POSE_KEYFRAME = 0xD112,
  257. // float time
  258. M_ANIMATION_POSE_REF = 0xD113, // repeat for number of referenced poses
  259. // unsigned short poseIndex
  260. // float influence
  261. // Optional submesh extreme vertex list chink
  262. M_TABLE_EXTREMES = 0xE000
  263. // unsigned short submesh_index;
  264. // float extremes [n_extremes][3];
  265. };
  266. /*
  267. static std::string MeshHeaderToString(MeshChunkId id)
  268. {
  269. switch(id)
  270. {
  271. case M_HEADER: return "HEADER";
  272. case M_MESH: return "MESH";
  273. case M_SUBMESH: return "SUBMESH";
  274. case M_SUBMESH_OPERATION: return "SUBMESH_OPERATION";
  275. case M_SUBMESH_BONE_ASSIGNMENT: return "SUBMESH_BONE_ASSIGNMENT";
  276. case M_SUBMESH_TEXTURE_ALIAS: return "SUBMESH_TEXTURE_ALIAS";
  277. case M_GEOMETRY: return "GEOMETRY";
  278. case M_GEOMETRY_VERTEX_DECLARATION: return "GEOMETRY_VERTEX_DECLARATION";
  279. case M_GEOMETRY_VERTEX_ELEMENT: return "GEOMETRY_VERTEX_ELEMENT";
  280. case M_GEOMETRY_VERTEX_BUFFER: return "GEOMETRY_VERTEX_BUFFER";
  281. case M_GEOMETRY_VERTEX_BUFFER_DATA: return "GEOMETRY_VERTEX_BUFFER_DATA";
  282. case M_MESH_SKELETON_LINK: return "MESH_SKELETON_LINK";
  283. case M_MESH_BONE_ASSIGNMENT: return "MESH_BONE_ASSIGNMENT";
  284. case M_MESH_LOD: return "MESH_LOD";
  285. case M_MESH_LOD_USAGE: return "MESH_LOD_USAGE";
  286. case M_MESH_LOD_MANUAL: return "MESH_LOD_MANUAL";
  287. case M_MESH_LOD_GENERATED: return "MESH_LOD_GENERATED";
  288. case M_MESH_BOUNDS: return "MESH_BOUNDS";
  289. case M_SUBMESH_NAME_TABLE: return "SUBMESH_NAME_TABLE";
  290. case M_SUBMESH_NAME_TABLE_ELEMENT: return "SUBMESH_NAME_TABLE_ELEMENT";
  291. case M_EDGE_LISTS: return "EDGE_LISTS";
  292. case M_EDGE_LIST_LOD: return "EDGE_LIST_LOD";
  293. case M_EDGE_GROUP: return "EDGE_GROUP";
  294. case M_POSES: return "POSES";
  295. case M_POSE: return "POSE";
  296. case M_POSE_VERTEX: return "POSE_VERTEX";
  297. case M_ANIMATIONS: return "ANIMATIONS";
  298. case M_ANIMATION: return "ANIMATION";
  299. case M_ANIMATION_BASEINFO: return "ANIMATION_BASEINFO";
  300. case M_ANIMATION_TRACK: return "ANIMATION_TRACK";
  301. case M_ANIMATION_MORPH_KEYFRAME: return "ANIMATION_MORPH_KEYFRAME";
  302. case M_ANIMATION_POSE_KEYFRAME: return "ANIMATION_POSE_KEYFRAME";
  303. case M_ANIMATION_POSE_REF: return "ANIMATION_POSE_REF";
  304. case M_TABLE_EXTREMES: return "TABLE_EXTREMES";
  305. }
  306. return "Unknown_MeshChunkId";
  307. }
  308. */
  309. enum SkeletonChunkId
  310. {
  311. SKELETON_HEADER = 0x1000,
  312. // char* version : Version number check
  313. SKELETON_BLENDMODE = 0x1010, // optional
  314. // unsigned short blendmode : SkeletonAnimationBlendMode
  315. SKELETON_BONE = 0x2000,
  316. // Repeating section defining each bone in the system.
  317. // Bones are assigned indexes automatically based on their order of declaration
  318. // starting with 0.
  319. // char* name : name of the bone
  320. // unsigned short handle : handle of the bone, should be contiguous & start at 0
  321. // Vector3 position : position of this bone relative to parent
  322. // Quaternion orientation : orientation of this bone relative to parent
  323. // Vector3 scale : scale of this bone relative to parent
  324. SKELETON_BONE_PARENT = 0x3000,
  325. // Record of the parent of a single bone, used to build the node tree
  326. // Repeating section, listed in Bone Index order, one per Bone
  327. // unsigned short handle : child bone
  328. // unsigned short parentHandle : parent bone
  329. SKELETON_ANIMATION = 0x4000,
  330. // A single animation for this skeleton
  331. // char* name : Name of the animation
  332. // float length : Length of the animation in seconds
  333. SKELETON_ANIMATION_BASEINFO = 0x4010,
  334. // [Optional] base keyframe information
  335. // char* baseAnimationName (blank for self)
  336. // float baseKeyFrameTime
  337. SKELETON_ANIMATION_TRACK = 0x4100,
  338. // A single animation track (relates to a single bone)
  339. // Repeating section (within SKELETON_ANIMATION)
  340. // unsigned short boneIndex : Index of bone to apply to
  341. SKELETON_ANIMATION_TRACK_KEYFRAME = 0x4110,
  342. // A single keyframe within the track
  343. // Repeating section
  344. // float time : The time position (seconds)
  345. // Quaternion rotate : Rotation to apply at this keyframe
  346. // Vector3 translate : Translation to apply at this keyframe
  347. // Vector3 scale : Scale to apply at this keyframe
  348. SKELETON_ANIMATION_LINK = 0x5000
  349. // Link to another skeleton, to re-use its animations
  350. // char* skeletonName : name of skeleton to get animations from
  351. // float scale : scale to apply to trans/scale keys
  352. };
  353. /*
  354. static std::string SkeletonHeaderToString(SkeletonChunkId id)
  355. {
  356. switch(id)
  357. {
  358. case SKELETON_HEADER: return "HEADER";
  359. case SKELETON_BLENDMODE: return "BLENDMODE";
  360. case SKELETON_BONE: return "BONE";
  361. case SKELETON_BONE_PARENT: return "BONE_PARENT";
  362. case SKELETON_ANIMATION: return "ANIMATION";
  363. case SKELETON_ANIMATION_BASEINFO: return "ANIMATION_BASEINFO";
  364. case SKELETON_ANIMATION_TRACK: return "ANIMATION_TRACK";
  365. case SKELETON_ANIMATION_TRACK_KEYFRAME: return "ANIMATION_TRACK_KEYFRAME";
  366. case SKELETON_ANIMATION_LINK: return "ANIMATION_LINK";
  367. }
  368. return "Unknown_SkeletonChunkId";
  369. }
  370. */
  371. } // Ogre
  372. } // Assimp
  373. #endif // ASSIMP_BUILD_NO_OGRE_IMPORTER
  374. #endif // AI_OGREBINARYSERIALIZER_H_INC