CameraNode.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Scene/CameraNode.h>
  6. #include <AnKi/Scene/Components/FrustumComponent.h>
  7. #include <AnKi/Scene/Components/MoveComponent.h>
  8. #include <AnKi/Scene/Components/SpatialComponent.h>
  9. #include <AnKi/Scene/SceneGraph.h>
  10. namespace anki {
  11. /// Feedback component.
  12. class CameraNode::MoveFeedbackComponent : public SceneComponent
  13. {
  14. ANKI_SCENE_COMPONENT(CameraNode::MoveFeedbackComponent)
  15. public:
  16. MoveFeedbackComponent(SceneNode* node)
  17. : SceneComponent(node, getStaticClassId(), true)
  18. {
  19. }
  20. ANKI_USE_RESULT Error update(SceneNode& node, Second prevTime, Second crntTime, Bool& updated) override
  21. {
  22. updated = false;
  23. MoveComponent& move = node.getFirstComponentOfType<MoveComponent>();
  24. if(move.getTimestamp() == node.getGlobalTimestamp())
  25. {
  26. CameraNode& cam = static_cast<CameraNode&>(node);
  27. cam.onMoveComponentUpdate(move);
  28. }
  29. return Error::NONE;
  30. }
  31. };
  32. ANKI_SCENE_COMPONENT_STATICS(CameraNode::MoveFeedbackComponent)
  33. CameraNode::CameraNode(SceneGraph* scene, CString name)
  34. : SceneNode(scene, name)
  35. {
  36. }
  37. CameraNode::~CameraNode()
  38. {
  39. }
  40. void CameraNode::initCommon(FrustumType frustumType)
  41. {
  42. // Move component
  43. newComponent<MoveComponent>();
  44. // Feedback component
  45. newComponent<MoveFeedbackComponent>();
  46. // Frustum component
  47. FrustumComponent* frc = newComponent<FrustumComponent>();
  48. frc->setFrustumType(frustumType);
  49. const FrustumComponentVisibilityTestFlag visibilityFlags =
  50. FrustumComponentVisibilityTestFlag::RENDER_COMPONENTS | FrustumComponentVisibilityTestFlag::LIGHT_COMPONENTS
  51. | FrustumComponentVisibilityTestFlag::LENS_FLARE_COMPONENTS
  52. | FrustumComponentVisibilityTestFlag::REFLECTION_PROBES | FrustumComponentVisibilityTestFlag::REFLECTION_PROXIES
  53. | FrustumComponentVisibilityTestFlag::OCCLUDERS | FrustumComponentVisibilityTestFlag::DECALS
  54. | FrustumComponentVisibilityTestFlag::FOG_DENSITY_COMPONENTS
  55. | FrustumComponentVisibilityTestFlag::GLOBAL_ILLUMINATION_PROBES | FrustumComponentVisibilityTestFlag::EARLY_Z
  56. | FrustumComponentVisibilityTestFlag::ALL_SHADOWS_ENABLED
  57. | FrustumComponentVisibilityTestFlag::GENERIC_COMPUTE_JOB_COMPONENTS
  58. | FrustumComponentVisibilityTestFlag::UI_COMPONENTS;
  59. frc->setEnabledVisibilityTests(visibilityFlags);
  60. frc->setLodDistance(0, getSceneGraph().getConfig().m_maxLodDistances[0]);
  61. frc->setLodDistance(1, getSceneGraph().getConfig().m_maxLodDistances[1]);
  62. // Extended frustum for RT
  63. if(getSceneGraph().getConfig().m_rayTracedShadows)
  64. {
  65. FrustumComponent* rtFrustumComponent = newComponent<FrustumComponent>();
  66. rtFrustumComponent->setFrustumType(FrustumType::ORTHOGRAPHIC);
  67. rtFrustumComponent->setEnabledVisibilityTests(FrustumComponentVisibilityTestFlag::RAY_TRACING_SHADOWS);
  68. const F32 dist = getSceneGraph().getConfig().m_rayTracingExtendedFrustumDistance;
  69. rtFrustumComponent->setOrthographic(0.1f, dist * 2.0f, dist, -dist, dist, -dist);
  70. rtFrustumComponent->setLodDistance(0, getSceneGraph().getConfig().m_maxLodDistances[0]);
  71. rtFrustumComponent->setLodDistance(1, getSceneGraph().getConfig().m_maxLodDistances[1]);
  72. }
  73. }
  74. void CameraNode::onMoveComponentUpdate(MoveComponent& move)
  75. {
  76. const Transform& worldTransform = move.getWorldTransform();
  77. // Frustum
  78. U count = 0;
  79. iterateComponentsOfType<FrustumComponent>([&](FrustumComponent& fc) {
  80. if(count == 0)
  81. {
  82. fc.setWorldTransform(worldTransform);
  83. }
  84. else
  85. {
  86. // Extended RT frustum, re-align it so the frustum is positioned at the center of the camera eye
  87. ANKI_ASSERT(fc.getFrustumType() == FrustumType::ORTHOGRAPHIC);
  88. const F32 far = fc.getFar();
  89. Transform extendedFrustumTransform = Transform::getIdentity();
  90. Vec3 newOrigin = worldTransform.getOrigin().xyz();
  91. newOrigin.z() += far / 2.0f;
  92. extendedFrustumTransform.setOrigin(newOrigin.xyz0());
  93. fc.setWorldTransform(extendedFrustumTransform);
  94. }
  95. ++count;
  96. });
  97. }
  98. PerspectiveCameraNode::PerspectiveCameraNode(SceneGraph* scene, CString name)
  99. : CameraNode(scene, name)
  100. {
  101. initCommon(FrustumType::PERSPECTIVE);
  102. }
  103. PerspectiveCameraNode::~PerspectiveCameraNode()
  104. {
  105. }
  106. OrthographicCameraNode::OrthographicCameraNode(SceneGraph* scene, CString name)
  107. : CameraNode(scene, name)
  108. {
  109. initCommon(FrustumType::ORTHOGRAPHIC);
  110. }
  111. OrthographicCameraNode::~OrthographicCameraNode()
  112. {
  113. }
  114. } // end namespace anki