IColladaMeshWriter.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #ifndef IRR_I_COLLADA_MESH_WRITER_H_INCLUDED
  5. #define IRR_I_COLLADA_MESH_WRITER_H_INCLUDED
  6. #include "IMeshWriter.h"
  7. #include "ISceneNode.h"
  8. #include "SMaterial.h"
  9. namespace irr
  10. {
  11. namespace io
  12. {
  13. class IWriteFile;
  14. } // end namespace io
  15. namespace scene
  16. {
  17. //! Lighting models - more or less the way Collada categorizes materials
  18. enum E_COLLADA_TECHNIQUE_FX
  19. {
  20. //! Blinn-phong which is default for opengl and dx fixed function pipelines.
  21. //! But several well-known renderers don't support it and prefer phong.
  22. ECTF_BLINN,
  23. //! Phong shading, default in many external renderers.
  24. ECTF_PHONG,
  25. //! diffuse shaded surface that is independent of lighting.
  26. ECTF_LAMBERT,
  27. // constantly shaded surface that is independent of lighting.
  28. ECTF_CONSTANT
  29. };
  30. //! How to interpret the opacity in collada
  31. enum E_COLLADA_TRANSPARENT_FX
  32. {
  33. //! default - only alpha channel of color or texture is used.
  34. ECOF_A_ONE = 0,
  35. //! Alpha values for each RGB channel of color or texture are used.
  36. ECOF_RGB_ZERO = 1
  37. };
  38. //! Color names collada uses in it's color samplers
  39. enum E_COLLADA_COLOR_SAMPLER
  40. {
  41. ECCS_DIFFUSE,
  42. ECCS_AMBIENT,
  43. ECCS_EMISSIVE,
  44. ECCS_SPECULAR,
  45. ECCS_TRANSPARENT,
  46. ECCS_REFLECTIVE
  47. };
  48. //! Irrlicht colors which can be mapped to E_COLLADA_COLOR_SAMPLER values
  49. enum E_COLLADA_IRR_COLOR
  50. {
  51. //! Don't write this element at all
  52. ECIC_NONE,
  53. //! Check IColladaMeshWriterProperties for custom color
  54. ECIC_CUSTOM,
  55. //! Use SMaterial::DiffuseColor
  56. ECIC_DIFFUSE,
  57. //! Use SMaterial::AmbientColor
  58. ECIC_AMBIENT,
  59. //! Use SMaterial::EmissiveColor
  60. ECIC_EMISSIVE,
  61. //! Use SMaterial::SpecularColor
  62. ECIC_SPECULAR
  63. };
  64. //! Control when geometry elements are created
  65. enum E_COLLADA_GEOMETRY_WRITING
  66. {
  67. //! Default - write each mesh exactly once to collada. Optimal but will not work with many tools.
  68. ECGI_PER_MESH,
  69. //! Write each mesh as often as it's used with different materials-names in the scene.
  70. //! Material names which are used here are created on export, so using the IColladaMeshWriterNames
  71. //! interface you have some control over how many geometries are written.
  72. ECGI_PER_MESH_AND_MATERIAL
  73. };
  74. //! Callback interface for properties which can be used to influence collada writing
  75. class IColladaMeshWriterProperties : public virtual IReferenceCounted
  76. {
  77. public:
  78. virtual ~IColladaMeshWriterProperties () {}
  79. //! Which lighting model should be used in the technique (FX) section when exporting effects (materials)
  80. virtual E_COLLADA_TECHNIQUE_FX getTechniqueFx(const video::SMaterial& material) const = 0;
  81. //! Which texture index should be used when writing the texture of the given sampler color.
  82. /** \return the index to the texture-layer or -1 if that texture should never be exported
  83. Note: for ECCS_TRANSPARENT by default the alpha channel is used, if you want to use RGB you have to set
  84. also the ECOF_RGB_ZERO flag in getTransparentFx. */
  85. virtual s32 getTextureIdx(const video::SMaterial & material, E_COLLADA_COLOR_SAMPLER cs) const = 0;
  86. //! Return which color from Irrlicht should be used for the color requested by collada
  87. /** Note that collada allows exporting either texture or color, not both.
  88. So color mapping is only checked if we have no valid texture already.
  89. By default we try to return best fits when possible. For example ECCS_DIFFUSE is mapped to ECIC_DIFFUSE.
  90. When ECIC_CUSTOM is returned then the result of getCustomColor will be used. */
  91. virtual E_COLLADA_IRR_COLOR getColorMapping(const video::SMaterial & material, E_COLLADA_COLOR_SAMPLER cs) const = 0;
  92. //! Return custom colors for certain color types requested by collada.
  93. /** Only used when getColorMapping returns ECIC_CUSTOM for the same parameters. */
  94. virtual video::SColor getCustomColor(const video::SMaterial & material, E_COLLADA_COLOR_SAMPLER cs) const = 0;
  95. //! Return the transparence color interpretation.
  96. /** Not this is only about ECCS_TRANSPARENT and does not affect getTransparency. */
  97. virtual E_COLLADA_TRANSPARENT_FX getTransparentFx(const video::SMaterial& material) const = 0;
  98. //! Transparency value for that material.
  99. /** This value is additional to transparent settings, if both are set they will be multiplicated.
  100. \return 1.0 for fully transparent, 0.0 for not transparent and not written at all when < 0.f */
  101. virtual f32 getTransparency(const video::SMaterial& material) const = 0;
  102. //! Reflectivity value for that material
  103. /** The amount of perfect mirror reflection to be added to the reflected light
  104. \return 0.0 - 1.0 for reflectivity and element is not written at all when < 0.f */
  105. virtual f32 getReflectivity(const video::SMaterial& material) const = 0;
  106. //! Return index of refraction for that material
  107. /** By default we don't write that.
  108. \return a value greater equal 0.f to write \<index_of_refraction\> when it is lesser than 0 nothing will be written */
  109. virtual f32 getIndexOfRefraction(const video::SMaterial& material) const = 0;
  110. //! Should node be used in scene export? (only needed for scene-writing, ignored in mesh-writing)
  111. //! By default all visible nodes are exported.
  112. virtual bool isExportable(const irr::scene::ISceneNode * node) const = 0;
  113. //! Return the mesh for the given node. If it has no mesh or shouldn't export it's mesh
  114. //! you can return 0 in which case only the transformation matrix of the node will be used.
  115. // TODO: Function is not const because there is no const getMesh() function for several Irrlicht nodes.
  116. virtual IMesh* getMesh(irr::scene::ISceneNode * node) = 0;
  117. //! Return if the node has it's own material overwriting the mesh-materials
  118. /** Usually true except for mesh-nodes which have isReadOnlyMaterials set.
  119. This is mostly important for naming (as ISceneNode::getMaterial() already returns the correct material).
  120. You have to override it when exporting custom scenenodes with own materials.
  121. \return true => The node's own material is used, false => ignore node material and use the one from the mesh */
  122. virtual bool useNodeMaterial(const scene::ISceneNode* node) const = 0;
  123. };
  124. //! Callback interface to use custom names on collada writing.
  125. /** You can either modify names and id's written to collada or you can use
  126. this interface to just find out which names are used on writing.
  127. Names are often used later as xs:anyURI, so avoid whitespace, '#' and '%' in the names.
  128. */
  129. class IColladaMeshWriterNames : public virtual IReferenceCounted
  130. {
  131. public:
  132. virtual ~IColladaMeshWriterNames () {}
  133. //! Return a unique name for the given mesh
  134. /** Note that names really must be unique here per mesh-pointer, so
  135. mostly it's a good idea to return the nameForMesh from
  136. IColladaMeshWriter::getDefaultNameGenerator(). Also names must follow
  137. the xs:NCName standard to be valid, you can run them through
  138. IColladaMeshWriter::toNCName to ensure that.
  139. \param mesh Pointer to the mesh which needs a name
  140. \param instance When E_COLLADA_GEOMETRY_WRITING is not ECGI_PER_MESH then
  141. several instances of the same mesh can be written and this counts them.
  142. */
  143. virtual irr::core::stringc nameForMesh(const scene::IMesh* mesh, int instance) = 0;
  144. //! Return a unique name for the given node
  145. /** Note that names really must be unique here per node-pointer, so
  146. mostly it's a good idea to return the nameForNode from
  147. IColladaMeshWriter::getDefaultNameGenerator(). Also names must follow
  148. the xs:NCName standard to be valid, you can run them through
  149. IColladaMeshWriter::toNCName to ensure that.
  150. */
  151. virtual irr::core::stringc nameForNode(const scene::ISceneNode* node) = 0;
  152. //! Return a name for the material
  153. /** There is one material created in the writer for each unique name.
  154. So you can use this to control the number of materials which get written.
  155. For example Irrlicht does by default write one material for each material
  156. instanced by a node. So if you know that in your application material
  157. instances per node are identical between different nodes you can reduce
  158. the number of exported materials using that knowledge by using identical
  159. names for such shared materials.
  160. Names must follow the xs:NCName standard to be valid, you can run them
  161. through IColladaMeshWriter::toNCName to ensure that.
  162. */
  163. virtual irr::core::stringc nameForMaterial(const video::SMaterial & material, int materialId, const scene::IMesh* mesh, const scene::ISceneNode* node) = 0;
  164. };
  165. //! Interface for writing meshes
  166. class IColladaMeshWriter : public IMeshWriter
  167. {
  168. public:
  169. IColladaMeshWriter()
  170. : Properties(0), DefaultProperties(0), NameGenerator(0), DefaultNameGenerator(0)
  171. , WriteTextures(true), WriteDefaultScene(true), ExportSMaterialOnce(true)
  172. , AmbientLight(0.f, 0.f, 0.f, 1.f)
  173. , UnitMeter(1.f), UnitName("meter")
  174. , GeometryWriting(ECGI_PER_MESH)
  175. {
  176. ParamNamesUV[0] = "U";
  177. ParamNamesUV[1] = "V";
  178. }
  179. //! Destructor
  180. virtual ~IColladaMeshWriter()
  181. {
  182. if ( Properties )
  183. Properties->drop();
  184. if ( DefaultProperties )
  185. DefaultProperties->drop();
  186. if ( NameGenerator )
  187. NameGenerator->drop();
  188. if ( DefaultNameGenerator )
  189. DefaultNameGenerator->drop();
  190. }
  191. //! writes a scene starting with the given node
  192. //\param writeRoot: 0 = no, 1=yes unless root is scenemanager, 2=yes
  193. virtual bool writeScene(io::IWriteFile* file, scene::ISceneNode* root, int writeRoot=1) = 0;
  194. //! Set if texture information should be written
  195. virtual void setWriteTextures(bool write)
  196. {
  197. WriteTextures = write;
  198. }
  199. //! Get if texture information should be written
  200. virtual bool getWriteTextures() const
  201. {
  202. return WriteTextures;
  203. }
  204. //! Set if a default scene should be written when writing meshes.
  205. /** Many collada readers fail to read a mesh if the collada files doesn't contain a scene as well.
  206. The scene is doing an instantiation of the mesh.
  207. When using writeScene this flag is ignored (as we have scene there already)
  208. */
  209. virtual void setWriteDefaultScene(bool write)
  210. {
  211. WriteDefaultScene = write;
  212. }
  213. //! Get if a default scene should be written
  214. virtual bool getWriteDefaultScene() const
  215. {
  216. return WriteDefaultScene;
  217. }
  218. //! Sets ambient color of the scene to write
  219. virtual void setAmbientLight(const video::SColorf &ambientColor)
  220. {
  221. AmbientLight = ambientColor;
  222. }
  223. //! Return ambient light of the scene which is written
  224. virtual video::SColorf getAmbientLight() const
  225. {
  226. return AmbientLight;
  227. }
  228. //! Set the unit distances for all elements and objects
  229. /**
  230. \param meter: Real-world meters to use per unit. Default 1 unit = 1 meter. For 1 unit = 1cm you would set to 0.01
  231. \param name: Name to use for distance unit. Default is "meter". */
  232. virtual void setUnit(irr::f32 meter, const irr::core::stringc& name)
  233. {
  234. UnitMeter = meter;
  235. UnitName = name;
  236. }
  237. //! Return real world meters to use per unit for all elements and objects
  238. virtual irr::f32 getUnitMeter() const
  239. {
  240. return UnitMeter;
  241. }
  242. //! Return name to use for distance units. Like p.E. "meter".
  243. virtual irr::core::stringc getUnitName() const
  244. {
  245. return UnitName;
  246. }
  247. //! Control when and how often a mesh is written
  248. /** Optimally ECGI_PER_MESH would be always sufficient - writing geometry once per mesh.
  249. Unfortunately many tools (at the time of writing this nearly all of them) have trouble
  250. on import when different materials are used per node. So when you override materials
  251. per node and importing the resulting collada has materials problems in other tools try
  252. using other values here.
  253. \param writeStyle One of the E_COLLADA_GEOMETRY_WRITING settings.
  254. */
  255. virtual void setGeometryWriting(E_COLLADA_GEOMETRY_WRITING writeStyle)
  256. {
  257. GeometryWriting = writeStyle;
  258. }
  259. //! Get the current style of geometry writing.
  260. virtual E_COLLADA_GEOMETRY_WRITING getGeometryWriting() const
  261. {
  262. return GeometryWriting;
  263. }
  264. //! Make certain there is only one collada material generated per Irrlicht material
  265. /** Checks before creating a collada material-name if an identical
  266. irr:::video::SMaterial has been exported already. If so don't export it with
  267. another name. This is set by default and leads to way smaller .dae files.
  268. Note that if you need to disable this flag for some reason you can still
  269. get a similar effect using the IColladaMeshWriterNames::nameForMaterial
  270. by returning identical names for identical materials there.
  271. */
  272. virtual void setExportSMaterialsOnlyOnce(bool exportOnce)
  273. {
  274. ExportSMaterialOnce = exportOnce;
  275. }
  276. virtual bool getExportSMaterialsOnlyOnce() const
  277. {
  278. return ExportSMaterialOnce;
  279. }
  280. //! Set properties to use by the meshwriter instead of it's default properties.
  281. /** Overloading properties with an own class allows modifying the writing process in certain ways.
  282. By default properties are set to the DefaultProperties. */
  283. virtual void setProperties(IColladaMeshWriterProperties * p)
  284. {
  285. if ( p == Properties )
  286. return;
  287. if ( p )
  288. p->grab();
  289. if ( Properties )
  290. Properties->drop();
  291. Properties = p;
  292. }
  293. //! Get properties which are currently used.
  294. virtual IColladaMeshWriterProperties * getProperties() const
  295. {
  296. return Properties;
  297. }
  298. //! Return the original default properties of the writer.
  299. /** You can use this pointer in your own properties to access and return default values. */
  300. IColladaMeshWriterProperties * getDefaultProperties() const
  301. {
  302. return DefaultProperties;
  303. }
  304. //! Install a generator to create custom names on export.
  305. virtual void setNameGenerator(IColladaMeshWriterNames * nameGenerator)
  306. {
  307. if ( nameGenerator == NameGenerator )
  308. return;
  309. if ( nameGenerator )
  310. nameGenerator->grab();
  311. if ( NameGenerator )
  312. NameGenerator->drop();
  313. NameGenerator = nameGenerator;
  314. }
  315. //! Get currently used name generator
  316. virtual IColladaMeshWriterNames * getNameGenerator() const
  317. {
  318. return NameGenerator;
  319. }
  320. //! Return the original default name generator of the writer.
  321. /** You can use this pointer in your own generator to access and return default values. */
  322. IColladaMeshWriterNames * getDefaultNameGenerator() const
  323. {
  324. return DefaultNameGenerator;
  325. }
  326. //! Restrict the characters of oldString a set of allowed characters in xs:NCName and add the prefix.
  327. /** A tool function to help when using a custom name generator to generative valid names for collada names and id's. */
  328. virtual irr::core::stringc toNCName(const irr::core::stringc& oldString, const irr::core::stringc& prefix=irr::core::stringc("_NC_")) const = 0;
  329. //! After export you can find out which name had been used for writing the geometry for this node.
  330. /** The name comes from IColladaMeshWriterNames::nameForMesh, but you can't access the node there.
  331. \return Either a pointer to the name or NULL */
  332. // TODO: Function is not const because there is no const getMesh() function for several Irrlicht nodes.
  333. virtual const irr::core::stringc* findGeometryNameForNode(ISceneNode* node) = 0;
  334. //! Change param name used for UV's.
  335. /** Param names for UV's have a name. By default it's "U" and "V".
  336. Usually it doesn't matter as names are optional in Collada anyway.
  337. But unfortunately some tools insist on specific names.
  338. So if "U", "V" does not work then try to export by setting this to "S", "T".
  339. One tool which insists on "S", "T" is for example SketchUp.
  340. */
  341. void SetParamNamesUV(const core::stringc& u, const core::stringc& v)
  342. {
  343. ParamNamesUV[0] = u;
  344. ParamNamesUV[1] = v;
  345. }
  346. protected:
  347. // NOTE: You usually should also call setProperties with the same parameter when using setDefaultProperties
  348. virtual void setDefaultProperties(IColladaMeshWriterProperties * p)
  349. {
  350. if ( p == DefaultProperties )
  351. return;
  352. if ( p )
  353. p->grab();
  354. if ( DefaultProperties )
  355. DefaultProperties->drop();
  356. DefaultProperties = p;
  357. }
  358. // NOTE: You usually should also call setNameGenerator with the same parameter when using setDefaultProperties
  359. virtual void setDefaultNameGenerator(IColladaMeshWriterNames * p)
  360. {
  361. if ( p == DefaultNameGenerator )
  362. return;
  363. if ( p )
  364. p->grab();
  365. if ( DefaultNameGenerator )
  366. DefaultNameGenerator->drop();
  367. DefaultNameGenerator = p;
  368. }
  369. protected:
  370. irr::core::stringc ParamNamesUV[2];
  371. private:
  372. IColladaMeshWriterProperties * Properties;
  373. IColladaMeshWriterProperties * DefaultProperties;
  374. IColladaMeshWriterNames * NameGenerator;
  375. IColladaMeshWriterNames * DefaultNameGenerator;
  376. bool WriteTextures;
  377. bool WriteDefaultScene;
  378. bool ExportSMaterialOnce;
  379. video::SColorf AmbientLight;
  380. irr::f32 UnitMeter;
  381. irr::core::stringc UnitName;
  382. E_COLLADA_GEOMETRY_WRITING GeometryWriting;
  383. };
  384. } // end namespace
  385. } // end namespace
  386. #endif