CMeshSceneNode.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 "IMeshCache.h"
  8. #include "IMeshBuffer.h"
  9. #include "IMaterialRenderer.h"
  10. #include "IFileSystem.h"
  11. namespace irr
  12. {
  13. namespace scene
  14. {
  15. //! constructor
  16. CMeshSceneNode::CMeshSceneNode(IMesh *mesh, ISceneNode *parent, ISceneManager *mgr, s32 id,
  17. const core::vector3df &position, const core::vector3df &rotation,
  18. const core::vector3df &scale) :
  19. IMeshSceneNode(parent, mgr, id, position, rotation, scale),
  20. Mesh(0),
  21. PassCount(0), ReadOnlyMaterials(false)
  22. {
  23. setMesh(mesh);
  24. }
  25. //! destructor
  26. CMeshSceneNode::~CMeshSceneNode()
  27. {
  28. if (Mesh)
  29. Mesh->drop();
  30. }
  31. //! frame
  32. void CMeshSceneNode::OnRegisterSceneNode()
  33. {
  34. if (IsVisible && Mesh) {
  35. // because this node supports rendering of mixed mode meshes consisting of
  36. // transparent and solid material at the same time, we need to go through all
  37. // materials, check of what type they are and register this node for the right
  38. // render pass according to that.
  39. video::IVideoDriver *driver = SceneManager->getVideoDriver();
  40. PassCount = 0;
  41. int transparentCount = 0;
  42. int solidCount = 0;
  43. // count transparent and solid materials in this scene node
  44. const u32 numMaterials = ReadOnlyMaterials ? Mesh->getMeshBufferCount() : Materials.size();
  45. for (u32 i = 0; i < numMaterials; ++i) {
  46. const video::SMaterial &material = ReadOnlyMaterials ? Mesh->getMeshBuffer(i)->getMaterial() : Materials[i];
  47. if (driver->needsTransparentRenderPass(material))
  48. ++transparentCount;
  49. else
  50. ++solidCount;
  51. if (solidCount && transparentCount)
  52. break;
  53. }
  54. // register according to material types counted
  55. if (solidCount)
  56. SceneManager->registerNodeForRendering(this, scene::ESNRP_SOLID);
  57. if (transparentCount)
  58. SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT);
  59. ISceneNode::OnRegisterSceneNode();
  60. }
  61. }
  62. //! renders the node.
  63. void CMeshSceneNode::render()
  64. {
  65. video::IVideoDriver *driver = SceneManager->getVideoDriver();
  66. if (!Mesh || !driver)
  67. return;
  68. const bool isTransparentPass =
  69. SceneManager->getSceneNodeRenderPass() == scene::ESNRP_TRANSPARENT;
  70. ++PassCount;
  71. driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
  72. Box = Mesh->getBoundingBox();
  73. for (u32 i = 0; i < Mesh->getMeshBufferCount(); ++i) {
  74. scene::IMeshBuffer *mb = Mesh->getMeshBuffer(i);
  75. if (mb) {
  76. const video::SMaterial &material = ReadOnlyMaterials ? mb->getMaterial() : Materials[i];
  77. const bool transparent = driver->needsTransparentRenderPass(material);
  78. // only render transparent buffer if this is the transparent render pass
  79. // and solid only in solid pass
  80. if (transparent == isTransparentPass) {
  81. driver->setMaterial(material);
  82. driver->drawMeshBuffer(mb);
  83. }
  84. }
  85. }
  86. // for debug purposes only:
  87. if (DebugDataVisible && PassCount == 1) {
  88. video::SMaterial m;
  89. m.AntiAliasing = video::EAAM_OFF;
  90. m.ZBuffer = video::ECFN_DISABLED;
  91. driver->setMaterial(m);
  92. if (DebugDataVisible & scene::EDS_BBOX_BUFFERS) {
  93. for (u32 g = 0; g < Mesh->getMeshBufferCount(); ++g) {
  94. driver->draw3DBox(
  95. Mesh->getMeshBuffer(g)->getBoundingBox(),
  96. video::SColor(255, 190, 128, 128));
  97. }
  98. }
  99. if (DebugDataVisible & scene::EDS_BBOX) {
  100. driver->draw3DBox(Box, video::SColor(255, 255, 255, 255));
  101. }
  102. if (DebugDataVisible & scene::EDS_NORMALS) {
  103. // draw normals
  104. const f32 debugNormalLength = 1.f;
  105. const video::SColor debugNormalColor = video::SColor(255, 34, 221, 221);
  106. const u32 count = Mesh->getMeshBufferCount();
  107. for (u32 i = 0; i != count; ++i) {
  108. driver->drawMeshBufferNormals(Mesh->getMeshBuffer(i), debugNormalLength, debugNormalColor);
  109. }
  110. }
  111. // show mesh
  112. if (DebugDataVisible & scene::EDS_MESH_WIRE_OVERLAY) {
  113. m.Wireframe = true;
  114. driver->setMaterial(m);
  115. for (u32 g = 0; g < Mesh->getMeshBufferCount(); ++g) {
  116. driver->drawMeshBuffer(Mesh->getMeshBuffer(g));
  117. }
  118. }
  119. }
  120. }
  121. //! Removes a child from this scene node.
  122. //! Implemented here, to be able to remove the shadow properly, if there is one,
  123. //! or to remove attached childs.
  124. bool CMeshSceneNode::removeChild(ISceneNode *child)
  125. {
  126. return ISceneNode::removeChild(child);
  127. }
  128. //! returns the axis aligned bounding box of this node
  129. const core::aabbox3d<f32> &CMeshSceneNode::getBoundingBox() const
  130. {
  131. return Mesh ? Mesh->getBoundingBox() : Box;
  132. }
  133. //! returns the material based on the zero based index i. To get the amount
  134. //! of materials used by this scene node, use getMaterialCount().
  135. //! This function is needed for inserting the node into the scene hierarchy on a
  136. //! optimal position for minimizing renderstate changes, but can also be used
  137. //! to directly modify the material of a scene node.
  138. video::SMaterial &CMeshSceneNode::getMaterial(u32 i)
  139. {
  140. if (Mesh && ReadOnlyMaterials && i < Mesh->getMeshBufferCount()) {
  141. ReadOnlyMaterial = Mesh->getMeshBuffer(i)->getMaterial();
  142. return ReadOnlyMaterial;
  143. }
  144. if (i >= Materials.size())
  145. return ISceneNode::getMaterial(i);
  146. return Materials[i];
  147. }
  148. //! returns amount of materials used by this scene node.
  149. u32 CMeshSceneNode::getMaterialCount() const
  150. {
  151. if (Mesh && ReadOnlyMaterials)
  152. return Mesh->getMeshBufferCount();
  153. return Materials.size();
  154. }
  155. //! Sets a new mesh
  156. void CMeshSceneNode::setMesh(IMesh *mesh)
  157. {
  158. if (mesh) {
  159. mesh->grab();
  160. if (Mesh)
  161. Mesh->drop();
  162. Mesh = mesh;
  163. copyMaterials();
  164. }
  165. }
  166. void CMeshSceneNode::copyMaterials()
  167. {
  168. Materials.clear();
  169. if (Mesh) {
  170. video::SMaterial mat;
  171. for (u32 i = 0; i < Mesh->getMeshBufferCount(); ++i) {
  172. IMeshBuffer *mb = Mesh->getMeshBuffer(i);
  173. if (mb)
  174. mat = mb->getMaterial();
  175. Materials.push_back(mat);
  176. }
  177. }
  178. }
  179. //! Sets if the scene node should not copy the materials of the mesh but use them in a read only style.
  180. /* In this way it is possible to change the materials a mesh causing all mesh scene nodes
  181. referencing this mesh to change too. */
  182. void CMeshSceneNode::setReadOnlyMaterials(bool readonly)
  183. {
  184. ReadOnlyMaterials = readonly;
  185. }
  186. //! Returns if the scene node should not copy the materials of the mesh but use them in a read only style
  187. bool CMeshSceneNode::isReadOnlyMaterials() const
  188. {
  189. return ReadOnlyMaterials;
  190. }
  191. //! Creates a clone of this scene node and its children.
  192. ISceneNode *CMeshSceneNode::clone(ISceneNode *newParent, ISceneManager *newManager)
  193. {
  194. if (!newParent)
  195. newParent = Parent;
  196. if (!newManager)
  197. newManager = SceneManager;
  198. CMeshSceneNode *nb = new CMeshSceneNode(Mesh, newParent,
  199. newManager, ID, RelativeTranslation, RelativeRotation, RelativeScale);
  200. nb->cloneMembers(this, newManager);
  201. nb->ReadOnlyMaterials = ReadOnlyMaterials;
  202. nb->Materials = Materials;
  203. if (newParent)
  204. nb->drop();
  205. return nb;
  206. }
  207. } // end namespace scene
  208. } // end namespace irr