3DSLoader.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. // ---------------------------------------------------------------------------------
  46. /** Importer class for 3D Studio r3 and r4 3DS files
  47. */
  48. class Discreet3DSImporter final : public BaseImporter {
  49. public:
  50. Discreet3DSImporter();
  51. ~Discreet3DSImporter() override = default;
  52. // -------------------------------------------------------------------
  53. /** Returns whether the class can handle the format of the given file.
  54. * See BaseImporter::CanRead() for details.
  55. */
  56. bool CanRead( const std::string& pFile, IOSystem* pIOHandler,
  57. bool checkSig) const override;
  58. // -------------------------------------------------------------------
  59. /** Called prior to ReadFile().
  60. * The function is a request to the importer to update its configuration
  61. * basing on the Importer's configuration property list.
  62. */
  63. void SetupProperties(const Importer* pImp) override;
  64. protected:
  65. // -------------------------------------------------------------------
  66. /** Return importer meta information.
  67. * See #BaseImporter::GetInfo for the details
  68. */
  69. const aiImporterDesc* GetInfo () const override;
  70. // -------------------------------------------------------------------
  71. /** Imports the given file into the given scene structure.
  72. * See BaseImporter::InternReadFile() for details
  73. */
  74. void InternReadFile( const std::string& pFile, aiScene* pScene,
  75. IOSystem* pIOHandler) override;
  76. // -------------------------------------------------------------------
  77. /** Converts a temporary material to the outer representation
  78. */
  79. void ConvertMaterial(D3DS::Material& p_cMat, aiMaterial& p_pcOut);
  80. // -------------------------------------------------------------------
  81. /** Read a chunk
  82. *
  83. * @param pcOut Receives the current chunk
  84. */
  85. void ReadChunk(D3DS::Discreet3DS::Chunk* pcOut);
  86. // -------------------------------------------------------------------
  87. /** Parse a percentage chunk. mCurrent will point to the next
  88. * chunk behind afterwards. If no percentage chunk is found
  89. * QNAN is returned.
  90. */
  91. ai_real ParsePercentageChunk();
  92. // -------------------------------------------------------------------
  93. /** Parse a color chunk. mCurrent will point to the next chunk behind
  94. * afterward. If no color chunk is found QNAN is returned in all members.
  95. */
  96. void ParseColorChunk(aiColor3D* p_pcOut, bool p_bAcceptPercent = true);
  97. // -------------------------------------------------------------------
  98. /** Skip a chunk in the file
  99. */
  100. void SkipChunk();
  101. // -------------------------------------------------------------------
  102. /** Generate the node-graph
  103. */
  104. void GenerateNodeGraph(aiScene* pcOut);
  105. // -------------------------------------------------------------------
  106. /** Parse a main top-level chunk in the file
  107. */
  108. void ParseMainChunk();
  109. // -------------------------------------------------------------------
  110. /** Parse a top-level chunk in the file
  111. */
  112. void ParseChunk(const char* name, unsigned int num);
  113. // -------------------------------------------------------------------
  114. /** Parse a top-level editor chunk in the file
  115. */
  116. void ParseEditorChunk();
  117. // -------------------------------------------------------------------
  118. /** Parse a top-level object chunk in the file
  119. */
  120. void ParseObjectChunk();
  121. // -------------------------------------------------------------------
  122. /** Parse a material chunk in the file
  123. */
  124. void ParseMaterialChunk();
  125. // -------------------------------------------------------------------
  126. /** Parse a mesh chunk in the file
  127. */
  128. void ParseMeshChunk();
  129. // -------------------------------------------------------------------
  130. /** Parse a light chunk in the file
  131. */
  132. void ParseLightChunk();
  133. // -------------------------------------------------------------------
  134. /** Parse a camera chunk in the file
  135. */
  136. void ParseCameraChunk();
  137. // -------------------------------------------------------------------
  138. /** Parse a face list chunk in the file
  139. */
  140. void ParseFaceChunk();
  141. // -------------------------------------------------------------------
  142. /** Parse a keyframe chunk in the file
  143. */
  144. void ParseKeyframeChunk();
  145. // -------------------------------------------------------------------
  146. /** Parse a hierarchy chunk in the file
  147. */
  148. void ParseHierarchyChunk(uint16_t parent);
  149. // -------------------------------------------------------------------
  150. /** Parse a texture chunk in the file
  151. */
  152. void ParseTextureChunk(D3DS::Texture* pcOut);
  153. // -------------------------------------------------------------------
  154. /** Convert the meshes in the file
  155. */
  156. void ConvertMeshes(aiScene* pcOut);
  157. // -------------------------------------------------------------------
  158. /** Replace the default material in the scene
  159. */
  160. void ReplaceDefaultMaterial();
  161. bool ContainsTextures(unsigned int i) const {
  162. return !mScene->mMaterials[i].sTexDiffuse.mMapName.empty() ||
  163. !mScene->mMaterials[i].sTexBump.mMapName.empty() ||
  164. !mScene->mMaterials[i].sTexOpacity.mMapName.empty() ||
  165. !mScene->mMaterials[i].sTexEmissive.mMapName.empty() ||
  166. !mScene->mMaterials[i].sTexSpecular.mMapName.empty() ||
  167. !mScene->mMaterials[i].sTexShininess.mMapName.empty() ;
  168. }
  169. // -------------------------------------------------------------------
  170. /** Convert the whole scene
  171. */
  172. void ConvertScene(aiScene* pcOut);
  173. // -------------------------------------------------------------------
  174. /** generate unique vertices for a mesh
  175. */
  176. void MakeUnique(D3DS::Mesh& sMesh);
  177. // -------------------------------------------------------------------
  178. /** Add a node to the node graph
  179. */
  180. void AddNodeToGraph(aiScene* pcSOut,aiNode* pcOut, D3DS::Node* pcIn,
  181. aiMatrix4x4& absTrafo);
  182. // -------------------------------------------------------------------
  183. /** Search for a node in the graph.
  184. * Called recursively
  185. */
  186. void InverseNodeSearch(D3DS::Node* pcNode, D3DS::Node* pcCurrent);
  187. // -------------------------------------------------------------------
  188. /** Apply the master scaling factor to the mesh
  189. */
  190. void ApplyMasterScale(const aiScene* pScene);
  191. // -------------------------------------------------------------------
  192. /** Clamp all indices in the file to a valid range
  193. */
  194. void CheckIndices(D3DS::Mesh& sMesh);
  195. // -------------------------------------------------------------------
  196. /** Skip the TCB info in a track key
  197. */
  198. void SkipTCBInfo();
  199. private:
  200. /// Stream to read from
  201. StreamReaderLE* mStream;
  202. /// Last touched node index
  203. short mLastNodeIndex;
  204. /// Current node
  205. D3DS::Node* mCurrentNode;
  206. /// Root node
  207. D3DS::Node *mRootNode;
  208. /// Scene under construction
  209. D3DS::Scene* mScene;
  210. /// Ambient base color of the scene
  211. aiColor3D mClrAmbient;
  212. /// Master scaling factor of the scene
  213. ai_real mMasterScale;
  214. /// Path to the background image of the scene
  215. std::string mBackgroundImage;
  216. /// true for has a background
  217. bool bHasBG;
  218. /// true if PRJ file
  219. bool bIsPrj;
  220. };
  221. } // end of namespace Assimp
  222. #endif // !! ASSIMP_BUILD_NO_3DS_IMPORTER
  223. #endif // AI_3DSIMPORTER_H_INC