3DSHelper.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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 Defines helper data structures for the import of 3DS files */
  34. #ifndef AI_3DSFILEHELPER_H_INC
  35. #define AI_3DSFILEHELPER_H_INC
  36. #include <assimp/SmoothingGroups.h>
  37. #include <assimp/SpatialSort.h>
  38. #include <assimp/StringUtils.h>
  39. #include <assimp/anim.h>
  40. #include <assimp/camera.h>
  41. #include <assimp/light.h>
  42. #include <assimp/material.h>
  43. #include <assimp/qnan.h>
  44. #include <cstdio> //sprintf
  45. namespace Assimp::D3DS {
  46. #include <assimp/Compiler/pushpack1.h>
  47. // ---------------------------------------------------------------------------
  48. /** Defines chunks and data structures.
  49. */
  50. namespace Discreet3DS {
  51. //! data structure for a single chunk in a .3ds file
  52. struct Chunk {
  53. uint16_t Flag;
  54. uint32_t Size;
  55. } PACK_STRUCT;
  56. //! Used for shading field in material3ds structure
  57. //! From AutoDesk 3ds SDK
  58. typedef enum {
  59. // translated to gouraud shading with wireframe active
  60. Wire = 0x0,
  61. // if this material is set, no vertex normals will
  62. // be calculated for the model. Face normals + gouraud
  63. Flat = 0x1,
  64. // standard gouraud shading
  65. Gouraud = 0x2,
  66. // phong shading
  67. Phong = 0x3,
  68. // cooktorrance or anistropic phong shading ...
  69. // the exact meaning is unknown, if you know it
  70. // feel free to tell me ;-)
  71. Metal = 0x4,
  72. // required by the ASE loader
  73. Blinn = 0x5
  74. } shadetype3ds;
  75. // Flags for animated keys
  76. enum {
  77. KEY_USE_TENS = 0x1,
  78. KEY_USE_CONT = 0x2,
  79. KEY_USE_BIAS = 0x4,
  80. KEY_USE_EASE_TO = 0x8,
  81. KEY_USE_EASE_FROM = 0x10
  82. };
  83. enum {
  84. // ********************************************************************
  85. // Basic chunks which can be found everywhere in the file
  86. CHUNK_VERSION = 0x0002,
  87. CHUNK_RGBF = 0x0010, // float4 R; float4 G; float4 B
  88. CHUNK_RGBB = 0x0011, // int1 R; int1 G; int B
  89. // Linear color values (gamma = 2.2?)
  90. CHUNK_LINRGBF = 0x0013, // float4 R; float4 G; float4 B
  91. CHUNK_LINRGBB = 0x0012, // int1 R; int1 G; int B
  92. CHUNK_PERCENTW = 0x0030, // int2 percentage
  93. CHUNK_PERCENTF = 0x0031, // float4 percentage
  94. CHUNK_PERCENTD = 0x0032, // float8 percentage
  95. // ********************************************************************
  96. // Prj master chunk
  97. CHUNK_PRJ = 0xC23D,
  98. // MDLI master chunk
  99. CHUNK_MLI = 0x3DAA,
  100. // Primary main chunk of the .3ds file
  101. CHUNK_MAIN = 0x4D4D,
  102. // Mesh main chunk
  103. CHUNK_OBJMESH = 0x3D3D,
  104. // Specifies the background color of the .3ds file
  105. // This is passed through the material system for
  106. // viewing purposes.
  107. CHUNK_BKGCOLOR = 0x1200,
  108. // Specifies the ambient base color of the scene.
  109. // This is added to all materials in the file
  110. CHUNK_AMBCOLOR = 0x2100,
  111. // Specifies the background image for the whole scene
  112. // This value is passed through the material system
  113. // to the viewer
  114. CHUNK_BIT_MAP = 0x1100,
  115. CHUNK_BIT_MAP_EXISTS = 0x1101,
  116. // ********************************************************************
  117. // Viewport related stuff. Ignored
  118. CHUNK_DEFAULT_VIEW = 0x3000,
  119. CHUNK_VIEW_TOP = 0x3010,
  120. CHUNK_VIEW_BOTTOM = 0x3020,
  121. CHUNK_VIEW_LEFT = 0x3030,
  122. CHUNK_VIEW_RIGHT = 0x3040,
  123. CHUNK_VIEW_FRONT = 0x3050,
  124. CHUNK_VIEW_BACK = 0x3060,
  125. CHUNK_VIEW_USER = 0x3070,
  126. CHUNK_VIEW_CAMERA = 0x3080,
  127. // ********************************************************************
  128. // Mesh chunks
  129. CHUNK_OBJBLOCK = 0x4000,
  130. CHUNK_TRIMESH = 0x4100,
  131. CHUNK_VERTLIST = 0x4110,
  132. CHUNK_VERTFLAGS = 0x4111,
  133. CHUNK_FACELIST = 0x4120,
  134. CHUNK_FACEMAT = 0x4130,
  135. CHUNK_MAPLIST = 0x4140,
  136. CHUNK_SMOOLIST = 0x4150,
  137. CHUNK_TRMATRIX = 0x4160,
  138. CHUNK_MESHCOLOR = 0x4165,
  139. CHUNK_TXTINFO = 0x4170,
  140. CHUNK_LIGHT = 0x4600,
  141. CHUNK_CAMERA = 0x4700,
  142. CHUNK_HIERARCHY = 0x4F00,
  143. // Specifies the global scaling factor. This is applied
  144. // to the root node's transformation matrix
  145. CHUNK_MASTER_SCALE = 0x0100,
  146. // ********************************************************************
  147. // Material chunks
  148. CHUNK_MAT_MATERIAL = 0xAFFF,
  149. // asciiz containing the name of the material
  150. CHUNK_MAT_MATNAME = 0xA000,
  151. CHUNK_MAT_AMBIENT = 0xA010, // followed by color chunk
  152. CHUNK_MAT_DIFFUSE = 0xA020, // followed by color chunk
  153. CHUNK_MAT_SPECULAR = 0xA030, // followed by color chunk
  154. // Specifies the shininess of the material
  155. // followed by percentage chunk
  156. CHUNK_MAT_SHININESS = 0xA040,
  157. CHUNK_MAT_SHININESS_PERCENT = 0xA041,
  158. // Specifies the shading mode to be used
  159. // followed by a short
  160. CHUNK_MAT_SHADING = 0xA100,
  161. // NOTE: Emissive color (self illumination) seems not
  162. // to be a color but a single value, type is unknown.
  163. // Make the parser accept both of them.
  164. // followed by percentage chunk (?)
  165. CHUNK_MAT_SELF_ILLUM = 0xA080,
  166. // Always followed by percentage chunk (?)
  167. CHUNK_MAT_SELF_ILPCT = 0xA084,
  168. // Always followed by percentage chunk
  169. CHUNK_MAT_TRANSPARENCY = 0xA050,
  170. // Diffuse texture channel 0
  171. CHUNK_MAT_TEXTURE = 0xA200,
  172. // Contains opacity information for each texel
  173. CHUNK_MAT_OPACMAP = 0xA210,
  174. // Contains a reflection map to be used to reflect
  175. // the environment. This is partially supported.
  176. CHUNK_MAT_REFLMAP = 0xA220,
  177. // Self Illumination map (emissive colors)
  178. CHUNK_MAT_SELFIMAP = 0xA33d,
  179. // Bumpmap. Not specified whether it is a heightmap
  180. // or a normal map. Assme it is a heightmap since
  181. // artist normally prefer this format.
  182. CHUNK_MAT_BUMPMAP = 0xA230,
  183. // Specular map. Seems to influence the specular color
  184. CHUNK_MAT_SPECMAP = 0xA204,
  185. // Holds shininess data.
  186. CHUNK_MAT_MAT_SHINMAP = 0xA33C,
  187. // Scaling in U/V direction.
  188. // (need to gen separate UV coordinate set
  189. // and do this by hand)
  190. CHUNK_MAT_MAP_USCALE = 0xA354,
  191. CHUNK_MAT_MAP_VSCALE = 0xA356,
  192. // Translation in U/V direction.
  193. // (need to gen separate UV coordinate set
  194. // and do this by hand)
  195. CHUNK_MAT_MAP_UOFFSET = 0xA358,
  196. CHUNK_MAT_MAP_VOFFSET = 0xA35a,
  197. // UV-coordinates rotation around the z-axis
  198. // Assumed to be in radians.
  199. CHUNK_MAT_MAP_ANG = 0xA35C,
  200. // Tiling flags for 3DS files
  201. CHUNK_MAT_MAP_TILING = 0xa351,
  202. // Specifies the file name of a texture
  203. CHUNK_MAPFILE = 0xA300,
  204. // Specifies whether a material requires two-sided rendering
  205. CHUNK_MAT_TWO_SIDE = 0xA081,
  206. // ********************************************************************
  207. // Main keyframer chunk. Contains translation/rotation/scaling data
  208. CHUNK_KEYFRAMER = 0xB000,
  209. // Supported sub chunks
  210. CHUNK_TRACKINFO = 0xB002,
  211. CHUNK_TRACKOBJNAME = 0xB010,
  212. CHUNK_TRACKDUMMYOBJNAME = 0xB011,
  213. CHUNK_TRACKPIVOT = 0xB013,
  214. CHUNK_TRACKPOS = 0xB020,
  215. CHUNK_TRACKROTATE = 0xB021,
  216. CHUNK_TRACKSCALE = 0xB022,
  217. // ********************************************************************
  218. // Keyframes for various other stuff in the file
  219. // Partially ignored
  220. CHUNK_AMBIENTKEY = 0xB001,
  221. CHUNK_TRACKMORPH = 0xB026,
  222. CHUNK_TRACKHIDE = 0xB029,
  223. CHUNK_OBJNUMBER = 0xB030,
  224. CHUNK_TRACKCAMERA = 0xB003,
  225. CHUNK_TRACKFOV = 0xB023,
  226. CHUNK_TRACKROLL = 0xB024,
  227. CHUNK_TRACKCAMTGT = 0xB004,
  228. CHUNK_TRACKLIGHT = 0xB005,
  229. CHUNK_TRACKLIGTGT = 0xB006,
  230. CHUNK_TRACKSPOTL = 0xB007,
  231. CHUNK_FRAMES = 0xB008,
  232. // ********************************************************************
  233. // light sub-chunks
  234. CHUNK_DL_OFF = 0x4620,
  235. CHUNK_DL_OUTER_RANGE = 0x465A,
  236. CHUNK_DL_INNER_RANGE = 0x4659,
  237. CHUNK_DL_MULTIPLIER = 0x465B,
  238. CHUNK_DL_EXCLUDE = 0x4654,
  239. CHUNK_DL_ATTENUATE = 0x4625,
  240. CHUNK_DL_SPOTLIGHT = 0x4610,
  241. // camera sub-chunks
  242. CHUNK_CAM_RANGES = 0x4720
  243. };
  244. }
  245. // ---------------------------------------------------------------------------
  246. /** Helper structure representing a 3ds mesh face */
  247. struct Face : public FaceWithSmoothingGroup {
  248. };
  249. #ifdef _MSC_VER
  250. #pragma warning(push)
  251. #pragma warning(disable : 4315)
  252. #endif // _MSC_VER
  253. // ---------------------------------------------------------------------------
  254. /** Helper structure representing a texture */
  255. struct Texture {
  256. //! Default constructor
  257. Texture() AI_NO_EXCEPT
  258. : mTextureBlend(0.0f),
  259. mOffsetU(0.0),
  260. mOffsetV(0.0),
  261. mScaleU(1.0),
  262. mScaleV(1.0),
  263. mRotation(0.0),
  264. mMapMode(aiTextureMapMode_Wrap),
  265. bPrivate(),
  266. iUVSrc(0) {
  267. mTextureBlend = get_qnan();
  268. }
  269. Texture(const Texture &other) = default;
  270. Texture(Texture &&other) AI_NO_EXCEPT = default;
  271. Texture &operator=(Texture &&other) AI_NO_EXCEPT = default;
  272. //! Specifies the blend factor for the texture
  273. ai_real mTextureBlend;
  274. //! Specifies the filename of the texture
  275. std::string mMapName;
  276. //! Specifies texture coordinate offsets/scaling/rotations
  277. ai_real mOffsetU;
  278. ai_real mOffsetV;
  279. ai_real mScaleU;
  280. ai_real mScaleV;
  281. ai_real mRotation;
  282. //! Specifies the mapping mode to be used for the texture
  283. aiTextureMapMode mMapMode;
  284. //! Used internally
  285. bool bPrivate;
  286. int iUVSrc;
  287. };
  288. #include <assimp/Compiler/poppack1.h>
  289. #ifdef _MSC_VER
  290. #pragma warning(pop)
  291. #endif // _MSC_VER
  292. // ---------------------------------------------------------------------------
  293. /** Helper structure representing a 3ds material */
  294. struct Material {
  295. //! Default constructor has been deleted
  296. Material() :
  297. mName(),
  298. mDiffuse(0.6f, 0.6f, 0.6f),
  299. mSpecularExponent(ai_real(0.0)),
  300. mShininessStrength(ai_real(1.0)),
  301. mShading(Discreet3DS::Gouraud),
  302. mTransparency(ai_real(1.0)),
  303. mBumpHeight(ai_real(1.0)),
  304. mTwoSided(false) {
  305. // empty
  306. }
  307. //! Constructor with explicit name
  308. explicit Material(const std::string &name) :
  309. mName(name),
  310. mDiffuse(0.6f, 0.6f, 0.6f),
  311. mSpecularExponent(ai_real(0.0)),
  312. mShininessStrength(ai_real(1.0)),
  313. mShading(Discreet3DS::Gouraud),
  314. mTransparency(ai_real(1.0)),
  315. mBumpHeight(ai_real(1.0)),
  316. mTwoSided(false) {
  317. // empty
  318. }
  319. Material(const Material &other) = default;
  320. virtual ~Material() = default;
  321. //! Name of the material
  322. std::string mName;
  323. //! Diffuse color of the material
  324. aiColor3D mDiffuse;
  325. //! Specular exponent
  326. ai_real mSpecularExponent;
  327. //! Shininess strength, in percent
  328. ai_real mShininessStrength;
  329. //! Specular color of the material
  330. aiColor3D mSpecular;
  331. //! Ambient color of the material
  332. aiColor3D mAmbient;
  333. //! Shading type to be used
  334. Discreet3DS::shadetype3ds mShading;
  335. //! Opacity of the material
  336. ai_real mTransparency;
  337. //! Diffuse texture channel
  338. Texture sTexDiffuse;
  339. //! Opacity texture channel
  340. Texture sTexOpacity;
  341. //! Specular texture channel
  342. Texture sTexSpecular;
  343. //! Reflective texture channel
  344. Texture sTexReflective;
  345. //! Bump texture channel
  346. Texture sTexBump;
  347. //! Emissive texture channel
  348. Texture sTexEmissive;
  349. //! Shininess texture channel
  350. Texture sTexShininess;
  351. //! Scaling factor for the bump values
  352. ai_real mBumpHeight;
  353. //! Emissive color
  354. aiColor3D mEmissive;
  355. //! Ambient texture channel
  356. //! (used by the ASE format)
  357. Texture sTexAmbient;
  358. //! True if the material must be rendered from two sides
  359. bool mTwoSided;
  360. };
  361. // ---------------------------------------------------------------------------
  362. /** Helper structure to represent a 3ds file mesh */
  363. struct Mesh : public MeshWithSmoothingGroups<D3DS::Face> {
  364. //! Default constructor has been deleted
  365. Mesh() = delete;
  366. //! Constructor with explicit name
  367. explicit Mesh(const std::string &name) :
  368. mName(name) {
  369. }
  370. //! Name of the mesh
  371. std::string mName;
  372. //! Texture coordinates
  373. std::vector<aiVector3D> mTexCoords;
  374. //! Face materials
  375. std::vector<unsigned int> mFaceMaterials;
  376. //! Local transformation matrix
  377. aiMatrix4x4 mMat;
  378. };
  379. // ---------------------------------------------------------------------------
  380. /** Float key - quite similar to aiVectorKey and aiQuatKey. Both are in the
  381. C-API, so it would be difficult to make them a template. */
  382. struct aiFloatKey {
  383. double mTime; ///< The time of this key
  384. ai_real mValue; ///< The value of this key
  385. #ifdef __cplusplus
  386. // time is not compared
  387. bool operator==(const aiFloatKey &o) const { return o.mValue == this->mValue; }
  388. bool operator!=(const aiFloatKey &o) const { return o.mValue != this->mValue; }
  389. // Only time is compared. This operator is defined
  390. // for use with std::sort
  391. bool operator<(const aiFloatKey &o) const { return mTime < o.mTime; }
  392. bool operator>(const aiFloatKey &o) const { return mTime > o.mTime; }
  393. #endif
  394. };
  395. // ---------------------------------------------------------------------------
  396. /** Helper structure to represent a 3ds file node */
  397. struct Node {
  398. Node() = delete;
  399. explicit Node(const std::string &name) :
  400. mParent(nullptr),
  401. mName(name),
  402. mInstanceNumber(0),
  403. mHierarchyPos(0),
  404. mHierarchyIndex(0),
  405. mInstanceCount(1) {
  406. aRotationKeys.reserve(20);
  407. aPositionKeys.reserve(20);
  408. aScalingKeys.reserve(20);
  409. }
  410. ~Node() {
  411. for (unsigned int i = 0; i < mChildren.size(); ++i)
  412. delete mChildren[i];
  413. }
  414. //! Pointer to the parent node
  415. Node *mParent;
  416. //! Holds all child nodes
  417. std::vector<Node *> mChildren;
  418. //! Name of the node
  419. std::string mName;
  420. //! InstanceNumber of the node
  421. int32_t mInstanceNumber;
  422. //! Dummy nodes: real name to be combined with the $$$DUMMY
  423. std::string mDummyName;
  424. //! Position of the node in the hierarchy (tree depth)
  425. int16_t mHierarchyPos;
  426. //! Index of the node
  427. int16_t mHierarchyIndex;
  428. //! Rotation keys loaded from the file
  429. std::vector<aiQuatKey> aRotationKeys;
  430. //! Position keys loaded from the file
  431. std::vector<aiVectorKey> aPositionKeys;
  432. //! Scaling keys loaded from the file
  433. std::vector<aiVectorKey> aScalingKeys;
  434. // For target lights (spot lights and directional lights):
  435. // The position of the target
  436. std::vector<aiVectorKey> aTargetPositionKeys;
  437. // For cameras: the camera roll angle
  438. std::vector<aiFloatKey> aCameraRollKeys;
  439. //! Pivot position loaded from the file
  440. aiVector3D vPivot;
  441. //instance count, will be kept only for the first node
  442. int32_t mInstanceCount;
  443. //! Add a child node, setup the right parent node for it
  444. //! \param pc Node to be 'adopted'
  445. inline Node &push_back(Node *pc) {
  446. mChildren.push_back(pc);
  447. pc->mParent = this;
  448. return *this;
  449. }
  450. };
  451. // ---------------------------------------------------------------------------
  452. /** Helper structure analogue to aiScene */
  453. struct Scene {
  454. //! List of all materials loaded
  455. //! NOTE: 3ds references materials globally
  456. std::vector<Material> mMaterials;
  457. //! List of all meshes loaded
  458. std::vector<Mesh> mMeshes;
  459. //! List of all cameras loaded
  460. std::vector<aiCamera *> mCameras;
  461. //! List of all lights loaded
  462. std::vector<aiLight *> mLights;
  463. //! Pointer to the root node of the scene
  464. // --- moved to main class
  465. // Node* pcRootNode;
  466. };
  467. } // end of namespace Assimp::D3DS
  468. #endif // AI_XFILEHELPER_H_INC