3DSLoader.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. Open Asset Import Library (ASSIMP)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2008, ASSIMP Development 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 Development 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. /** @file Definition of the .3ds importer class. */
  34. #ifndef AI_3DSIMPORTER_H_INC
  35. #define AI_3DSIMPORTER_H_INC
  36. #include <map>
  37. #include "BaseImporter.h"
  38. #include "../include/aiTypes.h"
  39. struct aiNode;
  40. #include "3DSHelper.h"
  41. namespace Assimp
  42. {
  43. class MaterialHelper;
  44. using namespace Dot3DS;
  45. // ---------------------------------------------------------------------------
  46. /** The Dot3DSImporter is a worker class capable of importing a scene from a
  47. * 3ds Max 4/5 Files (.3ds)
  48. */
  49. class Dot3DSImporter : public BaseImporter
  50. {
  51. friend class Importer;
  52. protected:
  53. /** Constructor to be privately used by Importer */
  54. Dot3DSImporter();
  55. /** Destructor, private as well */
  56. ~Dot3DSImporter();
  57. public:
  58. // -------------------------------------------------------------------
  59. /** Returns whether the class can handle the format of the given file.
  60. * See BaseImporter::CanRead() for details. */
  61. bool CanRead( const std::string& pFile, IOSystem* pIOHandler) const;
  62. protected:
  63. // -------------------------------------------------------------------
  64. /** Called by Importer::GetExtensionList() for each loaded importer.
  65. * See BaseImporter::GetExtensionList() for details
  66. */
  67. void GetExtensionList(std::string& append)
  68. {
  69. append.append("*.3ds");
  70. }
  71. // -------------------------------------------------------------------
  72. /** Imports the given file into the given scene structure.
  73. * See BaseImporter::InternReadFile() for details
  74. */
  75. void InternReadFile( const std::string& pFile, aiScene* pScene,
  76. IOSystem* pIOHandler);
  77. // -------------------------------------------------------------------
  78. /** Converts a temporary material to the outer representation
  79. */
  80. void ConvertMaterial(Dot3DS::Material& p_cMat,
  81. MaterialHelper& p_pcOut);
  82. // -------------------------------------------------------------------
  83. /** Read a chunk, get a pointer to it
  84. * The mCurrent pointer will be increased by sizeof(Dot3DSFile::Chunk),
  85. * thus pointing directly to the data of the chunk
  86. */
  87. void ReadChunk(const Dot3DSFile::Chunk** p_ppcOut);
  88. // -------------------------------------------------------------------
  89. /** Parse a percentage chunk. mCurrent will point to the next
  90. * chunk behind afterwards. If no percentage chunk is found
  91. * QNAN is returned.
  92. */
  93. float ParsePercentageChunk();
  94. // -------------------------------------------------------------------
  95. /** Parse a color chunk. mCurrent will point to the next
  96. * chunk behind afterwards. If no color chunk is found
  97. * QNAN is returned in all members.
  98. */
  99. void ParseColorChunk(aiColor3D* p_pcOut,
  100. bool p_bAcceptPercent = true);
  101. // -------------------------------------------------------------------
  102. /** Skip a chunk in the file
  103. */
  104. void SkipChunk();
  105. // -------------------------------------------------------------------
  106. /** Generate the nodegraph
  107. */
  108. void GenerateNodeGraph(aiScene* pcOut);
  109. // -------------------------------------------------------------------
  110. /** Parse a main top-level chunk in the file
  111. */
  112. void ParseMainChunk(int* piRemaining);
  113. // -------------------------------------------------------------------
  114. /** Parse a top-level chunk in the file
  115. */
  116. void ParseChunk(int* piRemaining);
  117. // -------------------------------------------------------------------
  118. /** Parse a top-level editor chunk in the file
  119. */
  120. void ParseEditorChunk(int* piRemaining);
  121. // -------------------------------------------------------------------
  122. /** Parse a top-level object chunk in the file
  123. */
  124. void ParseObjectChunk(int* piRemaining);
  125. // -------------------------------------------------------------------
  126. /** Parse a material chunk in the file
  127. */
  128. void ParseMaterialChunk(int* piRemaining);
  129. // -------------------------------------------------------------------
  130. /** Apply texture coordinate offsets
  131. */
  132. void ApplyScaleNOffset();
  133. void BakeScaleNOffset(aiMesh* pcMesh, Dot3DS::Material* pcSrc);
  134. // -------------------------------------------------------------------
  135. /** Parse a mesh chunk in the file
  136. */
  137. void ParseMeshChunk(int* piRemaining);
  138. // -------------------------------------------------------------------
  139. /** Parse a face list chunk in the file
  140. */
  141. void ParseFaceChunk(int* piRemaining);
  142. // -------------------------------------------------------------------
  143. /** Parse a keyframe chunk in the file
  144. */
  145. void ParseKeyframeChunk(int* piRemaining);
  146. // -------------------------------------------------------------------
  147. /** Parse a hierarchy chunk in the file
  148. */
  149. void ParseHierarchyChunk(int* piRemaining);
  150. // -------------------------------------------------------------------
  151. /** Parse a texture chunk in the file
  152. */
  153. void ParseTextureChunk(int* piRemaining,Dot3DS::Texture* pcOut);
  154. // -------------------------------------------------------------------
  155. /** Convert the meshes in the file
  156. */
  157. void ConvertMeshes(aiScene* pcOut);
  158. // -------------------------------------------------------------------
  159. /** Replace the default material in the scene
  160. */
  161. void ReplaceDefaultMaterial();
  162. // -------------------------------------------------------------------
  163. /** Convert the whole scene
  164. */
  165. void ConvertScene(aiScene* pcOut);
  166. // -------------------------------------------------------------------
  167. /** U/V Scaling/Offset handling
  168. */
  169. void GenTexCoord (Dot3DS::Texture* pcTexture,
  170. const std::vector<aiVector2D>& p_vIn,
  171. std::vector<aiVector2D>& p_vOut);
  172. // -------------------------------------------------------------------
  173. /** generate normal vectors for a given mesh
  174. */
  175. void GenNormals(Dot3DS::Mesh* sMesh);
  176. // -------------------------------------------------------------------
  177. /** generate unique vertices for a mesh
  178. */
  179. void MakeUnique(Dot3DS::Mesh* sMesh);
  180. // -------------------------------------------------------------------
  181. /** Add a node to the node graph
  182. */
  183. void AddNodeToGraph(aiScene* pcSOut,aiNode* pcOut,Dot3DS::Node* pcIn);
  184. // -------------------------------------------------------------------
  185. /** Search for a node in the graph.
  186. * Called recursively
  187. */
  188. void InverseNodeSearch(Dot3DS::Node* pcNode,Dot3DS::Node* pcCurrent);
  189. // -------------------------------------------------------------------
  190. /** Apply the master scaling factor to the mesh
  191. */
  192. void ApplyMasterScale(aiScene* pScene);
  193. // -------------------------------------------------------------------
  194. /** Clamp all indices in the file to a valid range
  195. */
  196. void CheckIndices(Dot3DS::Mesh* sMesh);
  197. protected:
  198. /** Buffer to hold the loaded file */
  199. unsigned char* mBuffer;
  200. /** Pointer to the current read position */
  201. const unsigned char* mCurrent;
  202. /** Used to store old chunk addresses to jump back in the file*/
  203. const unsigned char* mLast;
  204. /** Last touched node index */
  205. short mLastNodeIndex;
  206. /** Current node, root node */
  207. Dot3DS::Node* mCurrentNode, *mRootNode;
  208. /** Scene under construction */
  209. Dot3DS::Scene* mScene;
  210. /** Ambient base color of the scene */
  211. aiColor3D mClrAmbient;
  212. /** Master scaling factor of the scene */
  213. float mMasterScale;
  214. /** Path to the background image of the scene */
  215. std::string mBackgroundImage;
  216. bool bHasBG;
  217. };
  218. } // end of namespace Assimp
  219. #endif // AI_3DSIMPORTER_H_INC