MxEngine
CameraController.h
1 // Copyright(c) 2019 - 2020, #Momo
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met :
6 //
7 // 1. Redistributions of source code must retain the above copyright notice, this
8 // list of conditions and the following disclaimer.
9 //
10 // 2. Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and /or other materials provided with the distribution.
13 //
14 // 3. Neither the name of the copyright holder nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #pragma once
30 
31 #include "Utilities/ECS/Component.h"
32 #include "Utilities/Memory/Memory.h"
33 #include "Platform/GraphicAPI.h"
34 #include "CameraBase.h"
35 
36 namespace MxEngine
37 {
38  enum class CameraType : uint8_t
39  {
40  PERSPECTIVE,
41  ORTHOGRAPHIC,
42  FRUSTRUM,
43  };
44 
45  struct CameraRender
46  {
47  GResource<FrameBuffer> framebufferMSAA;
48  GResource<RenderBuffer> renderbufferMSAA;
49  GResource<FrameBuffer> framebufferHDR;
50  GResource<Texture> bloomTextureHDR;
51 
52  void Init(int width, int height);
53  void Resize(int width, int height);
54  void DeInit();
55  };
56 
58  {
59  MAKE_COMPONENT(CameraController);
60 
61  UniqueRef<CameraRender> renderBuffers = MakeUnique<CameraRender>();
62  GResource<Texture> renderTexture;
63 
64  Vector3 direction = { 1.0f, 0.0f, 0.0f };
65  Vector3 up = { 0.0f, 1.0f, 0.0f };
66  Vector3 forward ={ 0.0f, 0.0f, 1.0f };
67  Vector3 right = { 1.0f, 0.0f, 0.0f };
68  float verticalAngle = 0.0f;
69  float horizontalAngle = 0.0f;
70  float exposure = 1.0f;
71  float bloomWeight = 0.5f;
72  float moveSpeed = 1.0f;
73  float rotateSpeed = 10.0f;
74 
75  void SubmitMatrixProjectionChanges() const;
76 
77  uint8_t bloomIterations = 6;
78  CameraType cameraType = CameraType::PERSPECTIVE;
79  bool renderingEnabled = true;
80  public:
81  mutable CameraBase Camera;
82 
85  CameraController(CameraController&&) noexcept = default;
86  CameraController& operator=(CameraController&&) noexcept = default;
87 
88  template<typename T>
89  T& GetCamera();
90  template<typename T>
91  const T& GetCamera() const;
92  void SetCameraType(CameraType type);
93  CameraType GetCameraType() const;
94 
95  const Matrix4x4& GetMatrix(const Vector3& position) const;
96  Matrix4x4 GetStaticMatrix() const;
97  GResource<FrameBuffer> GetFrameBufferMSAA() const;
98  GResource<FrameBuffer> GetFrameBufferHDR() const;
99  GResource<RenderBuffer> GetRenderBufferMSAA() const;
100  GResource<Texture> GetBloomTexture() const;
101  GResource<Texture> GetRenderTexture() const;
102  void ListenWindowResizeEvent();
103  void ResizeRenderTexture(size_t width, size_t height);
104  void SetRenderTexture(const GResource<Texture>& texture);
105  bool IsRendered() const;
106  void ToggleRendering(bool value);
107 
108  const Vector3& GetDirection() const;
109  void SetDirection(const Vector3& direction);
110  Vector3 GetDirectionUp() const;
111  float GetHorizontalAngle() const;
112  float GetVerticalAngle() const;
113  void SetBloomWeight(float weight);
114  float GetBloomWeight() const;
115  size_t GetBloomIterations() const;
116  void SetBloomIterations(size_t iterCount);
117  float GetExposure() const;
118  void SetExposure(float exp);
119  float GetMoveSpeed() const;
120  void SetMoveSpeed(float speed);
121  float GetRotateSpeed() const;
122  void SetRotateSpeed(float speed);
123  CameraController& Rotate(float horz, float vert);
124 
125  void SetForwardVector(const Vector3& forward);
126  void SetUpVector(const Vector3& up);
127  void SetRightVector(const Vector3& right);
128 
129  const Vector3& GetForwardVector() const;
130  const Vector3& GetUpVector() const;
131  const Vector3& GetRightVector() const;
132  };
133 }
Definition: AbstractFactory.h:61
Definition: CameraBase.h:35
Definition: CameraController.h:45
Definition: CameraController.h:57
Definition: Application.cpp:49