OgreBinarySerializer.h 16 KB

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