3DSHelper.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /*
  2. Open Asset Import Library (ASSIMP)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2008, ASSIMP Development 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 Development 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 the helper data structures for importing XFiles */
  34. #ifndef AI_3DSFILEHELPER_H_INC
  35. #define AI_3DSFILEHELPER_H_INC
  36. #include <string>
  37. #include <vector>
  38. #include <sstream>
  39. #include "../include/aiTypes.h"
  40. #include "../include/aiQuaternion.h"
  41. #include "../include/aiMesh.h"
  42. #include "../include/aiAnim.h"
  43. #include "../include/aiMaterial.h"
  44. #include "SpatialSort.h"
  45. namespace Assimp
  46. {
  47. namespace Dot3DS
  48. {
  49. #if defined(_MSC_VER) || defined(__BORLANDC__) || defined (__BCPLUSPLUS__)
  50. # pragma pack(push,2)
  51. # define PACK_STRUCT
  52. #elif defined( __GNUC__ )
  53. # define PACK_STRUCT __attribute__((packed))
  54. #else
  55. # error Compiler not supported
  56. #endif
  57. // ---------------------------------------------------------------------------
  58. /** Dot3DSFile class: Helper class for loading 3ds files. Defines chunks
  59. * and data structures.
  60. */
  61. class Dot3DSFile
  62. {
  63. public:
  64. inline Dot3DSFile() {}
  65. //! data structure for a single chunk in a .3ds file
  66. struct Chunk
  67. {
  68. unsigned short Flag;
  69. long Size;
  70. } PACK_STRUCT;
  71. //! source for this used own structures,
  72. //! replaced it with out standard math helpers
  73. typedef aiMatrix3x3 MatTransform;
  74. typedef aiVector3D MatTranslate;
  75. //! Used for shading field in material3ds structure
  76. //! From AutoDesk 3ds SDK
  77. typedef enum
  78. {
  79. // translated to gouraud shading with wireframe active
  80. Wire = 0,
  81. // if this material is set, no vertex normals will
  82. // be calculated for the model. Face normals + gouraud
  83. Flat = 1,
  84. // standard gouraud shading
  85. Gouraud = 2,
  86. // phong shading
  87. Phong = 3,
  88. // cooktorrance or anistropic phong shading ...
  89. // the exact meaning is unknown, if you know it
  90. // feel free to tell me ;-)
  91. Metal = 4,
  92. // required by the ASE loader
  93. Blinn = 5
  94. } shadetype3ds;
  95. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  96. // enum for all chunks in 3ds files. Unused
  97. // ones are commented, list is not complete since
  98. // there are many undocumented chunks.
  99. //
  100. // Links: http://www.jalix.org/ressources/graphics/3DS/_unofficials/3ds-unofficial.txt
  101. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  102. enum
  103. {
  104. // **************************************************************
  105. // Base chunks which can be found everywhere in the file
  106. CHUNK_VERSION = 0x0002,
  107. CHUNK_RGBF = 0x0010, // float4 R; float4 G; float4 B
  108. CHUNK_RGBB = 0x0011, // int1 R; int1 G; int B
  109. // Linear color values (gamma = 2.2?)
  110. CHUNK_LINRGBF = 0x0013, // float4 R; float4 G; float4 B
  111. CHUNK_LINRGBB = 0x0012, // int1 R; int1 G; int B
  112. CHUNK_PERCENTW = 0x0030, // int2 percentage
  113. CHUNK_PERCENTF = 0x0031, // float4 percentage
  114. // **************************************************************
  115. // Unknown and ignored. Possibly a chunk used by PROJ (
  116. // Discreet 3DS max Project File)?
  117. CHUNK_PRJ = 0xC23D,
  118. // Unknown. Possibly a reference to an external .mli file?
  119. CHUNK_MLI = 0x3DAA,
  120. // Primary main chunk of the .3ds file
  121. CHUNK_MAIN = 0x4D4D,
  122. // Mesh main chunk
  123. CHUNK_OBJMESH = 0x3D3D,
  124. // Specifies the background color of the .3ds file
  125. // This is passed through the material system for
  126. // viewing purposes.
  127. CHUNK_BKGCOLOR = 0x1200,
  128. // Specifies the ambient base color of the scene.
  129. // This is added to all materials in the file
  130. CHUNK_AMBCOLOR = 0x2100,
  131. // Specifies the background image for the whole scene
  132. // This value is passed through the material system
  133. // to the viewer
  134. CHUNK_BIT_MAP = 0x1100,
  135. CHUNK_BIT_MAP_EXISTS = 0x1101,
  136. // **************************************************************
  137. // Viewport related stuff. Ignored
  138. CHUNK_DEFAULT_VIEW = 0x3000,
  139. CHUNK_VIEW_TOP = 0x3010,
  140. CHUNK_VIEW_BOTTOM = 0x3020,
  141. CHUNK_VIEW_LEFT = 0x3030,
  142. CHUNK_VIEW_RIGHT = 0x3040,
  143. CHUNK_VIEW_FRONT = 0x3050,
  144. CHUNK_VIEW_BACK = 0x3060,
  145. CHUNK_VIEW_USER = 0x3070,
  146. CHUNK_VIEW_CAMERA = 0x3080,
  147. // **************************************************************
  148. // Mesh chunks
  149. CHUNK_OBJBLOCK = 0x4000,
  150. CHUNK_TRIMESH = 0x4100,
  151. CHUNK_VERTLIST = 0x4110,
  152. CHUNK_VERTFLAGS = 0x4111,
  153. CHUNK_FACELIST = 0x4120,
  154. CHUNK_FACEMAT = 0x4130,
  155. CHUNK_MAPLIST = 0x4140,
  156. CHUNK_SMOOLIST = 0x4150,
  157. CHUNK_TRMATRIX = 0x4160,
  158. CHUNK_MESHCOLOR = 0x4165,
  159. CHUNK_TXTINFO = 0x4170,
  160. CHUNK_LIGHT = 0x4600,
  161. CHUNK_SPOTLIGHT = 0x4610,
  162. CHUNK_CAMERA = 0x4700,
  163. CHUNK_HIERARCHY = 0x4F00,
  164. // Specifies the global scaling factor. This is applied
  165. // to the root node's transformation matrix
  166. CHUNK_MASTER_SCALE = 0x0100,
  167. // **************************************************************
  168. // Material chunks
  169. CHUNK_MAT_MATERIAL = 0xAFFF,
  170. // asciiz containing the name of the material
  171. CHUNK_MAT_MATNAME = 0xA000,
  172. CHUNK_MAT_AMBIENT = 0xA010, // followed by color chunk
  173. CHUNK_MAT_DIFFUSE = 0xA020, // followed by color chunk
  174. CHUNK_MAT_SPECULAR = 0xA030, // followed by color chunk
  175. // Specifies the shininess of the material
  176. // followed by percentage chunk
  177. CHUNK_MAT_SHININESS = 0xA040,
  178. CHUNK_MAT_SHININESS_PERCENT = 0xA041 ,
  179. // Specifies the shading mode to be used
  180. // followed by a short
  181. CHUNK_MAT_SHADING = 0xA100,
  182. // NOTE: Emissive color (self illumination) seems not
  183. // to be a color but a single value, type is unknown.
  184. // Make the parser accept both of them.
  185. // followed by percentage chunk (?)
  186. CHUNK_MAT_SELF_ILLUM = 0xA080,
  187. // Always followed by percentage chunk (?)
  188. CHUNK_MAT_SELF_ILPCT = 0xA084,
  189. // Always followed by percentage chunk
  190. CHUNK_MAT_TRANSPARENCY = 0xA050,
  191. // Diffuse texture channel 0
  192. CHUNK_MAT_TEXTURE = 0xA200,
  193. // Contains opacity information for each texel
  194. CHUNK_MAT_OPACMAP = 0xA210,
  195. // Contains a reflection map to be used to reflect
  196. // the environment. This is partially supported.
  197. CHUNK_MAT_REFLMAP = 0xA220,
  198. // Self Illumination map (emissive colors)
  199. CHUNK_MAT_SELFIMAP = 0xA33d,
  200. // Bumpmap. Not specified whether it is a heightmap
  201. // or a normal map. Assme it is a heightmap since
  202. // artist normally prefer this format.
  203. CHUNK_MAT_BUMPMAP = 0xA230,
  204. // Specular map. Seems to influence the specular color
  205. CHUNK_MAT_SPECMAP = 0xA204,
  206. // Holds shininess data. I assume the specular exponent is
  207. // calculated like this:
  208. //
  209. // s[x,y] = stex[x,y] * base_shininess;
  210. // I also assume the data in the texture must be renormalized
  211. // (normally by dividing / 255) after loading.
  212. CHUNK_MAT_MAT_SHINMAP = 0xA33C,
  213. // Scaling in U/V direction.
  214. // (need to gen separate UV coordinate set
  215. // and do this by hand)
  216. CHUNK_MAT_MAP_USCALE = 0xA354,
  217. CHUNK_MAT_MAP_VSCALE = 0xA356,
  218. // Translation in U/V direction.
  219. // (need to gen separate UV coordinate set
  220. // and do this by hand)
  221. CHUNK_MAT_MAP_UOFFSET = 0xA358,
  222. CHUNK_MAT_MAP_VOFFSET = 0xA35a,
  223. // UV-coordinates rotation around the z-axis
  224. // Assumed to be in radians.
  225. CHUNK_MAT_MAP_ANG = 0xA35C,
  226. // Tiling flags for 3DS files
  227. CHUNK_MAT_MAP_TILING = 0xa351,
  228. // Specifies the file name of a texture
  229. CHUNK_MAPFILE = 0xA300,
  230. // Specifies whether a materail requires two-sided rendering
  231. CHUNK_MAT_TWO_SIDE = 0xA081,
  232. // **************************************************************
  233. // Main keyframer chunk. Contains translation/rotation/scaling data
  234. CHUNK_KEYFRAMER = 0xB000,
  235. // Supported sub chunks
  236. CHUNK_TRACKINFO = 0xB002,
  237. CHUNK_TRACKOBJNAME = 0xB010,
  238. CHUNK_TRACKPIVOT = 0xB013,
  239. CHUNK_TRACKPOS = 0xB020,
  240. CHUNK_TRACKROTATE = 0xB021,
  241. CHUNK_TRACKSCALE = 0xB022,
  242. // **************************************************************
  243. // Keyframes for various other stuff in the file
  244. // Ignored
  245. CHUNK_AMBIENTKEY = 0xB001,
  246. CHUNK_TRACKMORPH = 0xB026,
  247. CHUNK_TRACKHIDE = 0xB029,
  248. CHUNK_OBJNUMBER = 0xB030,
  249. CHUNK_TRACKCAMERA = 0xB003,
  250. CHUNK_TRACKFOV = 0xB023,
  251. CHUNK_TRACKROLL = 0xB024,
  252. CHUNK_TRACKCAMTGT = 0xB004,
  253. CHUNK_TRACKLIGHT = 0xB005,
  254. CHUNK_TRACKLIGTGT = 0xB006,
  255. CHUNK_TRACKSPOTL = 0xB007,
  256. CHUNK_FRAMES = 0xB008
  257. // **************************************************************
  258. };
  259. };
  260. #if defined(_MSC_VER) || defined(__BORLANDC__) || defined (__BCPLUSPLUS__)
  261. # pragma pack( pop )
  262. #endif
  263. #undef PACK_STRUCT
  264. // ---------------------------------------------------------------------------
  265. /** Helper structure representing a 3ds mesh face */
  266. struct Face
  267. {
  268. Face() : iSmoothGroup(0), bFlipped(false)
  269. {
  270. // let the rest uninitialized for performance
  271. this->mIndices[0] = 0;
  272. this->mIndices[1] = 0;
  273. this->mIndices[2] = 0;
  274. }
  275. //! Indices. .3ds is using uint16. However, after
  276. //! an unique vrtex set has been geneerated it might
  277. //! be an index becomes > 2^16
  278. uint32_t mIndices[3];
  279. //! specifies to which smoothing group the face belongs to
  280. uint32_t iSmoothGroup;
  281. //! Specifies that the face normal must be flipped
  282. bool bFlipped;
  283. };
  284. // ---------------------------------------------------------------------------
  285. /** Helper structure representing a texture */
  286. struct Texture
  287. {
  288. //! Default constructor
  289. Texture()
  290. :
  291. mScaleU(1.0f),
  292. mScaleV(1.0f),
  293. mOffsetU(0.0f),
  294. mOffsetV(0.0f),
  295. mRotation(0.0f),
  296. iUVSrc(0),
  297. mMapMode(aiTextureMapMode_Wrap)
  298. {
  299. mTextureBlend = std::numeric_limits<float>::quiet_NaN();
  300. }
  301. //! Specifies the blending factor for the texture
  302. float mTextureBlend;
  303. //! Specifies the filename of the texture
  304. std::string mMapName;
  305. //! Specifies texture coordinate offsets/scaling/rotations
  306. float mScaleU;
  307. float mScaleV;
  308. float mOffsetU;
  309. float mOffsetV;
  310. float mRotation;
  311. //! Specifies the mapping mode to be used for the texture
  312. aiTextureMapMode mMapMode;
  313. //! Used internally
  314. bool bPrivate;
  315. int iUVSrc;
  316. };
  317. // ---------------------------------------------------------------------------
  318. /** Helper structure representing a 3ds material */
  319. struct Material
  320. {
  321. //! Default constructor. Builds a default name for the material
  322. Material()
  323. :
  324. mSpecularExponent (0.0f),
  325. mShading(Dot3DSFile::Gouraud),
  326. mTransparency (1.0f),
  327. mBumpHeight (1.0f),
  328. iBakeUVTransform (0),
  329. pcSingleTexture (NULL),
  330. mShininessStrength (1.0f),
  331. mTwoSided (false)
  332. {
  333. static int iCnt = 0;
  334. char szTemp[128];
  335. sprintf(szTemp,"$$_UNNAMED_%i_$$",iCnt++);
  336. mName = szTemp;
  337. }
  338. //! Name of the material
  339. std::string mName;
  340. //! Diffuse color of the material
  341. aiColor3D mDiffuse;
  342. //! Specular exponent
  343. float mSpecularExponent;
  344. //! Shininess strength, in percent
  345. float mShininessStrength;
  346. //! Specular color of the material
  347. aiColor3D mSpecular;
  348. //! Ambient color of the material
  349. aiColor3D mAmbient;
  350. //! Shading type to be used
  351. Dot3DSFile::shadetype3ds mShading;
  352. //! Opacity of the material
  353. float mTransparency;
  354. //! Diffuse texture channel
  355. Texture sTexDiffuse;
  356. //! Opacity texture channel
  357. Texture sTexOpacity;
  358. //! Specular texture channel
  359. Texture sTexSpecular;
  360. //! Bump texture channel
  361. Texture sTexBump;
  362. //! Emissive texture channel
  363. Texture sTexEmissive;
  364. //! Shininess texture channel
  365. Texture sTexShininess;
  366. //! Scaling factor for the bump values
  367. float mBumpHeight;
  368. //! Emissive color
  369. aiColor3D mEmissive;
  370. //! Ambient texture channel
  371. //! (used by the ASE format)
  372. Texture sTexAmbient;
  373. //! True if the material must be rendered from two sides
  374. bool mTwoSided;
  375. //! Used internally
  376. unsigned int iBakeUVTransform;
  377. Texture* pcSingleTexture;
  378. };
  379. // ---------------------------------------------------------------------------
  380. /** Helper structure to represent a 3ds file mesh */
  381. struct Mesh
  382. {
  383. //! Default constructor
  384. Mesh()
  385. {
  386. static int iCnt = 0;
  387. char szTemp[128];
  388. #if _MSC_VER >= 1400
  389. ::sprintf_s(szTemp,"$$_UNNAMED_%i_$$",iCnt++);
  390. #else
  391. ::sprintf(szTemp,"$$_UNNAMED_%i_$$",iCnt++);
  392. #endif
  393. mName = szTemp;
  394. }
  395. //! Name of the mesh
  396. std::string mName;
  397. //! Vertex positions
  398. std::vector<aiVector3D> mPositions;
  399. //! Face lists
  400. std::vector<Face> mFaces;
  401. //! Texture coordinates
  402. std::vector<aiVector2D> mTexCoords;
  403. //! Face materials
  404. std::vector<unsigned int> mFaceMaterials;
  405. //! Normal vectors
  406. std::vector<aiVector3D> mNormals;
  407. //! Local transformation matrix
  408. aiMatrix4x4 mMat;
  409. };
  410. // ---------------------------------------------------------------------------
  411. /** Helper structure to represent a 3ds file node */
  412. struct Node
  413. {
  414. Node()
  415. : mHierarchyPos(0),mHierarchyIndex(0)
  416. {
  417. static int iCnt = 0;
  418. char szTemp[128];
  419. #if _MSC_VER >= 1400
  420. ::sprintf_s(szTemp,"$$_UNNAMED_%i_$$",iCnt++);
  421. #else
  422. ::sprintf(szTemp,"$$_UNNAMED_%i_$$",iCnt++);
  423. #endif
  424. mName = szTemp;
  425. #ifdef AI_3DS_KEYFRAME_ANIMATION
  426. aRotationKeys.reserve(10);
  427. aPositionKeys.reserve(10);
  428. aScalingKeys.reserve(10);
  429. #endif
  430. }
  431. //! Pointer to the parent node
  432. Node* mParent;
  433. //! Holds all child nodes
  434. std::vector<Node*> mChildren;
  435. //! Name of the node
  436. std::string mName;
  437. //! Position of the node in the hierarchy (tree depth)
  438. int16_t mHierarchyPos;
  439. //! Index of the node
  440. int16_t mHierarchyIndex;
  441. #ifdef AI_3DS_KEYFRAME_ANIMATION
  442. //! Rotation keys loaded from the file
  443. std::vector<aiQuatKey> aRotationKeys;
  444. //! Position keys loaded from the file
  445. std::vector<aiVectorKey> aPositionKeys;
  446. //! Scaling keys loaded from the file
  447. std::vector<aiVectorKey> aScalingKeys;
  448. #endif
  449. //! Pivot position loaded from the file
  450. aiVector3D vPivot;
  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. //pc->mHierarchyPos = this->mHierarchyPos+1;
  458. return *this;
  459. }
  460. };
  461. // ---------------------------------------------------------------------------
  462. /** Helper structure analogue to aiScene */
  463. struct Scene
  464. {
  465. //! List of all materials loaded
  466. //! NOTE: 3ds references materials globally
  467. std::vector<Material> mMaterials;
  468. //! List of all meshes loaded
  469. std::vector<Mesh> mMeshes;
  470. //! Pointer to the root node of the scene
  471. Node* pcRootNode;
  472. };
  473. } // end of namespace Dot3DS
  474. } // end of namespace Assimp
  475. #endif // AI_XFILEHELPER_H_INC