OgreBinarySerializer.h 15 KB

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