You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

585 lines
16 KiB

  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, assimp 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 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/assimp/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. // Prj master chunk
  99. CHUNK_PRJ = 0xC23D,
  100. // MDLI master chunk
  101. CHUNK_MLI = 0x3DAA,
  102. // Primary main chunk of the .3ds file
  103. CHUNK_MAIN = 0x4D4D,
  104. // Mesh main chunk
  105. CHUNK_OBJMESH = 0x3D3D,
  106. // Specifies the background color of the .3ds file
  107. // This is passed through the material system for
  108. // viewing purposes.
  109. CHUNK_BKGCOLOR = 0x1200,
  110. // Specifies the ambient base color of the scene.
  111. // This is added to all materials in the file
  112. CHUNK_AMBCOLOR = 0x2100,
  113. // Specifies the background image for the whole scene
  114. // This value is passed through the material system
  115. // to the viewer
  116. CHUNK_BIT_MAP = 0x1100,
  117. CHUNK_BIT_MAP_EXISTS = 0x1101,
  118. // ********************************************************************
  119. // Viewport related stuff. Ignored
  120. CHUNK_DEFAULT_VIEW = 0x3000,
  121. CHUNK_VIEW_TOP = 0x3010,
  122. CHUNK_VIEW_BOTTOM = 0x3020,
  123. CHUNK_VIEW_LEFT = 0x3030,
  124. CHUNK_VIEW_RIGHT = 0x3040,
  125. CHUNK_VIEW_FRONT = 0x3050,
  126. CHUNK_VIEW_BACK = 0x3060,
  127. CHUNK_VIEW_USER = 0x3070,
  128. CHUNK_VIEW_CAMERA = 0x3080,
  129. // ********************************************************************
  130. // Mesh chunks
  131. CHUNK_OBJBLOCK = 0x4000,
  132. CHUNK_TRIMESH = 0x4100,
  133. CHUNK_VERTLIST = 0x4110,
  134. CHUNK_VERTFLAGS = 0x4111,
  135. CHUNK_FACELIST = 0x4120,
  136. CHUNK_FACEMAT = 0x4130,
  137. CHUNK_MAPLIST = 0x4140,
  138. CHUNK_SMOOLIST = 0x4150,
  139. CHUNK_TRMATRIX = 0x4160,
  140. CHUNK_MESHCOLOR = 0x4165,
  141. CHUNK_TXTINFO = 0x4170,
  142. CHUNK_LIGHT = 0x4600,
  143. CHUNK_CAMERA = 0x4700,
  144. CHUNK_HIERARCHY = 0x4F00,
  145. // Specifies the global scaling factor. This is applied
  146. // to the root node's transformation matrix
  147. CHUNK_MASTER_SCALE = 0x0100,
  148. // ********************************************************************
  149. // Material chunks
  150. CHUNK_MAT_MATERIAL = 0xAFFF,
  151. // asciiz containing the name of the material
  152. CHUNK_MAT_MATNAME = 0xA000,
  153. CHUNK_MAT_AMBIENT = 0xA010, // followed by color chunk
  154. CHUNK_MAT_DIFFUSE = 0xA020, // followed by color chunk
  155. CHUNK_MAT_SPECULAR = 0xA030, // followed by color chunk
  156. // Specifies the shininess of the material
  157. // followed by percentage chunk
  158. CHUNK_MAT_SHININESS = 0xA040,
  159. CHUNK_MAT_SHININESS_PERCENT = 0xA041 ,
  160. // Specifies the shading mode to be used
  161. // followed by a short
  162. CHUNK_MAT_SHADING = 0xA100,
  163. // NOTE: Emissive color (self illumination) seems not
  164. // to be a color but a single value, type is unknown.
  165. // Make the parser accept both of them.
  166. // followed by percentage chunk (?)
  167. CHUNK_MAT_SELF_ILLUM = 0xA080,
  168. // Always followed by percentage chunk (?)
  169. CHUNK_MAT_SELF_ILPCT = 0xA084,
  170. // Always followed by percentage chunk
  171. CHUNK_MAT_TRANSPARENCY = 0xA050,
  172. // Diffuse texture channel 0
  173. CHUNK_MAT_TEXTURE = 0xA200,
  174. // Contains opacity information for each texel
  175. CHUNK_MAT_OPACMAP = 0xA210,
  176. // Contains a reflection map to be used to reflect
  177. // the environment. This is partially supported.
  178. CHUNK_MAT_REFLMAP = 0xA220,
  179. // Self Illumination map (emissive colors)
  180. CHUNK_MAT_SELFIMAP = 0xA33d,
  181. // Bumpmap. Not specified whether it is a heightmap
  182. // or a normal map. Assme it is a heightmap since
  183. // artist normally prefer this format.
  184. CHUNK_MAT_BUMPMAP = 0xA230,
  185. // Specular map. Seems to influence the specular color
  186. CHUNK_MAT_SPECMAP = 0xA204,
  187. // Holds shininess data.
  188. CHUNK_MAT_MAT_SHINMAP = 0xA33C,
  189. // Scaling in U/V direction.
  190. // (need to gen separate UV coordinate set
  191. // and do this by hand)
  192. CHUNK_MAT_MAP_USCALE = 0xA354,
  193. CHUNK_MAT_MAP_VSCALE = 0xA356,
  194. // Translation in U/V direction.
  195. // (need to gen separate UV coordinate set
  196. // and do this by hand)
  197. CHUNK_MAT_MAP_UOFFSET = 0xA358,
  198. CHUNK_MAT_MAP_VOFFSET = 0xA35a,
  199. // UV-coordinates rotation around the z-axis
  200. // Assumed to be in radians.
  201. CHUNK_MAT_MAP_ANG = 0xA35C,
  202. // Tiling flags for 3DS files
  203. CHUNK_MAT_MAP_TILING = 0xa351,
  204. // Specifies the file name of a texture
  205. CHUNK_MAPFILE = 0xA300,
  206. // Specifies whether a materail requires two-sided rendering
  207. CHUNK_MAT_TWO_SIDE = 0xA081,
  208. // ********************************************************************
  209. // Main keyframer chunk. Contains translation/rotation/scaling data
  210. CHUNK_KEYFRAMER = 0xB000,
  211. // Supported sub chunks
  212. CHUNK_TRACKINFO = 0xB002,
  213. CHUNK_TRACKOBJNAME = 0xB010,
  214. CHUNK_TRACKDUMMYOBJNAME = 0xB011,
  215. CHUNK_TRACKPIVOT = 0xB013,
  216. CHUNK_TRACKPOS = 0xB020,
  217. CHUNK_TRACKROTATE = 0xB021,
  218. CHUNK_TRACKSCALE = 0xB022,
  219. // ********************************************************************
  220. // Keyframes for various other stuff in the file
  221. // Partially ignored
  222. CHUNK_AMBIENTKEY = 0xB001,
  223. CHUNK_TRACKMORPH = 0xB026,
  224. CHUNK_TRACKHIDE = 0xB029,
  225. CHUNK_OBJNUMBER = 0xB030,
  226. CHUNK_TRACKCAMERA = 0xB003,
  227. CHUNK_TRACKFOV = 0xB023,
  228. CHUNK_TRACKROLL = 0xB024,
  229. CHUNK_TRACKCAMTGT = 0xB004,
  230. CHUNK_TRACKLIGHT = 0xB005,
  231. CHUNK_TRACKLIGTGT = 0xB006,
  232. CHUNK_TRACKSPOTL = 0xB007,
  233. CHUNK_FRAMES = 0xB008,
  234. // ********************************************************************
  235. // light sub-chunks
  236. CHUNK_DL_OFF = 0x4620,
  237. CHUNK_DL_OUTER_RANGE = 0x465A,
  238. CHUNK_DL_INNER_RANGE = 0x4659,
  239. CHUNK_DL_MULTIPLIER = 0x465B,
  240. CHUNK_DL_EXCLUDE = 0x4654,
  241. CHUNK_DL_ATTENUATE = 0x4625,
  242. CHUNK_DL_SPOTLIGHT = 0x4610,
  243. // camera sub-chunks
  244. CHUNK_CAM_RANGES = 0x4720
  245. };
  246. };
  247. // ---------------------------------------------------------------------------
  248. /** Helper structure representing a 3ds mesh face */
  249. struct Face : public FaceWithSmoothingGroup
  250. {
  251. };
  252. // ---------------------------------------------------------------------------
  253. /** Helper structure representing a texture */
  254. struct Texture
  255. {
  256. //! Default constructor
  257. Texture()
  258. : mOffsetU (0.0f)
  259. , mOffsetV (0.0f)
  260. , mScaleU (1.0f)
  261. , mScaleV (1.0f)
  262. , mRotation (0.0f)
  263. , mMapMode (aiTextureMapMode_Wrap)
  264. , iUVSrc (0)
  265. {
  266. mTextureBlend = get_qnan();
  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 mOffsetU;
  274. float mOffsetV;
  275. float mScaleU;
  276. float mScaleV;
  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. #include "./../include/assimp/Compiler/poppack1.h"
  285. // ---------------------------------------------------------------------------
  286. /** Helper structure representing a 3ds material */
  287. struct Material
  288. {
  289. //! Default constructor. Builds a default name for the material
  290. Material()
  291. :
  292. mDiffuse (0.6f,0.6f,0.6f), // FIX ... we won't want object to be black
  293. mSpecularExponent (0.0f),
  294. mShininessStrength (1.0f),
  295. mShading(Discreet3DS::Gouraud),
  296. mTransparency (1.0f),
  297. mBumpHeight (1.0f),
  298. mTwoSided (false)
  299. {
  300. static int iCnt = 0;
  301. char szTemp[128];
  302. sprintf(szTemp,"UNNAMED_%i",iCnt++);
  303. mName = szTemp;
  304. }
  305. //! Name of the material
  306. std::string mName;
  307. //! Diffuse color of the material
  308. aiColor3D mDiffuse;
  309. //! Specular exponent
  310. float mSpecularExponent;
  311. //! Shininess strength, in percent
  312. float mShininessStrength;
  313. //! Specular color of the material
  314. aiColor3D mSpecular;
  315. //! Ambient color of the material
  316. aiColor3D mAmbient;
  317. //! Shading type to be used
  318. Discreet3DS::shadetype3ds mShading;
  319. //! Opacity of the material
  320. float mTransparency;
  321. //! Diffuse texture channel
  322. Texture sTexDiffuse;
  323. //! Opacity texture channel
  324. Texture sTexOpacity;
  325. //! Specular texture channel
  326. Texture sTexSpecular;
  327. //! Reflective texture channel
  328. Texture sTexReflective;
  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. , mInstanceCount (1)
  396. {
  397. static int iCnt = 0;
  398. // Generate a default name for the node
  399. char szTemp[128];
  400. ::sprintf(szTemp,"UNNAMED_%i",iCnt++);
  401. mName = szTemp;
  402. aRotationKeys.reserve (20);
  403. aPositionKeys.reserve (20);
  404. aScalingKeys.reserve (20);
  405. }
  406. ~Node()
  407. {
  408. for (unsigned int i = 0; i < mChildren.size();++i)
  409. delete mChildren[i];
  410. }
  411. //! Pointer to the parent node
  412. Node* mParent;
  413. //! Holds all child nodes
  414. std::vector<Node*> mChildren;
  415. //! Name of the node
  416. std::string mName;
  417. //! InstanceNumber of the node
  418. int32_t mInstanceNumber;
  419. //! Dummy nodes: real name to be combined with the $$$DUMMY
  420. std::string mDummyName;
  421. //! Position of the node in the hierarchy (tree depth)
  422. int16_t mHierarchyPos;
  423. //! Index of the node
  424. int16_t mHierarchyIndex;
  425. //! Rotation keys loaded from the file
  426. std::vector<aiQuatKey> aRotationKeys;
  427. //! Position keys loaded from the file
  428. std::vector<aiVectorKey> aPositionKeys;
  429. //! Scaling keys loaded from the file
  430. std::vector<aiVectorKey> aScalingKeys;
  431. // For target lights (spot lights and directional lights):
  432. // The position of the target
  433. std::vector< aiVectorKey > aTargetPositionKeys;
  434. // For cameras: the camera roll angle
  435. std::vector< aiFloatKey > aCameraRollKeys;
  436. //! Pivot position loaded from the file
  437. aiVector3D vPivot;
  438. //instance count, will be kept only for the first node
  439. int32_t mInstanceCount;
  440. //! Add a child node, setup the right parent node for it
  441. //! \param pc Node to be 'adopted'
  442. inline Node& push_back(Node* pc)
  443. {
  444. mChildren.push_back(pc);
  445. pc->mParent = this;
  446. return *this;
  447. }
  448. };
  449. // ---------------------------------------------------------------------------
  450. /** Helper structure analogue to aiScene */
  451. struct Scene
  452. {
  453. //! List of all materials loaded
  454. //! NOTE: 3ds references materials globally
  455. std::vector<Material> mMaterials;
  456. //! List of all meshes loaded
  457. std::vector<Mesh> mMeshes;
  458. //! List of all cameras loaded
  459. std::vector<aiCamera*> mCameras;
  460. //! List of all lights loaded
  461. std::vector<aiLight*> mLights;
  462. //! Pointer to the root node of the scene
  463. // --- moved to main class
  464. // Node* pcRootNode;
  465. };
  466. } // end of namespace D3DS
  467. } // end of namespace Assimp
  468. #endif // AI_XFILEHELPER_H_INC