3DSHelper.h 18 KB

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