CameraNode.cpp 4.3 KB

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