| 1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
|---|---|
| 2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
| 3 | #include "Components/BsCCamera.h" |
| 4 | #include "Private/RTTI/BsCCameraRTTI.h" |
| 5 | #include "Scene/BsSceneObject.h" |
| 6 | #include "Scene/BsSceneManager.h" |
| 7 | #include "BsCoreApplication.h" |
| 8 | |
| 9 | namespace bs |
| 10 | { |
| 11 | CCamera::CCamera() |
| 12 | { |
| 13 | setFlag(ComponentFlag::AlwaysRun, true); |
| 14 | setName("Camera"); |
| 15 | } |
| 16 | |
| 17 | CCamera::CCamera(const HSceneObject& parent) |
| 18 | : Component(parent) |
| 19 | { |
| 20 | setFlag(ComponentFlag::AlwaysRun, true); |
| 21 | setName("Camera"); |
| 22 | } |
| 23 | |
| 24 | ConvexVolume CCamera::getWorldFrustum() const |
| 25 | { |
| 26 | const Vector<Plane>& frustumPlanes = getFrustum().getPlanes(); |
| 27 | Matrix4 worldMatrix = SO()->getWorldMatrix(); |
| 28 | |
| 29 | Vector<Plane> worldPlanes(frustumPlanes.size()); |
| 30 | UINT32 i = 0; |
| 31 | for (auto& plane : frustumPlanes) |
| 32 | { |
| 33 | worldPlanes[i] = worldMatrix.multiplyAffine(plane); |
| 34 | i++; |
| 35 | } |
| 36 | |
| 37 | return ConvexVolume(worldPlanes); |
| 38 | } |
| 39 | |
| 40 | void CCamera::updateView() const |
| 41 | { |
| 42 | mInternal->_updateState(*SO()); |
| 43 | } |
| 44 | |
| 45 | void CCamera::setMain(bool main) |
| 46 | { |
| 47 | mInternal->setMain(main); |
| 48 | } |
| 49 | |
| 50 | void CCamera::_instantiate() |
| 51 | { |
| 52 | // If mInternal already exists this means this object was deserialized, |
| 53 | // so all we need to do is initialize it. |
| 54 | if (mInternal != nullptr) |
| 55 | mInternal->initialize(); |
| 56 | else |
| 57 | mInternal = Camera::create(); |
| 58 | } |
| 59 | |
| 60 | void CCamera::onInitialized() |
| 61 | { |
| 62 | gSceneManager()._bindActor(mInternal, SO()); |
| 63 | |
| 64 | // Make sure primary RT gets applied if camera gets deserialized with main camera state |
| 65 | gSceneManager()._notifyMainCameraStateChanged(mInternal); |
| 66 | } |
| 67 | |
| 68 | void CCamera::onDestroyed() |
| 69 | { |
| 70 | gSceneManager()._unbindActor(mInternal); |
| 71 | |
| 72 | mInternal->destroy(); |
| 73 | } |
| 74 | |
| 75 | RTTITypeBase* CCamera::getRTTIStatic() |
| 76 | { |
| 77 | return CCameraRTTI::instance(); |
| 78 | } |
| 79 | |
| 80 | RTTITypeBase* CCamera::getRTTI() const |
| 81 | { |
| 82 | return CCamera::getRTTIStatic(); |
| 83 | } |
| 84 | } |
| 85 |