3DSHelper.h 16 KB

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