3DSHelper.h 18 KB

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