e779f494bbe1c4f4064960057845d8ca4cb98b0e
[platform/core/uifw/dali-core.git] / dali / internal / update / render-tasks / scene-graph-camera.h
1 #ifndef DALI_INTERNAL_SCENE_GRAPH_CAMERA_H
2 #define DALI_INTERNAL_SCENE_GRAPH_CAMERA_H
3
4 /*
5  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali/devel-api/actors/camera-actor-devel.h>
23 #include <dali/internal/update/common/animatable-property.h>
24 #include <dali/internal/update/common/double-buffered.h>
25 #include <dali/internal/update/common/inherited-property.h>
26 #include <dali/internal/update/nodes/node.h>
27 #include <dali/public-api/actors/camera-actor.h>
28 #include <dali/public-api/math/rect.h>
29
30 namespace Dali
31 {
32 namespace Internal
33 {
34 namespace SceneGraph
35 {
36 class SceneController;
37
38 /**
39  * Scene-graph camera object
40  */
41 class Camera : public Node
42 {
43 public:
44   static const Dali::Camera::Type                          DEFAULT_TYPE;
45   static const Dali::Camera::ProjectionMode                DEFAULT_MODE;
46   static const Dali::DevelCameraActor::ProjectionDirection DEFAULT_PROJECTION_DIRECTION;
47   static const bool                                        DEFAULT_INVERT_Y_AXIS;
48   static const float                                       DEFAULT_FIELD_OF_VIEW;
49   static const float                                       DEFAULT_ORTHOGRAPHIC_SIZE;
50   static const float                                       DEFAULT_ASPECT_RATIO;
51   static const float                                       DEFAULT_LEFT_CLIPPING_PLANE;
52   static const float                                       DEFAULT_RIGHT_CLIPPING_PLANE;
53   static const float                                       DEFAULT_TOP_CLIPPING_PLANE;
54   static const float                                       DEFAULT_BOTTOM_CLIPPING_PLANE;
55   static const float                                       DEFAULT_NEAR_CLIPPING_PLANE;
56   static const float                                       DEFAULT_FAR_CLIPPING_PLANE;
57   static const Vector2                                     DEFAULT_STEREO_BIAS;
58   static const Vector3                                     DEFAULT_TARGET_POSITION;
59
60   /**
61    * Plane equation container for a plane of the view frustum
62    */
63   struct Plane
64   {
65     Vector3 mNormal;
66     float   mDistance;
67   };
68
69   /**
70    * @brief Container for six planes in a view frustum
71    */
72   struct FrustumPlanes
73   {
74     Plane   mPlanes[6];
75     Vector3 mSign[6];
76   };
77
78   /**
79    * Construct a new Camera.
80    * @return a new camera.
81    */
82   static Camera* New();
83
84   /**
85    * Virtual destructor
86    */
87   ~Camera() override;
88
89   /**
90    * Overriden delete operator
91    * Deletes the camera from its global memory pool
92    */
93   void operator delete(void* ptr);
94
95   /**
96    * @copydoc Dali::Internal::CameraActor::SetType
97    */
98   void SetType(Dali::Camera::Type type);
99
100   /**
101    * @copydoc Dali::Internal::CameraActor::SetInvertYAxis
102    */
103   void SetInvertYAxis(bool invertYAxis);
104
105   /**
106    * Returns whether the Y axis is inverted.
107    * @return True if the Y axis is inverted, false otherwise.
108    */
109   bool IsYAxisInverted() const
110   {
111     return mInvertYAxis;
112   }
113
114   /**
115    * @copydoc Dali::Internal::CameraActor::SetProjectionMode
116    */
117   void SetProjectionMode(Dali::Camera::ProjectionMode projectionMode);
118
119   /**
120    * @copydoc Dali::Internal::CameraActor::SetProjectionDirection
121    */
122   void SetProjectionDirection(Dali::DevelCameraActor::ProjectionDirection direction);
123
124   /**
125    * @copydoc Dali::Internal::CameraActor::SetLeftClippingPlane
126    */
127   void SetLeftClippingPlane(float leftClippingPlane);
128
129   /**
130    * @copydoc Dali::Internal::CameraActor::SetRightClippingPlane
131    */
132   void SetRightClippingPlane(float rightClippingPlane);
133
134   /**
135    * @copydoc Dali::Internal::CameraActor::SetTopClippingPlane
136    */
137   void SetTopClippingPlane(float topClippingPlane);
138
139   /**
140    * @copydoc Dali::Internal::CameraActor::SetBottomClippingPlane
141    */
142   void SetBottomClippingPlane(float bottomClippingPlane);
143
144   /**
145    * @copydoc Dali::Internal::CameraActor::SetNearClippingPlane
146    */
147   void SetNearClippingPlane(float nearClippingPlane);
148
149   /**
150    * @copydoc Dali::Internal::CameraActor::SetFarClippingPlane
151    */
152   void SetFarClippingPlane(float farClippingPlane);
153
154   /**
155    * @copydoc Dali::Internal::CameraActor::RotateProjection
156    */
157   void RotateProjection(int rotationAngle);
158
159   /**
160    * @copydoc Dali::Internal::CameraActor::SetTarget
161    */
162   void SetTargetPosition(const Vector3& targetPosition);
163
164   /**
165    * @brief Bakes the field of view.
166    * @param[in] updateBufferIndex The current update buffer index.
167    * @param[in] fieldOfView The field of view.
168    */
169   void BakeFieldOfView(BufferIndex updateBufferIndex, float fieldOfView);
170
171   /**
172    * @brief Retrieve the field of view.
173    * @param[in] bufferIndex The buffer to read from.
174    * @return The field of view.
175    */
176   float GetFieldOfView(BufferIndex bufferIndex) const
177   {
178     return mFieldOfView[bufferIndex];
179   }
180
181   /**
182    * @brief Bakes the orthographic size.
183    * @param[in] updateBufferIndex The current update buffer index.
184    * @param[in] orthographicSize The orthographic size.
185    */
186   void BakeOrthographicSize(BufferIndex updateBufferIndex, float orthographicSize);
187
188   /**
189    * @brief Retrieve the orthographic size.
190    * @param[in] bufferIndex The buffer to read from.
191    * @return The orthographic size.
192    */
193   float GetOrthographicSize(BufferIndex bufferIndex) const
194   {
195     return mOrthographicSize[bufferIndex];
196   }
197
198   /**
199    * @brief Bakes the aspect ratio.
200    * @param[in] updateBufferIndex The current update buffer index.
201    * @param[in] aspectRatio The aspect ratio.
202    */
203   void BakeAspectRatio(BufferIndex updateBufferIndex, float aspectRatio);
204
205   /**
206    * @brief Retrieve the aspect ratio.
207    * @param[in] bufferIndex The buffer to read from.
208    * @return The aspect ratio.
209    */
210   float GetAspectRatio(BufferIndex bufferIndex) const
211   {
212     return mAspectRatio[bufferIndex];
213   }
214
215   /**
216    * Sets the reflection plane
217    * @param[in] plane reflection plane
218    */
219   void SetReflectByPlane(const Vector4& plane);
220
221   /**
222    * Tests whether reflection is used
223    * @return True if used, False otherwise
224    */
225   bool GetReflectionUsed() const
226   {
227     return mUseReflection;
228   }
229
230   /**
231    * Retrieve the view-matrix; this is double buffered for input handling.
232    * @param[in] bufferIndex The buffer to read from.
233    * @return The view-matrix.
234    */
235   const Matrix& GetViewMatrix(BufferIndex bufferIndex) const;
236
237   /**
238    * @brief Check to see if a sphere lies within the view frustum.
239    *
240    * @param bufferIndex The buffer to read from.
241    * @param origin The world position center of the sphere to check.
242    * @param radius The length of the sphere radius in world scale.
243    *
244    * @return false if the sphere lies outside of the frustum.
245    */
246   bool CheckSphereInFrustum(BufferIndex bufferIndex, const Vector3& origin, float radius) const;
247
248   /**
249    * @brief Check to see if a bounding box lies within the view frustum.
250    *
251    * @param bufferIndex The buffer to read from.
252    * @param origin the world position center of the cubeoid to check.
253    * @param halfExtents The half length of the cubeoid in world co-ordinates in each axis.
254    *
255    * @return false if the cubeoid lies completely outside of the frustum, true otherwise
256    */
257   bool CheckAABBInFrustum(BufferIndex bufferIndex, const Vector3& origin, const Vector3& halfExtents) const;
258
259   /**
260    * @brief Calculate orthographic clipping box by this camera's orthographic size.
261    */
262   Dali::Rect<int32_t> GetOrthographicClippingBox(BufferIndex bufferIndex) const;
263
264   /**
265    * Retrieve the projection-matrix; this is double buffered for input handling.
266    * @param[in] bufferIndex The buffer to read from.
267    * @return The projection-matrix.
268    */
269   const Matrix& GetProjectionMatrix(BufferIndex bufferIndex) const;
270
271   /**
272    * Retrieve the inverted view-projection-matrix; this is double buffered for input handling.
273    * @param[in] bufferIndex The buffer to read from.
274    * @return The inverse view-projection-matrix.
275    */
276   const Matrix& GetInverseViewProjectionMatrix(BufferIndex bufferIndex) const;
277
278   /**
279    * Retrieve the final projection-matrix; this is double buffered for input handling.
280    * @param[in] bufferIndex The buffer to read from.
281    * @return The projection-matrix that should be used to render.
282    */
283   const Matrix& GetFinalProjectionMatrix(BufferIndex bufferIndex) const;
284
285   /**
286    * Retrieve the field of view property querying interface.
287    * @pre The camera is on-stage.
288    * @return The field of view property querying interface.
289    */
290   const PropertyBase* GetFieldOfView() const;
291
292   /**
293    * Retrieve the orthographic size property querying interface.
294    * @pre The camera is on-stage.
295    * @return The orthographic size property querying interface.
296    */
297   const PropertyBase* GetOrthographicSize() const;
298
299   /**
300    * Retrieve the aspect ratio property querying interface.
301    * @pre The camera is on-stage.
302    * @return The aspect ratio property querying interface.
303    */
304   const PropertyBase* GetAspectRatio() const;
305
306   /**
307    * Retrieve the projection-matrix property querying interface.
308    * @pre The camera is on-stage.
309    * @return The projection-matrix property querying interface.
310    */
311   const PropertyInputImpl* GetProjectionMatrix() const;
312
313   /**
314    * Retrieve the viewMatrix property querying interface.
315    * @pre The camera is on-stage.
316    * @return The viewMatrix property querying interface.
317    */
318   const PropertyInputImpl* GetViewMatrix() const;
319
320   /**
321    * Updates view and projection matrices.
322    * Called by the render task using the camera
323    * @param[in] updateBufferIndex The buffer to read from.
324    */
325   void Update(BufferIndex updateBufferIndex);
326
327   /**
328    * @return true if the view matrix of camera is updated this or the previous frame
329    */
330   bool ViewMatrixUpdated() const;
331
332   /**
333    * @return true if the projection matrix projection matrix relative properties are animated this or the previous frame
334    */
335   bool IsProjectionMatrixAnimated() const;
336
337 private:
338   /**
339    * Constructor
340    */
341   Camera();
342
343   // Delete copy and move
344   Camera(const Camera&) = delete;
345   Camera(Camera&&)      = delete;
346   Camera& operator=(const Camera& rhs) = delete;
347   Camera& operator=(Camera&& rhs) = delete;
348
349   /**
350    * Recalculates the view matrix.
351    * @param[in] bufferIndex The current update buffer index.
352    * @return count how many frames ago the matrix was changed.
353    */
354   uint32_t UpdateViewMatrix(BufferIndex updateBufferIndex);
355
356   /**
357    * Recalculates the projection matrix.
358    * @param[in] bufferIndex The current update buffer index.
359    * @return count how many frames ago the matrix was changed.
360    */
361   uint32_t UpdateProjection(BufferIndex updateBufferIndex);
362
363 private:
364   /**
365    * @brief Extracts the frustum planes.
366    *
367    * @param[in] bufferIndex The current update buffer index.
368    * @param[in] normalize will normalize plane equation coefficients by default.
369    */
370   void UpdateFrustum(BufferIndex updateBufferIndex, bool normalize = true);
371
372   uint32_t mUpdateViewFlag;       ///< This is non-zero if the view matrix requires an update
373   uint32_t mUpdateProjectionFlag; ///< This is non-zero if the projection matrix requires an update
374   int      mProjectionRotation;   ///< The rotaion angle of the projection
375
376 public:                                                             // PROPERTIES
377   Dali::Camera::Type                          mType;                // Non-animatable
378   Dali::Camera::ProjectionMode                mProjectionMode;      // Non-animatable
379   Dali::DevelCameraActor::ProjectionDirection mProjectionDirection; // Non-animatable
380   bool                                        mInvertYAxis;         // Non-animatable
381
382   AnimatableProperty<float> mFieldOfView;      // Animatable
383   AnimatableProperty<float> mOrthographicSize; // Animatable
384   AnimatableProperty<float> mAspectRatio;      // Animatable
385
386   float   mNearClippingPlane;
387   float   mFarClippingPlane;
388   Vector3 mTargetPosition;
389
390   Dali::Matrix  mReflectionMtx;
391   Dali::Vector4 mReflectionPlane;
392   Dali::Vector4 mReflectionEye;
393   bool          mUseReflection{false};
394   bool          mUseReflectionClip{false};
395
396   InheritedMatrix mViewMatrix;       ///< The viewMatrix; this is double buffered for input handling.
397   InheritedMatrix mProjectionMatrix; ///< The projectionMatrix; this is double buffered for input handling.
398
399   DoubleBuffered<FrustumPlanes> mFrustum;               ///< Clipping frustum; double buffered for input handling
400   DoubleBuffered<Matrix>        mInverseViewProjection; ///< Inverted viewprojection; double buffered for input handling
401   DoubleBuffered<Matrix>        mFinalProjection;       ///< Final projection matrix; double buffered for input handling
402 };
403
404 } // namespace SceneGraph
405 } // namespace Internal
406 } // namespace Dali
407
408 #endif // DALI_INTERNAL_SCENE_GRAPH_CAMERA_H