BsCRenderable.cpp 2.5 KB

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