3DSLoader.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, 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. /** @file 3DSLoader.h
  34. * @brief 3DS File format loader
  35. */
  36. #ifndef AI_3DSIMPORTER_H_INC
  37. #define AI_3DSIMPORTER_H_INC
  38. #ifndef ASSIMP_BUILD_NO_3DS_IMPORTER
  39. #include <assimp/BaseImporter.h>
  40. #include <assimp/types.h>
  41. #include "3DSHelper.h"
  42. #include <assimp/StreamReader.h>
  43. struct aiNode;
  44. namespace Assimp {
  45. using namespace D3DS;
  46. // ---------------------------------------------------------------------------------
  47. /** Importer class for 3D Studio r3 and r4 3DS files
  48. */
  49. class Discreet3DSImporter final : public BaseImporter {
  50. public:
  51. Discreet3DSImporter();
  52. ~Discreet3DSImporter() override = default;
  53. // -------------------------------------------------------------------
  54. /** Returns whether the class can handle the format of the given file.
  55. * See BaseImporter::CanRead() for details.
  56. */
  57. bool CanRead( const std::string& pFile, IOSystem* pIOHandler,
  58. bool checkSig) const override;
  59. // -------------------------------------------------------------------
  60. /** Called prior to ReadFile().
  61. * The function is a request to the importer to update its configuration
  62. * basing on the Importer's configuration property list.
  63. */
  64. void SetupProperties(const Importer* pImp) override;
  65. protected:
  66. // -------------------------------------------------------------------
  67. /** Return importer meta information.
  68. * See #BaseImporter::GetInfo for the details
  69. */
  70. const aiImporterDesc* GetInfo () const override;
  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) override;
  77. // -------------------------------------------------------------------
  78. /** Converts a temporary material to the outer representation
  79. */
  80. void ConvertMaterial(D3DS::Material& p_cMat,
  81. aiMaterial& p_pcOut);
  82. // -------------------------------------------------------------------
  83. /** Read a chunk
  84. *
  85. * @param pcOut Receives the current chunk
  86. */
  87. void ReadChunk(Discreet3DS::Chunk* pcOut);
  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. ai_real 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();
  113. // -------------------------------------------------------------------
  114. /** Parse a top-level chunk in the file
  115. */
  116. void ParseChunk(const char* name, unsigned int num);
  117. // -------------------------------------------------------------------
  118. /** Parse a top-level editor chunk in the file
  119. */
  120. void ParseEditorChunk();
  121. // -------------------------------------------------------------------
  122. /** Parse a top-level object chunk in the file
  123. */
  124. void ParseObjectChunk();
  125. // -------------------------------------------------------------------
  126. /** Parse a material chunk in the file
  127. */
  128. void ParseMaterialChunk();
  129. // -------------------------------------------------------------------
  130. /** Parse a mesh chunk in the file
  131. */
  132. void ParseMeshChunk();
  133. // -------------------------------------------------------------------
  134. /** Parse a light chunk in the file
  135. */
  136. void ParseLightChunk();
  137. // -------------------------------------------------------------------
  138. /** Parse a camera chunk in the file
  139. */
  140. void ParseCameraChunk();
  141. // -------------------------------------------------------------------
  142. /** Parse a face list chunk in the file
  143. */
  144. void ParseFaceChunk();
  145. // -------------------------------------------------------------------
  146. /** Parse a keyframe chunk in the file
  147. */
  148. void ParseKeyframeChunk();
  149. // -------------------------------------------------------------------
  150. /** Parse a hierarchy chunk in the file
  151. */
  152. void ParseHierarchyChunk(uint16_t parent);
  153. // -------------------------------------------------------------------
  154. /** Parse a texture chunk in the file
  155. */
  156. void ParseTextureChunk(D3DS::Texture* pcOut);
  157. // -------------------------------------------------------------------
  158. /** Convert the meshes in the file
  159. */
  160. void ConvertMeshes(aiScene* pcOut);
  161. // -------------------------------------------------------------------
  162. /** Replace the default material in the scene
  163. */
  164. void ReplaceDefaultMaterial();
  165. bool ContainsTextures(unsigned int i) const {
  166. return !mScene->mMaterials[i].sTexDiffuse.mMapName.empty() ||
  167. !mScene->mMaterials[i].sTexBump.mMapName.empty() ||
  168. !mScene->mMaterials[i].sTexOpacity.mMapName.empty() ||
  169. !mScene->mMaterials[i].sTexEmissive.mMapName.empty() ||
  170. !mScene->mMaterials[i].sTexSpecular.mMapName.empty() ||
  171. !mScene->mMaterials[i].sTexShininess.mMapName.empty() ;
  172. }
  173. // -------------------------------------------------------------------
  174. /** Convert the whole scene
  175. */
  176. void ConvertScene(aiScene* pcOut);
  177. // -------------------------------------------------------------------
  178. /** generate unique vertices for a mesh
  179. */
  180. void MakeUnique(D3DS::Mesh& sMesh);
  181. // -------------------------------------------------------------------
  182. /** Add a node to the node graph
  183. */
  184. void AddNodeToGraph(aiScene* pcSOut,aiNode* pcOut,D3DS::Node* pcIn,
  185. aiMatrix4x4& absTrafo);
  186. // -------------------------------------------------------------------
  187. /** Search for a node in the graph.
  188. * Called recursively
  189. */
  190. void InverseNodeSearch(D3DS::Node* pcNode,D3DS::Node* pcCurrent);
  191. // -------------------------------------------------------------------
  192. /** Apply the master scaling factor to the mesh
  193. */
  194. void ApplyMasterScale(aiScene* pScene);
  195. // -------------------------------------------------------------------
  196. /** Clamp all indices in the file to a valid range
  197. */
  198. void CheckIndices(D3DS::Mesh& sMesh);
  199. // -------------------------------------------------------------------
  200. /** Skip the TCB info in a track key
  201. */
  202. void SkipTCBInfo();
  203. protected:
  204. /** Stream to read from */
  205. StreamReaderLE* stream;
  206. /** Last touched node index */
  207. short mLastNodeIndex;
  208. /** Current node, root node */
  209. D3DS::Node* mCurrentNode, *mRootNode;
  210. /** Scene under construction */
  211. D3DS::Scene* mScene;
  212. /** Ambient base color of the scene */
  213. aiColor3D mClrAmbient;
  214. /** Master scaling factor of the scene */
  215. ai_real mMasterScale;
  216. /** Path to the background image of the scene */
  217. std::string mBackgroundImage;
  218. bool bHasBG;
  219. /** true if PRJ file */
  220. bool bIsPrj;
  221. };
  222. } // end of namespace Assimp
  223. #endif // !! ASSIMP_BUILD_NO_3DS_IMPORTER
  224. #endif // AI_3DSIMPORTER_H_INC