3DSHelper.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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 helper data structures for the import of 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. #include "SmoothingGroups.h"
  47. namespace Assimp {
  48. namespace Dot3DS {
  49. #include "./../include/Compiler/pushpack1.h"
  50. // ---------------------------------------------------------------------------
  51. /** Dot3DSFile class: Helper class for loading 3ds files. Defines chunks
  52. * and data structures.
  53. */
  54. // ---------------------------------------------------------------------------
  55. class Dot3DSFile
  56. {
  57. public:
  58. inline Dot3DSFile() {}
  59. //! data structure for a single chunk in a .3ds file
  60. struct Chunk
  61. {
  62. uint16_t Flag;
  63. uint32_t Size;
  64. } PACK_STRUCT;
  65. //! source for this used own structures,
  66. //! replaced it with out standard math helpers
  67. typedef aiMatrix3x3 MatTransform;
  68. typedef aiVector3D MatTranslate;
  69. //! Used for shading field in material3ds structure
  70. //! From AutoDesk 3ds SDK
  71. typedef enum
  72. {
  73. // translated to gouraud shading with wireframe active
  74. Wire = 0x0,
  75. // if this material is set, no vertex normals will
  76. // be calculated for the model. Face normals + gouraud
  77. Flat = 0x1,
  78. // standard gouraud shading
  79. Gouraud = 0x2,
  80. // phong shading
  81. Phong = 0x3,
  82. // cooktorrance or anistropic phong shading ...
  83. // the exact meaning is unknown, if you know it
  84. // feel free to tell me ;-)
  85. Metal = 0x4,
  86. // required by the ASE loader
  87. Blinn = 0x5
  88. } shadetype3ds;
  89. enum
  90. {
  91. // ********************************************************************
  92. // Basic chunks which can be found everywhere in the file
  93. CHUNK_VERSION = 0x0002,
  94. CHUNK_RGBF = 0x0010, // float4 R; float4 G; float4 B
  95. CHUNK_RGBB = 0x0011, // int1 R; int1 G; int B
  96. // Linear color values (gamma = 2.2?)
  97. CHUNK_LINRGBF = 0x0013, // float4 R; float4 G; float4 B
  98. CHUNK_LINRGBB = 0x0012, // int1 R; int1 G; int B
  99. CHUNK_PERCENTW = 0x0030, // int2 percentage
  100. CHUNK_PERCENTF = 0x0031, // float4 percentage
  101. // ********************************************************************
  102. // Unknown and ignored. Possibly a chunk used by PROJ (
  103. // Discreet 3DS max Project File)?
  104. CHUNK_PRJ = 0xC23D,
  105. // Unknown. Possibly a reference to an external .mli file?
  106. CHUNK_MLI = 0x3DAA,
  107. // Primary main chunk of the .3ds file
  108. CHUNK_MAIN = 0x4D4D,
  109. // Mesh main chunk
  110. CHUNK_OBJMESH = 0x3D3D,
  111. // Specifies the background color of the .3ds file
  112. // This is passed through the material system for
  113. // viewing purposes.
  114. CHUNK_BKGCOLOR = 0x1200,
  115. // Specifies the ambient base color of the scene.
  116. // This is added to all materials in the file
  117. CHUNK_AMBCOLOR = 0x2100,
  118. // Specifies the background image for the whole scene
  119. // This value is passed through the material system
  120. // to the viewer
  121. CHUNK_BIT_MAP = 0x1100,
  122. CHUNK_BIT_MAP_EXISTS = 0x1101,
  123. // ********************************************************************
  124. // Viewport related stuff. Ignored
  125. CHUNK_DEFAULT_VIEW = 0x3000,
  126. CHUNK_VIEW_TOP = 0x3010,
  127. CHUNK_VIEW_BOTTOM = 0x3020,
  128. CHUNK_VIEW_LEFT = 0x3030,
  129. CHUNK_VIEW_RIGHT = 0x3040,
  130. CHUNK_VIEW_FRONT = 0x3050,
  131. CHUNK_VIEW_BACK = 0x3060,
  132. CHUNK_VIEW_USER = 0x3070,
  133. CHUNK_VIEW_CAMERA = 0x3080,
  134. // ********************************************************************
  135. // Mesh chunks
  136. CHUNK_OBJBLOCK = 0x4000,
  137. CHUNK_TRIMESH = 0x4100,
  138. CHUNK_VERTLIST = 0x4110,
  139. CHUNK_VERTFLAGS = 0x4111,
  140. CHUNK_FACELIST = 0x4120,
  141. CHUNK_FACEMAT = 0x4130,
  142. CHUNK_MAPLIST = 0x4140,
  143. CHUNK_SMOOLIST = 0x4150,
  144. CHUNK_TRMATRIX = 0x4160,
  145. CHUNK_MESHCOLOR = 0x4165,
  146. CHUNK_TXTINFO = 0x4170,
  147. CHUNK_LIGHT = 0x4600,
  148. CHUNK_SPOTLIGHT = 0x4610,
  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. I assume the specular exponent is
  194. // calculated like this:
  195. //
  196. // s[x,y] = stex[x,y] * base_shininess;
  197. // I also assume the data in the texture must be renormalized
  198. // (normally by dividing / 255) after loading.
  199. CHUNK_MAT_MAT_SHINMAP = 0xA33C,
  200. // Scaling in U/V direction.
  201. // (need to gen separate UV coordinate set
  202. // and do this by hand)
  203. CHUNK_MAT_MAP_USCALE = 0xA354,
  204. CHUNK_MAT_MAP_VSCALE = 0xA356,
  205. // Translation in U/V direction.
  206. // (need to gen separate UV coordinate set
  207. // and do this by hand)
  208. CHUNK_MAT_MAP_UOFFSET = 0xA358,
  209. CHUNK_MAT_MAP_VOFFSET = 0xA35a,
  210. // UV-coordinates rotation around the z-axis
  211. // Assumed to be in radians.
  212. CHUNK_MAT_MAP_ANG = 0xA35C,
  213. // Tiling flags for 3DS files
  214. CHUNK_MAT_MAP_TILING = 0xa351,
  215. // Specifies the file name of a texture
  216. CHUNK_MAPFILE = 0xA300,
  217. // Specifies whether a materail requires two-sided rendering
  218. CHUNK_MAT_TWO_SIDE = 0xA081,
  219. // ********************************************************************
  220. // Main keyframer chunk. Contains translation/rotation/scaling data
  221. CHUNK_KEYFRAMER = 0xB000,
  222. // Supported sub chunks
  223. CHUNK_TRACKINFO = 0xB002,
  224. CHUNK_TRACKOBJNAME = 0xB010,
  225. CHUNK_TRACKPIVOT = 0xB013,
  226. CHUNK_TRACKPOS = 0xB020,
  227. CHUNK_TRACKROTATE = 0xB021,
  228. CHUNK_TRACKSCALE = 0xB022,
  229. // ********************************************************************
  230. // Keyframes for various other stuff in the file
  231. // Ignored
  232. CHUNK_AMBIENTKEY = 0xB001,
  233. CHUNK_TRACKMORPH = 0xB026,
  234. CHUNK_TRACKHIDE = 0xB029,
  235. CHUNK_OBJNUMBER = 0xB030,
  236. CHUNK_TRACKCAMERA = 0xB003,
  237. CHUNK_TRACKFOV = 0xB023,
  238. CHUNK_TRACKROLL = 0xB024,
  239. CHUNK_TRACKCAMTGT = 0xB004,
  240. CHUNK_TRACKLIGHT = 0xB005,
  241. CHUNK_TRACKLIGTGT = 0xB006,
  242. CHUNK_TRACKSPOTL = 0xB007,
  243. CHUNK_FRAMES = 0xB008
  244. // ********************************************************************
  245. };
  246. };
  247. #include "./../include/Compiler/poppack1.h"
  248. // ---------------------------------------------------------------------------
  249. /** Helper structure representing a 3ds mesh face */
  250. struct Face : public FaceWithSmoothingGroup
  251. {
  252. //! Specifies that the face normal must be flipped.
  253. //! todo: do we really need this?
  254. bool bFlipped;
  255. };
  256. // ---------------------------------------------------------------------------
  257. /** Helper structure representing a texture */
  258. struct Texture
  259. {
  260. //! Default constructor
  261. Texture()
  262. : mScaleU (1.0f)
  263. , mScaleV (1.0f)
  264. , mOffsetU (0.0f)
  265. , mOffsetV (0.0f)
  266. , mRotation (0.0f)
  267. , mMapMode (aiTextureMapMode_Wrap)
  268. , iUVSrc (0)
  269. {
  270. mTextureBlend = std::numeric_limits<float>::quiet_NaN();
  271. }
  272. //! Specifies the blend factor for the texture
  273. float mTextureBlend;
  274. //! Specifies the filename of the texture
  275. std::string mMapName;
  276. //! Specifies texture coordinate offsets/scaling/rotations
  277. float mScaleU;
  278. float mScaleV;
  279. float mOffsetU;
  280. float mOffsetV;
  281. float mRotation;
  282. //! Specifies the mapping mode to be used for the texture
  283. aiTextureMapMode mMapMode;
  284. //! Used internally
  285. bool bPrivate;
  286. int iUVSrc;
  287. };
  288. // ---------------------------------------------------------------------------
  289. /** Helper structure representing a 3ds material */
  290. struct Material
  291. {
  292. //! Default constructor. Builds a default name for the material
  293. Material()
  294. :
  295. mSpecularExponent (0.0f),
  296. mShininessStrength (1.0f),
  297. mShading(Dot3DSFile::Gouraud),
  298. mTransparency (1.0f),
  299. mBumpHeight (1.0f),
  300. mTwoSided (false),
  301. iBakeUVTransform (0),
  302. pcSingleTexture (NULL)
  303. {
  304. static int iCnt = 0;
  305. char szTemp[128];
  306. sprintf(szTemp,"UNNAMED_%i",iCnt++);
  307. mName = szTemp;
  308. }
  309. //! Name of the material
  310. std::string mName;
  311. //! Diffuse color of the material
  312. aiColor3D mDiffuse;
  313. //! Specular exponent
  314. float mSpecularExponent;
  315. //! Shininess strength, in percent
  316. float mShininessStrength;
  317. //! Specular color of the material
  318. aiColor3D mSpecular;
  319. //! Ambient color of the material
  320. aiColor3D mAmbient;
  321. //! Shading type to be used
  322. Dot3DSFile::shadetype3ds mShading;
  323. //! Opacity of the material
  324. float mTransparency;
  325. //! Diffuse texture channel
  326. Texture sTexDiffuse;
  327. //! Opacity texture channel
  328. Texture sTexOpacity;
  329. //! Specular texture channel
  330. Texture sTexSpecular;
  331. //! Bump texture channel
  332. Texture sTexBump;
  333. //! Emissive texture channel
  334. Texture sTexEmissive;
  335. //! Shininess texture channel
  336. Texture sTexShininess;
  337. //! Scaling factor for the bump values
  338. float mBumpHeight;
  339. //! Emissive color
  340. aiColor3D mEmissive;
  341. //! Ambient texture channel
  342. //! (used by the ASE format)
  343. Texture sTexAmbient;
  344. //! True if the material must be rendered from two sides
  345. bool mTwoSided;
  346. //! Used internally
  347. unsigned int iBakeUVTransform;
  348. Texture* pcSingleTexture;
  349. };
  350. // ---------------------------------------------------------------------------
  351. /** Helper structure to represent a 3ds file mesh */
  352. struct Mesh : public MeshWithSmoothingGroups<Dot3DS::Face>
  353. {
  354. //! Default constructor
  355. Mesh()
  356. {
  357. static int iCnt = 0;
  358. char szTemp[128];
  359. ::sprintf(szTemp,"UNNAMED_%i",iCnt++);
  360. mName = szTemp;
  361. }
  362. //! Name of the mesh
  363. std::string mName;
  364. //! Texture coordinates
  365. std::vector<aiVector2D> mTexCoords;
  366. //! Face materials
  367. std::vector<unsigned int> mFaceMaterials;
  368. //! Local transformation matrix
  369. aiMatrix4x4 mMat;
  370. };
  371. // ---------------------------------------------------------------------------
  372. /** Helper structure to represent a 3ds file node */
  373. struct Node
  374. {
  375. Node()
  376. : mHierarchyPos(0),mHierarchyIndex(0)
  377. {
  378. static int iCnt = 0;
  379. char szTemp[128];
  380. ::sprintf(szTemp,"UNNAMED_%i",iCnt++);
  381. mName = szTemp;
  382. #ifdef AI_3DS_KEYFRAME_ANIMATION
  383. aRotationKeys.reserve(10);
  384. aPositionKeys.reserve(10);
  385. aScalingKeys.reserve(10);
  386. #endif
  387. }
  388. //! Pointer to the parent node
  389. Node* mParent;
  390. //! Holds all child nodes
  391. std::vector<Node*> mChildren;
  392. //! Name of the node
  393. std::string mName;
  394. //! Position of the node in the hierarchy (tree depth)
  395. int16_t mHierarchyPos;
  396. //! Index of the node
  397. int16_t mHierarchyIndex;
  398. #ifdef AI_3DS_KEYFRAME_ANIMATION
  399. //! Rotation keys loaded from the file
  400. std::vector<aiQuatKey> aRotationKeys;
  401. //! Position keys loaded from the file
  402. std::vector<aiVectorKey> aPositionKeys;
  403. //! Scaling keys loaded from the file
  404. std::vector<aiVectorKey> aScalingKeys;
  405. #endif
  406. //! Pivot position loaded from the file
  407. aiVector3D vPivot;
  408. //! Add a child node, setup the right parent node for it
  409. //! \param pc Node to be 'adopted'
  410. inline Node& push_back(Node* pc)
  411. {
  412. mChildren.push_back(pc);
  413. pc->mParent = this;
  414. //pc->mHierarchyPos = this->mHierarchyPos+1;
  415. return *this;
  416. }
  417. };
  418. // ---------------------------------------------------------------------------
  419. /** Helper structure analogue to aiScene */
  420. struct Scene
  421. {
  422. //! List of all materials loaded
  423. //! NOTE: 3ds references materials globally
  424. std::vector<Material> mMaterials;
  425. //! List of all meshes loaded
  426. std::vector<Mesh> mMeshes;
  427. //! Pointer to the root node of the scene
  428. Node* pcRootNode;
  429. };
  430. } // end of namespace Dot3DS
  431. } // end of namespace Assimp
  432. #endif // AI_XFILEHELPER_H_INC