3DSHelper.h 18 KB

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