Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
CameraComponent.hpp
1 // Copyright (C) 2017 Jérôme Leclercq
2 // This file is part of the "Nazara Development Kit"
3 // For conditions of distribution and use, see copyright notice in Prerequesites.hpp
4 
5 #pragma once
6 
7 #ifndef NDK_SERVER
8 #ifndef NDK_COMPONENTS_CAMERACOMPONENT_HPP
9 #define NDK_COMPONENTS_CAMERACOMPONENT_HPP
10 
11 #include <Nazara/Math/Frustum.hpp>
12 #include <Nazara/Math/Matrix4.hpp>
13 #include <Nazara/Math/Vector3.hpp>
14 #include <Nazara/Graphics/AbstractViewer.hpp>
15 #include <Nazara/Renderer/RenderTarget.hpp>
16 #include <Nazara/Utility/Node.hpp>
17 #include <NDK/Component.hpp>
18 
19 namespace Ndk
20 {
21  class CameraComponent;
22 
23  using CameraComponentHandle = Nz::ObjectHandle<CameraComponent>;
24 
25  class NDK_API CameraComponent : public Component<CameraComponent>, public Nz::AbstractViewer, public Nz::HandledObject<CameraComponent>
26  {
27  public:
28  inline CameraComponent();
29  inline CameraComponent(const CameraComponent& camera);
30  ~CameraComponent() = default;
31 
32  void ApplyView() const override;
33 
34  inline void EnsureFrustumUpdate() const;
35  inline void EnsureProjectionMatrixUpdate() const;
36  inline void EnsureViewMatrixUpdate() const;
37  inline void EnsureViewportUpdate() const;
38 
39  float GetAspectRatio() const override;
40  Nz::Vector3f GetEyePosition() const override;
41  Nz::Vector3f GetForward() const override;
42  inline float GetFOV() const;
43  const Nz::Frustumf& GetFrustum() const override;
44  inline unsigned int GetLayer() const;
45  const Nz::Matrix4f& GetProjectionMatrix() const override;
46  Nz::ProjectionType GetProjectionType() const override;
47  inline const Nz::Vector2f& GetSize() const;
48  const Nz::RenderTarget* GetTarget() const override;
49  inline const Nz::Rectf& GetTargetRegion() const;
50  const Nz::Matrix4f& GetViewMatrix() const override;
51  const Nz::Recti& GetViewport() const override;
52  float GetZFar() const override;
53  float GetZNear() const override;
54 
55  inline void SetFOV(float fov);
56  void SetLayer(unsigned int layer);
57  inline void SetProjectionType(Nz::ProjectionType projection);
58  inline void SetSize(const Nz::Vector2f& size);
59  inline void SetSize(float width, float height);
60  inline void SetTarget(const Nz::RenderTarget* renderTarget);
61  inline void SetTargetRegion(const Nz::Rectf& region);
62  inline void SetViewport(const Nz::Recti& viewport);
63  inline void SetZFar(float zFar);
64  inline void SetZNear(float zNear);
65 
66  inline bool UpdateVisibility(std::size_t visibilityHash);
67 
68  static ComponentIndex componentIndex;
69 
70  private:
71  inline void InvalidateFrustum() const;
72  inline void InvalidateProjectionMatrix() const;
73  inline void InvalidateViewMatrix() const;
74  inline void InvalidateViewport() const;
75 
76  void OnAttached() override;
77  void OnComponentAttached(BaseComponent& component) override;
78  void OnComponentDetached(BaseComponent& component) override;
79  void OnDetached() override;
80  void OnNodeInvalidated(const Nz::Node* node);
81  void OnRenderTargetRelease(const Nz::RenderTarget* renderTarget);
82  void OnRenderTargetSizeChange(const Nz::RenderTarget* renderTarget);
83 
84  void UpdateFrustum() const;
85  void UpdateProjectionMatrix() const;
86  void UpdateViewMatrix() const;
87  void UpdateViewport() const;
88 
89  NazaraSlot(Nz::Node, OnNodeInvalidation, m_nodeInvalidationSlot);
90  NazaraSlot(Nz::RenderTarget, OnRenderTargetRelease, m_targetReleaseSlot);
91  NazaraSlot(Nz::RenderTarget, OnRenderTargetSizeChange, m_targetResizeSlot);
92 
93  std::size_t m_visibilityHash;
94  Nz::ProjectionType m_projectionType;
95  mutable Nz::Frustumf m_frustum;
96  mutable Nz::Matrix4f m_projectionMatrix;
97  mutable Nz::Matrix4f m_viewMatrix;
98  Nz::Rectf m_targetRegion;
99  mutable Nz::Recti m_viewport;
100  const Nz::RenderTarget* m_target;
101  Nz::Vector2f m_size;
102  mutable bool m_frustumUpdated;
103  mutable bool m_projectionMatrixUpdated;
104  mutable bool m_viewMatrixUpdated;
105  mutable bool m_viewportUpdated;
106  mutable float m_aspectRatio;
107  float m_fov;
108  float m_zFar;
109  float m_zNear;
110  unsigned int m_layer;
111  };
112 }
113 
114 #include <NDK/Components/CameraComponent.inl>
115 
116 #endif // NDK_COMPONENTS_CAMERACOMPONENT_HPP
117 #endif // NDK_SERVER
TODO: For now is unable to display different color in the history, it needs a RichTextDrawer to do so...
Definition: Algorithm.hpp:12
Math class that represents a frustum in the three dimensional vector space.
Definition: Frustum.hpp:24
Graphics class that represents the viewer for our scene.
Definition: AbstractViewer.hpp:21
NDK class that represents the component for camera.
Definition: CameraComponent.hpp:25
NDK class that represents the common base of all components.
Definition: BaseComponent.hpp:17