3DSHelper.h 16 KB

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