BsCamera.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "BsCamera.h"
  2. #include "BsCameraRTTI.h"
  3. #include "BsMath.h"
  4. #include "BsMatrix3.h"
  5. #include "BsVector2.h"
  6. #include "BsAABox.h"
  7. #include "BsSphere.h"
  8. #include "BsHardwareBufferManager.h"
  9. #include "BsVertexBuffer.h"
  10. #include "BsIndexBuffer.h"
  11. #include "BsException.h"
  12. #include "BsRenderAPI.h"
  13. #include "BsSceneObject.h"
  14. #include "BsDebug.h"
  15. #include "BsSceneManager.h"
  16. namespace BansheeEngine
  17. {
  18. Camera::Camera(const HSceneObject& parent, RenderTargetPtr target, float left, float top, float width, float height)
  19. : Component(parent), mLastUpdateHash(std::numeric_limits<UINT32>::max())
  20. {
  21. mInternal = CameraHandler::create(target, left, top, width, height);
  22. gSceneManager()._registerCamera(mInternal, parent);
  23. setName("Camera");
  24. }
  25. Camera::~Camera()
  26. {
  27. mInternal->destroy();
  28. }
  29. ConvexVolume Camera::getWorldFrustum() const
  30. {
  31. const Vector<Plane>& frustumPlanes = getFrustum().getPlanes();
  32. Matrix4 worldMatrix = SO()->getWorldTfrm();
  33. Vector<Plane> worldPlanes(frustumPlanes.size());
  34. UINT32 i = 0;
  35. for (auto& plane : frustumPlanes)
  36. {
  37. worldPlanes[i] = worldMatrix.multiplyAffine(plane);
  38. i++;
  39. }
  40. return ConvexVolume(worldPlanes);
  41. }
  42. void Camera::updateView() const
  43. {
  44. UINT32 curHash = SO()->getTransformHash();
  45. if (curHash != mLastUpdateHash)
  46. {
  47. mInternal->setPosition(SO()->getWorldPosition());
  48. mInternal->setRotation(SO()->getWorldRotation());
  49. mLastUpdateHash = curHash;
  50. }
  51. }
  52. void Camera::update()
  53. {
  54. updateView();
  55. }
  56. void Camera::onDestroyed()
  57. {
  58. gSceneManager()._unregisterCamera(mInternal);
  59. }
  60. RTTITypeBase* Camera::getRTTIStatic()
  61. {
  62. return CameraRTTI::instance();
  63. }
  64. RTTITypeBase* Camera::getRTTI() const
  65. {
  66. return Camera::getRTTIStatic();
  67. }
  68. }