3DSHelper.h 15 KB

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