3DSHelper.h 16 KB

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