CSphereSceneNode.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 "CSphereSceneNode.h"
  5. #include "IVideoDriver.h"
  6. #include "ISceneManager.h"
  7. #include "S3DVertex.h"
  8. #include "os.h"
  9. namespace irr
  10. {
  11. namespace scene
  12. {
  13. //! constructor
  14. CSphereSceneNode::CSphereSceneNode(f32 radius, u32 polyCountX, u32 polyCountY, ISceneNode* parent, ISceneManager* mgr, s32 id,
  15. const core::vector3df& position, const core::vector3df& rotation, const core::vector3df& scale)
  16. : IMeshSceneNode(parent, mgr, id, position, rotation, scale), Mesh(0),
  17. Radius(radius), PolyCountX(polyCountX), PolyCountY(polyCountY)
  18. {
  19. #ifdef _DEBUG
  20. setDebugName("CSphereSceneNode");
  21. #endif
  22. Mesh = SceneManager->getGeometryCreator()->createSphereMesh(radius, polyCountX, polyCountY);
  23. }
  24. //! destructor
  25. CSphereSceneNode::~CSphereSceneNode()
  26. {
  27. if (Mesh)
  28. Mesh->drop();
  29. }
  30. //! renders the node.
  31. void CSphereSceneNode::render()
  32. {
  33. video::IVideoDriver* driver = SceneManager->getVideoDriver();
  34. if (Mesh && driver)
  35. {
  36. driver->setMaterial(Mesh->getMeshBuffer(0)->getMaterial());
  37. driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
  38. driver->drawMeshBuffer(Mesh->getMeshBuffer(0));
  39. if ( DebugDataVisible & scene::EDS_BBOX )
  40. {
  41. video::SMaterial m;
  42. m.Lighting = false;
  43. driver->setMaterial(m);
  44. driver->draw3DBox(Mesh->getMeshBuffer(0)->getBoundingBox(), video::SColor(255,255,255,255));
  45. }
  46. }
  47. }
  48. //! Removes a child from this scene node.
  49. //! Implemented here, to be able to remove the shadow properly, if there is one,
  50. //! or to remove attached childs.
  51. bool CSphereSceneNode::removeChild(ISceneNode* child)
  52. {
  53. return ISceneNode::removeChild(child);
  54. }
  55. //! returns the axis aligned bounding box of this node
  56. const core::aabbox3d<f32>& CSphereSceneNode::getBoundingBox() const
  57. {
  58. return Mesh ? Mesh->getBoundingBox() : Box;
  59. }
  60. void CSphereSceneNode::OnRegisterSceneNode()
  61. {
  62. if (IsVisible)
  63. SceneManager->registerNodeForRendering(this);
  64. ISceneNode::OnRegisterSceneNode();
  65. }
  66. //! returns the material based on the zero based index i. To get the amount
  67. //! of materials used by this scene node, use getMaterialCount().
  68. //! This function is needed for inserting the node into the scene hirachy on a
  69. //! optimal position for minimizing renderstate changes, but can also be used
  70. //! to directly modify the material of a scene node.
  71. video::SMaterial& CSphereSceneNode::getMaterial(u32 i)
  72. {
  73. if (i>0 || !Mesh)
  74. return ISceneNode::getMaterial(i);
  75. else
  76. return Mesh->getMeshBuffer(i)->getMaterial();
  77. }
  78. //! returns amount of materials used by this scene node.
  79. u32 CSphereSceneNode::getMaterialCount() const
  80. {
  81. return 1;
  82. }
  83. //! Writes attributes of the scene node.
  84. void CSphereSceneNode::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const
  85. {
  86. ISceneNode::serializeAttributes(out, options);
  87. out->addFloat("Radius", Radius);
  88. out->addInt("PolyCountX", PolyCountX);
  89. out->addInt("PolyCountY", PolyCountY);
  90. }
  91. //! Reads attributes of the scene node.
  92. void CSphereSceneNode::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
  93. {
  94. f32 oldRadius = Radius;
  95. u32 oldPolyCountX = PolyCountX;
  96. u32 oldPolyCountY = PolyCountY;
  97. Radius = in->getAttributeAsFloat("Radius");
  98. PolyCountX = in->getAttributeAsInt("PolyCountX");
  99. PolyCountY = in->getAttributeAsInt("PolyCountY");
  100. // legacy values read for compatibility with older versions
  101. u32 polyCount = in->getAttributeAsInt("PolyCount");
  102. if (PolyCountX ==0 && PolyCountY == 0)
  103. PolyCountX = PolyCountY = polyCount;
  104. Radius = core::max_(Radius, 0.0001f);
  105. if ( !core::equals(Radius, oldRadius) || PolyCountX != oldPolyCountX || PolyCountY != oldPolyCountY)
  106. {
  107. if (Mesh)
  108. Mesh->drop();
  109. Mesh = SceneManager->getGeometryCreator()->createSphereMesh(Radius, PolyCountX, PolyCountY);
  110. }
  111. ISceneNode::deserializeAttributes(in, options);
  112. }
  113. //! Creates a clone of this scene node and its children.
  114. ISceneNode* CSphereSceneNode::clone(ISceneNode* newParent, ISceneManager* newManager)
  115. {
  116. if (!newParent)
  117. newParent = Parent;
  118. if (!newManager)
  119. newManager = SceneManager;
  120. CSphereSceneNode* nb = new CSphereSceneNode(Radius, PolyCountX, PolyCountY, newParent,
  121. newManager, ID, RelativeTranslation);
  122. nb->cloneMembers(this, newManager);
  123. nb->getMaterial(0) = Mesh->getMeshBuffer(0)->getMaterial();
  124. if ( newParent )
  125. nb->drop();
  126. return nb;
  127. }
  128. } // end namespace scene
  129. } // end namespace irr