3DSHelper.h 18 KB

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