CMeshSceneNode.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. #include "CMeshSceneNode.h"
  5. #include "IVideoDriver.h"
  6. #include "ISceneManager.h"
  7. #include "S3DVertex.h"
  8. #include "ICameraSceneNode.h"
  9. #include "IMeshCache.h"
  10. #include "IAnimatedMesh.h"
  11. #include "IMaterialRenderer.h"
  12. #include "IFileSystem.h"
  13. namespace irr
  14. {
  15. namespace scene
  16. {
  17. //! constructor
  18. CMeshSceneNode::CMeshSceneNode(IMesh* mesh, ISceneNode* parent, ISceneManager* mgr, s32 id,
  19. const core::vector3df& position, const core::vector3df& rotation,
  20. const core::vector3df& scale)
  21. : IMeshSceneNode(parent, mgr, id, position, rotation, scale), Mesh(0),
  22. PassCount(0), ReadOnlyMaterials(false)
  23. {
  24. #ifdef _DEBUG
  25. setDebugName("CMeshSceneNode");
  26. #endif
  27. setMesh(mesh);
  28. }
  29. //! destructor
  30. CMeshSceneNode::~CMeshSceneNode()
  31. {
  32. if (Mesh)
  33. Mesh->drop();
  34. }
  35. //! frame
  36. void CMeshSceneNode::OnRegisterSceneNode()
  37. {
  38. if (IsVisible)
  39. {
  40. // because this node supports rendering of mixed mode meshes consisting of
  41. // transparent and solid material at the same time, we need to go through all
  42. // materials, check of what type they are and register this node for the right
  43. // render pass according to that.
  44. video::IVideoDriver* driver = SceneManager->getVideoDriver();
  45. PassCount = 0;
  46. int transparentCount = 0;
  47. int solidCount = 0;
  48. // count transparent and solid materials in this scene node
  49. if (ReadOnlyMaterials && Mesh)
  50. {
  51. // count mesh materials
  52. for (u32 i=0; i<Mesh->getMeshBufferCount(); ++i)
  53. {
  54. scene::IMeshBuffer* mb = Mesh->getMeshBuffer(i);
  55. video::IMaterialRenderer* rnd = mb ? driver->getMaterialRenderer(mb->getMaterial().MaterialType) : 0;
  56. if (rnd && rnd->isTransparent())
  57. ++transparentCount;
  58. else
  59. ++solidCount;
  60. if (solidCount && transparentCount)
  61. break;
  62. }
  63. }
  64. else
  65. {
  66. // count copied materials
  67. for (u32 i=0; i<Materials.size(); ++i)
  68. {
  69. video::IMaterialRenderer* rnd =
  70. driver->getMaterialRenderer(Materials[i].MaterialType);
  71. if (rnd && rnd->isTransparent())
  72. ++transparentCount;
  73. else
  74. ++solidCount;
  75. if (solidCount && transparentCount)
  76. break;
  77. }
  78. }
  79. // register according to material types counted
  80. if (solidCount)
  81. SceneManager->registerNodeForRendering(this, scene::ESNRP_SOLID);
  82. if (transparentCount)
  83. SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT);
  84. ISceneNode::OnRegisterSceneNode();
  85. }
  86. }
  87. //! renders the node.
  88. void CMeshSceneNode::render()
  89. {
  90. video::IVideoDriver* driver = SceneManager->getVideoDriver();
  91. if (!Mesh || !driver)
  92. return;
  93. bool isTransparentPass =
  94. SceneManager->getSceneNodeRenderPass() == scene::ESNRP_TRANSPARENT;
  95. ++PassCount;
  96. driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
  97. Box = Mesh->getBoundingBox();
  98. // for debug purposes only:
  99. bool renderMeshes = true;
  100. video::SMaterial mat;
  101. if (DebugDataVisible && PassCount==1)
  102. {
  103. // overwrite half transparency
  104. if (DebugDataVisible & scene::EDS_HALF_TRANSPARENCY)
  105. {
  106. for (u32 g=0; g<Mesh->getMeshBufferCount(); ++g)
  107. {
  108. mat = Materials[g];
  109. mat.MaterialType = video::EMT_TRANSPARENT_ADD_COLOR;
  110. driver->setMaterial(mat);
  111. driver->drawMeshBuffer(Mesh->getMeshBuffer(g));
  112. }
  113. renderMeshes = false;
  114. }
  115. }
  116. // render original meshes
  117. if (renderMeshes)
  118. {
  119. for (u32 i=0; i<Mesh->getMeshBufferCount(); ++i)
  120. {
  121. scene::IMeshBuffer* mb = Mesh->getMeshBuffer(i);
  122. if (mb)
  123. {
  124. const video::SMaterial& material = ReadOnlyMaterials ? mb->getMaterial() : Materials[i];
  125. video::IMaterialRenderer* rnd = driver->getMaterialRenderer(material.MaterialType);
  126. bool transparent = (rnd && rnd->isTransparent());
  127. // only render transparent buffer if this is the transparent render pass
  128. // and solid only in solid pass
  129. if (transparent == isTransparentPass)
  130. {
  131. driver->setMaterial(material);
  132. driver->drawMeshBuffer(mb);
  133. }
  134. }
  135. }
  136. }
  137. driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
  138. // for debug purposes only:
  139. if (DebugDataVisible && PassCount==1)
  140. {
  141. video::SMaterial m;
  142. m.Lighting = false;
  143. m.AntiAliasing=0;
  144. driver->setMaterial(m);
  145. if (DebugDataVisible & scene::EDS_BBOX)
  146. {
  147. driver->draw3DBox(Box, video::SColor(255,255,255,255));
  148. }
  149. if (DebugDataVisible & scene::EDS_BBOX_BUFFERS)
  150. {
  151. for (u32 g=0; g<Mesh->getMeshBufferCount(); ++g)
  152. {
  153. driver->draw3DBox(
  154. Mesh->getMeshBuffer(g)->getBoundingBox(),
  155. video::SColor(255,190,128,128));
  156. }
  157. }
  158. if (DebugDataVisible & scene::EDS_NORMALS)
  159. {
  160. // draw normals
  161. const f32 debugNormalLength = SceneManager->getParameters()->getAttributeAsFloat(DEBUG_NORMAL_LENGTH);
  162. const video::SColor debugNormalColor = SceneManager->getParameters()->getAttributeAsColor(DEBUG_NORMAL_COLOR);
  163. const u32 count = Mesh->getMeshBufferCount();
  164. for (u32 i=0; i != count; ++i)
  165. {
  166. driver->drawMeshBufferNormals(Mesh->getMeshBuffer(i), debugNormalLength, debugNormalColor);
  167. }
  168. }
  169. // show mesh
  170. if (DebugDataVisible & scene::EDS_MESH_WIRE_OVERLAY)
  171. {
  172. m.Wireframe = true;
  173. driver->setMaterial(m);
  174. for (u32 g=0; g<Mesh->getMeshBufferCount(); ++g)
  175. {
  176. driver->drawMeshBuffer(Mesh->getMeshBuffer(g));
  177. }
  178. }
  179. }
  180. }
  181. //! Removes a child from this scene node.
  182. //! Implemented here, to be able to remove the shadow properly, if there is one,
  183. //! or to remove attached childs.
  184. bool CMeshSceneNode::removeChild(ISceneNode* child)
  185. {
  186. return ISceneNode::removeChild(child);
  187. }
  188. //! returns the axis aligned bounding box of this node
  189. const core::aabbox3d<f32>& CMeshSceneNode::getBoundingBox() const
  190. {
  191. return Mesh ? Mesh->getBoundingBox() : Box;
  192. }
  193. //! returns the material based on the zero based index i. To get the amount
  194. //! of materials used by this scene node, use getMaterialCount().
  195. //! This function is needed for inserting the node into the scene hierarchy on a
  196. //! optimal position for minimizing renderstate changes, but can also be used
  197. //! to directly modify the material of a scene node.
  198. video::SMaterial& CMeshSceneNode::getMaterial(u32 i)
  199. {
  200. if (Mesh && ReadOnlyMaterials && i<Mesh->getMeshBufferCount())
  201. {
  202. ReadOnlyMaterial = Mesh->getMeshBuffer(i)->getMaterial();
  203. return ReadOnlyMaterial;
  204. }
  205. if (i >= Materials.size())
  206. return ISceneNode::getMaterial(i);
  207. return Materials[i];
  208. }
  209. //! returns amount of materials used by this scene node.
  210. u32 CMeshSceneNode::getMaterialCount() const
  211. {
  212. if (Mesh && ReadOnlyMaterials)
  213. return Mesh->getMeshBufferCount();
  214. return Materials.size();
  215. }
  216. //! Sets a new mesh
  217. void CMeshSceneNode::setMesh(IMesh* mesh)
  218. {
  219. if (mesh)
  220. {
  221. mesh->grab();
  222. if (Mesh)
  223. Mesh->drop();
  224. Mesh = mesh;
  225. copyMaterials();
  226. }
  227. }
  228. void CMeshSceneNode::copyMaterials()
  229. {
  230. Materials.clear();
  231. if (Mesh)
  232. {
  233. video::SMaterial mat;
  234. for (u32 i=0; i<Mesh->getMeshBufferCount(); ++i)
  235. {
  236. IMeshBuffer* mb = Mesh->getMeshBuffer(i);
  237. if (mb)
  238. mat = mb->getMaterial();
  239. Materials.push_back(mat);
  240. }
  241. }
  242. }
  243. //! Writes attributes of the scene node.
  244. void CMeshSceneNode::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const
  245. {
  246. IMeshSceneNode::serializeAttributes(out, options);
  247. if (options && (options->Flags&io::EARWF_USE_RELATIVE_PATHS) && options->Filename)
  248. {
  249. const io::path path = SceneManager->getFileSystem()->getRelativeFilename(
  250. SceneManager->getFileSystem()->getAbsolutePath(SceneManager->getMeshCache()->getMeshName(Mesh).getPath()),
  251. options->Filename);
  252. out->addString("Mesh", path.c_str());
  253. }
  254. else
  255. out->addString("Mesh", SceneManager->getMeshCache()->getMeshName(Mesh).getPath().c_str());
  256. out->addBool("ReadOnlyMaterials", ReadOnlyMaterials);
  257. }
  258. //! Reads attributes of the scene node.
  259. void CMeshSceneNode::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
  260. {
  261. io::path oldMeshStr = SceneManager->getMeshCache()->getMeshName(Mesh);
  262. io::path newMeshStr = in->getAttributeAsString("Mesh");
  263. ReadOnlyMaterials = in->getAttributeAsBool("ReadOnlyMaterials");
  264. if (newMeshStr != "" && oldMeshStr != newMeshStr)
  265. {
  266. IMesh* newMesh = 0;
  267. IAnimatedMesh* newAnimatedMesh = SceneManager->getMesh(newMeshStr.c_str());
  268. if (newAnimatedMesh)
  269. newMesh = newAnimatedMesh->getMesh(0);
  270. if (newMesh)
  271. setMesh(newMesh);
  272. }
  273. // optional attribute to assign the hint to the whole mesh
  274. if (in->existsAttribute("HardwareMappingHint") &&
  275. in->existsAttribute("HardwareMappingBufferType"))
  276. {
  277. scene::E_HARDWARE_MAPPING mapping = scene::EHM_NEVER;
  278. scene::E_BUFFER_TYPE bufferType = scene::EBT_NONE;
  279. core::stringc smapping = in->getAttributeAsString("HardwareMappingHint");
  280. if (smapping.equals_ignore_case("static"))
  281. mapping = scene::EHM_STATIC;
  282. else if (smapping.equals_ignore_case("dynamic"))
  283. mapping = scene::EHM_DYNAMIC;
  284. else if (smapping.equals_ignore_case("stream"))
  285. mapping = scene::EHM_STREAM;
  286. core::stringc sbufferType = in->getAttributeAsString("HardwareMappingBufferType");
  287. if (sbufferType.equals_ignore_case("vertex"))
  288. bufferType = scene::EBT_VERTEX;
  289. else if (sbufferType.equals_ignore_case("index"))
  290. bufferType = scene::EBT_INDEX;
  291. else if (sbufferType.equals_ignore_case("vertexindex"))
  292. bufferType = scene::EBT_VERTEX_AND_INDEX;
  293. IMesh* mesh = getMesh();
  294. if (mesh)
  295. mesh->setHardwareMappingHint(mapping, bufferType);
  296. }
  297. IMeshSceneNode::deserializeAttributes(in, options);
  298. }
  299. //! Sets if the scene node should not copy the materials of the mesh but use them in a read only style.
  300. /* In this way it is possible to change the materials a mesh causing all mesh scene nodes
  301. referencing this mesh to change too. */
  302. void CMeshSceneNode::setReadOnlyMaterials(bool readonly)
  303. {
  304. ReadOnlyMaterials = readonly;
  305. }
  306. //! Returns if the scene node should not copy the materials of the mesh but use them in a read only style
  307. bool CMeshSceneNode::isReadOnlyMaterials() const
  308. {
  309. return ReadOnlyMaterials;
  310. }
  311. //! Creates a clone of this scene node and its children.
  312. ISceneNode* CMeshSceneNode::clone(ISceneNode* newParent, ISceneManager* newManager)
  313. {
  314. if (!newParent)
  315. newParent = Parent;
  316. if (!newManager)
  317. newManager = SceneManager;
  318. CMeshSceneNode* nb = new CMeshSceneNode(Mesh, newParent,
  319. newManager, ID, RelativeTranslation, RelativeRotation, RelativeScale);
  320. nb->cloneMembers(this, newManager);
  321. nb->ReadOnlyMaterials = ReadOnlyMaterials;
  322. nb->Materials = Materials;
  323. if (newParent)
  324. nb->drop();
  325. return nb;
  326. }
  327. } // end namespace scene
  328. } // end namespace irr