3DSHelper.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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. :
  301. mDiffuse (0.6,0.6,0.6), // FIX ... we won't want object to be black
  302. mSpecularExponent (0.0),
  303. mShininessStrength (1.0),
  304. mShading(Discreet3DS::Gouraud),
  305. mTransparency (1.0),
  306. mBumpHeight (1.0),
  307. mTwoSided (false)
  308. {
  309. static int iCnt = 0;
  310. char szTemp[128];
  311. ai_snprintf(szTemp, 128, "UNNAMED_%i",iCnt++);
  312. mName = szTemp;
  313. }
  314. //! Name of the material
  315. std::string mName;
  316. //! Diffuse color of the material
  317. aiColor3D mDiffuse;
  318. //! Specular exponent
  319. ai_real mSpecularExponent;
  320. //! Shininess strength, in percent
  321. ai_real mShininessStrength;
  322. //! Specular color of the material
  323. aiColor3D mSpecular;
  324. //! Ambient color of the material
  325. aiColor3D mAmbient;
  326. //! Shading type to be used
  327. Discreet3DS::shadetype3ds mShading;
  328. //! Opacity of the material
  329. ai_real mTransparency;
  330. //! Diffuse texture channel
  331. Texture sTexDiffuse;
  332. //! Opacity texture channel
  333. Texture sTexOpacity;
  334. //! Specular texture channel
  335. Texture sTexSpecular;
  336. //! Reflective texture channel
  337. Texture sTexReflective;
  338. //! Bump texture channel
  339. Texture sTexBump;
  340. //! Emissive texture channel
  341. Texture sTexEmissive;
  342. //! Shininess texture channel
  343. Texture sTexShininess;
  344. //! Scaling factor for the bump values
  345. ai_real mBumpHeight;
  346. //! Emissive color
  347. aiColor3D mEmissive;
  348. //! Ambient texture channel
  349. //! (used by the ASE format)
  350. Texture sTexAmbient;
  351. //! True if the material must be rendered from two sides
  352. bool mTwoSided;
  353. };
  354. // ---------------------------------------------------------------------------
  355. /** Helper structure to represent a 3ds file mesh */
  356. struct Mesh : public MeshWithSmoothingGroups<D3DS::Face>
  357. {
  358. //! Default constructor
  359. Mesh()
  360. {
  361. static int iCnt = 0;
  362. // Generate a default name for the mesh
  363. char szTemp[128];
  364. ai_snprintf(szTemp, 128, "UNNAMED_%i",iCnt++);
  365. mName = szTemp;
  366. }
  367. //! Name of the mesh
  368. std::string mName;
  369. //! Texture coordinates
  370. std::vector<aiVector3D> mTexCoords;
  371. //! Face materials
  372. std::vector<unsigned int> mFaceMaterials;
  373. //! Local transformation matrix
  374. aiMatrix4x4 mMat;
  375. };
  376. // ---------------------------------------------------------------------------
  377. /** Float key - quite similar to aiVectorKey and aiQuatKey. Both are in the
  378. C-API, so it would be difficult to make them a template. */
  379. struct aiFloatKey
  380. {
  381. double mTime; ///< The time of this key
  382. ai_real mValue; ///< The value of this key
  383. #ifdef __cplusplus
  384. // time is not compared
  385. bool operator == (const aiFloatKey& o) const
  386. {return o.mValue == this->mValue;}
  387. bool operator != (const aiFloatKey& o) const
  388. {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
  392. {return mTime < o.mTime;}
  393. bool operator > (const aiFloatKey& o) const
  394. {return mTime > o.mTime;}
  395. #endif
  396. };
  397. // ---------------------------------------------------------------------------
  398. /** Helper structure to represent a 3ds file node */
  399. struct Node
  400. {
  401. Node():
  402. mParent(NULL)
  403. , mInstanceNumber(0)
  404. , mHierarchyPos (0)
  405. , mHierarchyIndex (0)
  406. , mInstanceCount (1)
  407. {
  408. static int iCnt = 0;
  409. // Generate a default name for the node
  410. char szTemp[128];
  411. ::ai_snprintf(szTemp, 128, "UNNAMED_%i",iCnt++);
  412. mName = szTemp;
  413. aRotationKeys.reserve (20);
  414. aPositionKeys.reserve (20);
  415. aScalingKeys.reserve (20);
  416. }
  417. ~Node()
  418. {
  419. for (unsigned int i = 0; i < mChildren.size();++i)
  420. delete mChildren[i];
  421. }
  422. //! Pointer to the parent node
  423. Node* mParent;
  424. //! Holds all child nodes
  425. std::vector<Node*> mChildren;
  426. //! Name of the node
  427. std::string mName;
  428. //! InstanceNumber of the node
  429. int32_t mInstanceNumber;
  430. //! Dummy nodes: real name to be combined with the $$$DUMMY
  431. std::string mDummyName;
  432. //! Position of the node in the hierarchy (tree depth)
  433. int16_t mHierarchyPos;
  434. //! Index of the node
  435. int16_t mHierarchyIndex;
  436. //! Rotation keys loaded from the file
  437. std::vector<aiQuatKey> aRotationKeys;
  438. //! Position keys loaded from the file
  439. std::vector<aiVectorKey> aPositionKeys;
  440. //! Scaling keys loaded from the file
  441. std::vector<aiVectorKey> aScalingKeys;
  442. // For target lights (spot lights and directional lights):
  443. // The position of the target
  444. std::vector< aiVectorKey > aTargetPositionKeys;
  445. // For cameras: the camera roll angle
  446. std::vector< aiFloatKey > aCameraRollKeys;
  447. //! Pivot position loaded from the file
  448. aiVector3D vPivot;
  449. //instance count, will be kept only for the first node
  450. int32_t mInstanceCount;
  451. //! Add a child node, setup the right parent node for it
  452. //! \param pc Node to be 'adopted'
  453. inline Node& push_back(Node* pc)
  454. {
  455. mChildren.push_back(pc);
  456. pc->mParent = this;
  457. return *this;
  458. }
  459. };
  460. // ---------------------------------------------------------------------------
  461. /** Helper structure analogue to aiScene */
  462. struct Scene
  463. {
  464. //! List of all materials loaded
  465. //! NOTE: 3ds references materials globally
  466. std::vector<Material> mMaterials;
  467. //! List of all meshes loaded
  468. std::vector<Mesh> mMeshes;
  469. //! List of all cameras loaded
  470. std::vector<aiCamera*> mCameras;
  471. //! List of all lights loaded
  472. std::vector<aiLight*> mLights;
  473. //! Pointer to the root node of the scene
  474. // --- moved to main class
  475. // Node* pcRootNode;
  476. };
  477. } // end of namespace D3DS
  478. } // end of namespace Assimp
  479. #endif // AI_XFILEHELPER_H_INC