BsCRenderable.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Components/BsCRenderable.h"
  4. #include "Private/RTTI/BsCRenderableRTTI.h"
  5. #include "Scene/BsSceneObject.h"
  6. #include "Mesh/BsMesh.h"
  7. #include "Material/BsMaterial.h"
  8. #include "Components/BsCAnimation.h"
  9. #include "Math/BsBounds.h"
  10. #include "Scene/BsSceneManager.h"
  11. namespace bs
  12. {
  13. CRenderable::CRenderable()
  14. {
  15. setName("Renderable");
  16. setFlag(ComponentFlag::AlwaysRun, true);
  17. }
  18. CRenderable::CRenderable(const HSceneObject& parent)
  19. :Component(parent)
  20. {
  21. setName("Renderable");
  22. setFlag(ComponentFlag::AlwaysRun, true);
  23. }
  24. void CRenderable::setMesh(HMesh mesh)
  25. {
  26. mInternal->setMesh(mesh);
  27. if (mAnimation != nullptr)
  28. mAnimation->_updateBounds(false);
  29. }
  30. void CRenderable::onInitialized()
  31. {
  32. // If mInternal already exists this means this object was deserialized,
  33. // so all we need to do is initialize it.
  34. if (mInternal != nullptr)
  35. mInternal->initialize();
  36. else
  37. mInternal = Renderable::create();
  38. gSceneManager()._bindActor(mInternal, sceneObject());
  39. mAnimation = SO()->getComponent<CAnimation>();
  40. if (mAnimation != nullptr)
  41. {
  42. _registerAnimation(mAnimation);
  43. mAnimation->_registerRenderable(mThisHandle);
  44. }
  45. }
  46. Bounds CRenderable::getBounds() const
  47. {
  48. mInternal->_updateState(*SO());
  49. return mInternal->getBounds();
  50. }
  51. bool CRenderable::calculateBounds(Bounds& bounds)
  52. {
  53. bounds = getBounds();
  54. return true;
  55. }
  56. void CRenderable::_registerAnimation(const HAnimation& animation)
  57. {
  58. mAnimation = animation;
  59. if (mInternal != nullptr)
  60. {
  61. mInternal->setAnimation(animation->_getInternal());
  62. // Need to update transform because animated renderables handle local transforms through bones, so it
  63. // shouldn't be included in the renderable's transform.
  64. mInternal->_updateState(*SO(), true);
  65. }
  66. }
  67. void CRenderable::_unregisterAnimation()
  68. {
  69. mAnimation = nullptr;
  70. if(mInternal != nullptr)
  71. {
  72. mInternal->setAnimation(nullptr);
  73. // Need to update transform because animated renderables handle local transforms through bones, so it
  74. // shouldn't be included in the renderable's transform.
  75. mInternal->_updateState(*SO(), true);
  76. }
  77. }
  78. void CRenderable::update()
  79. {
  80. }
  81. void CRenderable::onDestroyed()
  82. {
  83. if (mAnimation != nullptr)
  84. mAnimation->_unregisterRenderable();
  85. gSceneManager()._unbindActor(mInternal);
  86. mInternal->destroy();
  87. }
  88. RTTITypeBase* CRenderable::getRTTIStatic()
  89. {
  90. return CRenderableRTTI::instance();
  91. }
  92. RTTITypeBase* CRenderable::getRTTI() const
  93. {
  94. return CRenderable::getRTTIStatic();
  95. }
  96. }