CMeshSceneNode.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 = 0;
  90. driver->setMaterial(m);
  91. if (DebugDataVisible & scene::EDS_BBOX) {
  92. driver->draw3DBox(Box, video::SColor(255, 255, 255, 255));
  93. }
  94. if (DebugDataVisible & scene::EDS_BBOX_BUFFERS) {
  95. for (u32 g = 0; g < Mesh->getMeshBufferCount(); ++g) {
  96. driver->draw3DBox(
  97. Mesh->getMeshBuffer(g)->getBoundingBox(),
  98. video::SColor(255, 190, 128, 128));
  99. }
  100. }
  101. if (DebugDataVisible & scene::EDS_NORMALS) {
  102. // draw normals
  103. const f32 debugNormalLength = 1.f;
  104. const video::SColor debugNormalColor = video::SColor(255, 34, 221, 221);
  105. const u32 count = Mesh->getMeshBufferCount();
  106. for (u32 i = 0; i != count; ++i) {
  107. driver->drawMeshBufferNormals(Mesh->getMeshBuffer(i), debugNormalLength, debugNormalColor);
  108. }
  109. }
  110. // show mesh
  111. if (DebugDataVisible & scene::EDS_MESH_WIRE_OVERLAY) {
  112. m.Wireframe = true;
  113. driver->setMaterial(m);
  114. for (u32 g = 0; g < Mesh->getMeshBufferCount(); ++g) {
  115. driver->drawMeshBuffer(Mesh->getMeshBuffer(g));
  116. }
  117. }
  118. }
  119. }
  120. //! Removes a child from this scene node.
  121. //! Implemented here, to be able to remove the shadow properly, if there is one,
  122. //! or to remove attached childs.
  123. bool CMeshSceneNode::removeChild(ISceneNode *child)
  124. {
  125. return ISceneNode::removeChild(child);
  126. }
  127. //! returns the axis aligned bounding box of this node
  128. const core::aabbox3d<f32> &CMeshSceneNode::getBoundingBox() const
  129. {
  130. return Mesh ? Mesh->getBoundingBox() : Box;
  131. }
  132. //! returns the material based on the zero based index i. To get the amount
  133. //! of materials used by this scene node, use getMaterialCount().
  134. //! This function is needed for inserting the node into the scene hierarchy on a
  135. //! optimal position for minimizing renderstate changes, but can also be used
  136. //! to directly modify the material of a scene node.
  137. video::SMaterial &CMeshSceneNode::getMaterial(u32 i)
  138. {
  139. if (Mesh && ReadOnlyMaterials && i < Mesh->getMeshBufferCount()) {
  140. ReadOnlyMaterial = Mesh->getMeshBuffer(i)->getMaterial();
  141. return ReadOnlyMaterial;
  142. }
  143. if (i >= Materials.size())
  144. return ISceneNode::getMaterial(i);
  145. return Materials[i];
  146. }
  147. //! returns amount of materials used by this scene node.
  148. u32 CMeshSceneNode::getMaterialCount() const
  149. {
  150. if (Mesh && ReadOnlyMaterials)
  151. return Mesh->getMeshBufferCount();
  152. return Materials.size();
  153. }
  154. //! Sets a new mesh
  155. void CMeshSceneNode::setMesh(IMesh *mesh)
  156. {
  157. if (mesh) {
  158. mesh->grab();
  159. if (Mesh)
  160. Mesh->drop();
  161. Mesh = mesh;
  162. copyMaterials();
  163. }
  164. }
  165. void CMeshSceneNode::copyMaterials()
  166. {
  167. Materials.clear();
  168. if (Mesh) {
  169. video::SMaterial mat;
  170. for (u32 i = 0; i < Mesh->getMeshBufferCount(); ++i) {
  171. IMeshBuffer *mb = Mesh->getMeshBuffer(i);
  172. if (mb)
  173. mat = mb->getMaterial();
  174. Materials.push_back(mat);
  175. }
  176. }
  177. }
  178. //! Sets if the scene node should not copy the materials of the mesh but use them in a read only style.
  179. /* In this way it is possible to change the materials a mesh causing all mesh scene nodes
  180. referencing this mesh to change too. */
  181. void CMeshSceneNode::setReadOnlyMaterials(bool readonly)
  182. {
  183. ReadOnlyMaterials = readonly;
  184. }
  185. //! Returns if the scene node should not copy the materials of the mesh but use them in a read only style
  186. bool CMeshSceneNode::isReadOnlyMaterials() const
  187. {
  188. return ReadOnlyMaterials;
  189. }
  190. //! Creates a clone of this scene node and its children.
  191. ISceneNode *CMeshSceneNode::clone(ISceneNode *newParent, ISceneManager *newManager)
  192. {
  193. if (!newParent)
  194. newParent = Parent;
  195. if (!newManager)
  196. newManager = SceneManager;
  197. CMeshSceneNode *nb = new CMeshSceneNode(Mesh, newParent,
  198. newManager, ID, RelativeTranslation, RelativeRotation, RelativeScale);
  199. nb->cloneMembers(this, newManager);
  200. nb->ReadOnlyMaterials = ReadOnlyMaterials;
  201. nb->Materials = Materials;
  202. if (newParent)
  203. nb->drop();
  204. return nb;
  205. }
  206. } // end namespace scene
  207. } // end namespace irr