3DSHelper.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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 D3DS {
  49. #include "./../include/Compiler/pushpack1.h"
  50. // ---------------------------------------------------------------------------
  51. /** Discreet3DS class: Helper class for loading 3ds files. Defines chunks
  52. * and data structures.
  53. */
  54. // ---------------------------------------------------------------------------
  55. class Discreet3DS
  56. {
  57. public:
  58. inline Discreet3DS() {}
  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.
  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. };
  242. };
  243. #include "./../include/Compiler/poppack1.h"
  244. // ---------------------------------------------------------------------------
  245. /** Helper structure representing a 3ds mesh face */
  246. struct Face : public FaceWithSmoothingGroup
  247. {
  248. //! Specifies that the face normal must be flipped.
  249. //! todo: do we really need this?
  250. bool bFlipped;
  251. };
  252. // ---------------------------------------------------------------------------
  253. /** Helper structure representing a texture */
  254. struct Texture
  255. {
  256. //! Default constructor
  257. Texture()
  258. : mScaleU (1.0f)
  259. , mScaleV (1.0f)
  260. , mOffsetU (0.0f)
  261. , mOffsetV (0.0f)
  262. , mRotation (0.0f)
  263. , mMapMode (aiTextureMapMode_Wrap)
  264. , iUVSrc (0)
  265. {
  266. mTextureBlend = std::numeric_limits<float>::quiet_NaN();
  267. }
  268. //! Specifies the blend factor for the texture
  269. float mTextureBlend;
  270. //! Specifies the filename of the texture
  271. std::string mMapName;
  272. //! Specifies texture coordinate offsets/scaling/rotations
  273. float mScaleU;
  274. float mScaleV;
  275. float mOffsetU;
  276. float mOffsetV;
  277. float mRotation;
  278. //! Specifies the mapping mode to be used for the texture
  279. aiTextureMapMode mMapMode;
  280. //! Used internally
  281. bool bPrivate;
  282. int iUVSrc;
  283. };
  284. // ---------------------------------------------------------------------------
  285. /** Helper structure representing a 3ds material */
  286. struct Material
  287. {
  288. //! Default constructor. Builds a default name for the material
  289. Material()
  290. :
  291. mDiffuse (0.6f,0.6f,0.6f), // FIX ... we won't want object to be black
  292. mSpecularExponent (0.0f),
  293. mShininessStrength (1.0f),
  294. mShading(Discreet3DS::Gouraud),
  295. mTransparency (1.0f),
  296. mBumpHeight (1.0f),
  297. mTwoSided (false),
  298. iBakeUVTransform (0),
  299. pcSingleTexture (NULL)
  300. {
  301. static int iCnt = 0;
  302. char szTemp[128];
  303. sprintf(szTemp,"UNNAMED_%i",iCnt++);
  304. mName = szTemp;
  305. }
  306. //! Name of the material
  307. std::string mName;
  308. //! Diffuse color of the material
  309. aiColor3D mDiffuse;
  310. //! Specular exponent
  311. float mSpecularExponent;
  312. //! Shininess strength, in percent
  313. float mShininessStrength;
  314. //! Specular color of the material
  315. aiColor3D mSpecular;
  316. //! Ambient color of the material
  317. aiColor3D mAmbient;
  318. //! Shading type to be used
  319. Discreet3DS::shadetype3ds mShading;
  320. //! Opacity of the material
  321. float mTransparency;
  322. //! Diffuse texture channel
  323. Texture sTexDiffuse;
  324. //! Opacity texture channel
  325. Texture sTexOpacity;
  326. //! Specular texture channel
  327. Texture sTexSpecular;
  328. //! Bump texture channel
  329. Texture sTexBump;
  330. //! Emissive texture channel
  331. Texture sTexEmissive;
  332. //! Shininess texture channel
  333. Texture sTexShininess;
  334. //! Scaling factor for the bump values
  335. float mBumpHeight;
  336. //! Emissive color
  337. aiColor3D mEmissive;
  338. //! Ambient texture channel
  339. //! (used by the ASE format)
  340. Texture sTexAmbient;
  341. //! True if the material must be rendered from two sides
  342. bool mTwoSided;
  343. //! Used internally
  344. unsigned int iBakeUVTransform;
  345. Texture* pcSingleTexture;
  346. };
  347. // ---------------------------------------------------------------------------
  348. /** Helper structure to represent a 3ds file mesh */
  349. struct Mesh : public MeshWithSmoothingGroups<D3DS::Face>
  350. {
  351. //! Default constructor
  352. Mesh()
  353. {
  354. static int iCnt = 0;
  355. char szTemp[128];
  356. ::sprintf(szTemp,"UNNAMED_%i",iCnt++);
  357. mName = szTemp;
  358. }
  359. //! Name of the mesh
  360. std::string mName;
  361. //! Texture coordinates
  362. std::vector<aiVector2D> mTexCoords;
  363. //! Face materials
  364. std::vector<unsigned int> mFaceMaterials;
  365. //! Local transformation matrix
  366. aiMatrix4x4 mMat;
  367. };
  368. typedef std::pair<double, float> aiFloatKey;
  369. // ---------------------------------------------------------------------------
  370. /** Helper structure to represent a 3ds file node */
  371. struct Node
  372. {
  373. Node()
  374. : mHierarchyPos(0),mHierarchyIndex(0)
  375. {
  376. static int iCnt = 0;
  377. char szTemp[128];
  378. ::sprintf(szTemp,"UNNAMED_%i",iCnt++);
  379. mName = szTemp;
  380. aRotationKeys.reserve (20);
  381. aPositionKeys.reserve (20);
  382. aScalingKeys.reserve (20);
  383. }
  384. //! Pointer to the parent node
  385. Node* mParent;
  386. //! Holds all child nodes
  387. std::vector<Node*> mChildren;
  388. //! Name of the node
  389. std::string mName;
  390. //! Dummy nodes: real name to be combined with the $$$DUMMY
  391. std::string mDummyName;
  392. //! Position of the node in the hierarchy (tree depth)
  393. int16_t mHierarchyPos;
  394. //! Index of the node
  395. int16_t mHierarchyIndex;
  396. //! Rotation keys loaded from the file
  397. std::vector<aiQuatKey> aRotationKeys;
  398. //! Position keys loaded from the file
  399. std::vector<aiVectorKey> aPositionKeys;
  400. //! Scaling keys loaded from the file
  401. std::vector<aiVectorKey> aScalingKeys;
  402. // For target lights (spot lights and directional lights):
  403. // The position of the target
  404. std::vector< aiVectorKey > aTargetPositionKeys;
  405. // For cameras: the camera roll angle
  406. std::vector< aiFloatKey > aCameraRollKeys;
  407. //! Pivot position loaded from the file
  408. aiVector3D vPivot;
  409. //! Add a child node, setup the right parent node for it
  410. //! \param pc Node to be 'adopted'
  411. inline Node& push_back(Node* pc)
  412. {
  413. mChildren.push_back(pc);
  414. pc->mParent = this;
  415. //pc->mHierarchyPos = this->mHierarchyPos+1;
  416. return *this;
  417. }
  418. };
  419. // ---------------------------------------------------------------------------
  420. /** Helper structure analogue to aiScene */
  421. struct Scene
  422. {
  423. //! List of all materials loaded
  424. //! NOTE: 3ds references materials globally
  425. std::vector<Material> mMaterials;
  426. //! List of all meshes loaded
  427. std::vector<Mesh> mMeshes;
  428. //! List of all cameras loaded
  429. std::vector<aiCamera*> mCameras;
  430. //! List of all lights loaded
  431. std::vector<aiLight*> mLights;
  432. //! Pointer to the root node of the scene
  433. Node* pcRootNode;
  434. };
  435. } // end of namespace D3DS
  436. } // end of namespace Assimp
  437. #endif // AI_XFILEHELPER_H_INC