Create ProjectionDirection property at CameraActor
[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) 2022 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/common/message.h>
24 #include <dali/internal/common/type-abstraction-enums.h>
25 #include <dali/internal/event/common/event-thread-services.h>
26 #include <dali/internal/update/common/double-buffered.h>
27 #include <dali/internal/update/common/inherited-property.h>
28 #include <dali/public-api/actors/camera-actor.h>
29 #include <dali/public-api/math/rect.h>
30
31 namespace Dali
32 {
33 namespace Internal
34 {
35 // value types used by messages
36 template<>
37 struct ParameterType<Dali::Camera::Type>
38 : public BasicType<Dali::Camera::Type>
39 {
40 };
41 template<>
42 struct ParameterType<Dali::Camera::ProjectionMode>
43 : public BasicType<Dali::Camera::ProjectionMode>
44 {
45 };
46
47 namespace SceneGraph
48 {
49 class Node;
50 class SceneController;
51
52 /**
53  * Scene-graph camera object
54  */
55 class Camera
56 {
57 public:
58   static const Dali::Camera::Type                          DEFAULT_TYPE;
59   static const Dali::Camera::ProjectionMode                DEFAULT_MODE;
60   static const Dali::DevelCameraActor::ProjectionDirection DEFAULT_PROJECTION_DIRECTION;
61   static const bool                                        DEFAULT_INVERT_Y_AXIS;
62   static const float                                       DEFAULT_FIELD_OF_VIEW;
63   static const float                                       DEFAULT_ASPECT_RATIO;
64   static const float                                       DEFAULT_LEFT_CLIPPING_PLANE;
65   static const float                                       DEFAULT_RIGHT_CLIPPING_PLANE;
66   static const float                                       DEFAULT_TOP_CLIPPING_PLANE;
67   static const float                                       DEFAULT_BOTTOM_CLIPPING_PLANE;
68   static const float                                       DEFAULT_NEAR_CLIPPING_PLANE;
69   static const float                                       DEFAULT_FAR_CLIPPING_PLANE;
70   static const Vector2                                     DEFAULT_STEREO_BIAS;
71   static const Vector3                                     DEFAULT_TARGET_POSITION;
72
73   /**
74    * Plane equation container for a plane of the view frustum
75    */
76   struct Plane
77   {
78     Vector3 mNormal;
79     float   mDistance;
80   };
81
82   /**
83    * @brief Container for six planes in a view frustum
84    */
85   struct FrustumPlanes
86   {
87     Plane   mPlanes[6];
88     Vector3 mSign[6];
89   };
90
91   /**
92    * Construct a new Camera.
93    * @return a new camera.
94    */
95   static Camera* New();
96
97   /**
98    * Destructor
99    */
100   ~Camera();
101
102   /**
103    * Set the node this scene graph camera belongs to.
104    * @param[in] node The owning node.
105    */
106   void SetNode(const Node* node);
107
108   /**
109    * Get the node this scene graph camera belongs to.
110    * @return node The owning node.
111    */
112   const Node* GetNode() const;
113
114   /**
115    * @copydoc Dali::Internal::CameraActor::SetType
116    */
117   void SetType(Dali::Camera::Type type);
118
119   /**
120    * @copydoc Dali::Internal::CameraActor::SetInvertYAxis
121    */
122   void SetInvertYAxis(bool invertYAxis);
123
124   /**
125    * Returns whether the Y axis is inverted.
126    * @return True if the Y axis is inverted, false otherwise.
127    */
128   bool IsYAxisInverted() const
129   {
130     return mInvertYAxis;
131   }
132
133   /**
134    * @copydoc Dali::Internal::CameraActor::SetProjectionMode
135    */
136   void SetProjectionMode(Dali::Camera::ProjectionMode projectionMode);
137
138   /**
139    * @copydoc Dali::Internal::CameraActor::SetProjectionDirection
140    */
141   void SetProjectionDirection(Dali::DevelCameraActor::ProjectionDirection direction);
142
143   /**
144    * @copydoc Dali::Internal::CameraActor::SetFieldOfView
145    */
146   void SetFieldOfView(float fieldOfView);
147
148   /**
149    * @copydoc Dali::Internal::CameraActor::SetAspectRatio
150    */
151   void SetAspectRatio(float aspectRatio);
152
153   /**
154    * @copydoc Dali::Internal::CameraActor::SetLeftClippingPlane
155    */
156   void SetLeftClippingPlane(float leftClippingPlane);
157
158   /**
159    * @copydoc Dali::Internal::CameraActor::SetRightClippingPlane
160    */
161   void SetRightClippingPlane(float rightClippingPlane);
162
163   /**
164    * @copydoc Dali::Internal::CameraActor::SetTopClippingPlane
165    */
166   void SetTopClippingPlane(float topClippingPlane);
167
168   /**
169    * @copydoc Dali::Internal::CameraActor::SetBottomClippingPlane
170    */
171   void SetBottomClippingPlane(float bottomClippingPlane);
172
173   /**
174    * @copydoc Dali::Internal::CameraActor::SetNearClippingPlane
175    */
176   void SetNearClippingPlane(float nearClippingPlane);
177
178   /**
179    * @copydoc Dali::Internal::CameraActor::SetFarClippingPlane
180    */
181   void SetFarClippingPlane(float farClippingPlane);
182
183   /**
184    * @copydoc Dali::Internal::CameraActor::RotateProjection
185    */
186   void RotateProjection(int rotationAngle);
187
188   /**
189    * @copydoc Dali::Internal::CameraActor::SetTarget
190    */
191   void SetTargetPosition(const Vector3& targetPosition);
192
193   /**
194    * Sets the reflection plane
195    * @param[in] plane reflection plane
196    */
197   void SetReflectByPlane(const Vector4& plane);
198
199   /**
200    * Tests whether reflection is used
201    * @return True if used, False otherwise
202    */
203   bool GetReflectionUsed() const
204   {
205     return mUseReflection;
206   }
207
208   /**
209    * Retrieve the view-matrix; this is double buffered for input handling.
210    * @param[in] bufferIndex The buffer to read from.
211    * @return The view-matrix.
212    */
213   const Matrix& GetViewMatrix(BufferIndex bufferIndex) const;
214
215   /**
216    * @brief Check to see if a sphere lies within the view frustum.
217    *
218    * @param bufferIndex The buffer to read from.
219    * @param origin The world position center of the sphere to check.
220    * @param radius The length of the sphere radius in world scale.
221    *
222    * @return false if the sphere lies outside of the frustum.
223    */
224   bool CheckSphereInFrustum(BufferIndex bufferIndex, const Vector3& origin, float radius);
225
226   /**
227    * @brief Check to see if a bounding box lies within the view frustum.
228    *
229    * @param bufferIndex The buffer to read from.
230    * @param origin the world position center of the cubeoid to check.
231    * @param halfExtents The half length of the cubeoid in world co-ordinates in each axis.
232    *
233    * @return false if the cubeoid lies completely outside of the frustum, true otherwise
234    */
235   bool CheckAABBInFrustum(BufferIndex bufferIndex, const Vector3& origin, const Vector3& halfExtents);
236
237   /**
238    * Retrieve the projection-matrix; this is double buffered for input handling.
239    * @param[in] bufferIndex The buffer to read from.
240    * @return The projection-matrix.
241    */
242   const Matrix& GetProjectionMatrix(BufferIndex bufferIndex) const;
243
244   /**
245    * Retrieve the inverted view-projection-matrix; this is double buffered for input handling.
246    * @param[in] bufferIndex The buffer to read from.
247    * @return The inverse view-projection-matrix.
248    */
249   const Matrix& GetInverseViewProjectionMatrix(BufferIndex bufferIndex) const;
250
251   /**
252    * Retrieve the final projection-matrix; this is double buffered for input handling.
253    * @param[in] bufferIndex The buffer to read from.
254    * @return The projection-matrix that should be used to render.
255    */
256   const Matrix& GetFinalProjectionMatrix(BufferIndex bufferIndex) const;
257
258   /**
259    * Retrieve the projection-matrix property querying interface.
260    * @pre The camera is on-stage.
261    * @return The projection-matrix property querying interface.
262    */
263   const PropertyInputImpl* GetProjectionMatrix() const;
264
265   /**
266    * Retrieve the viewMatrix property querying interface.
267    * @pre The camera is on-stage.
268    * @return The viewMatrix property querying interface.
269    */
270   const PropertyInputImpl* GetViewMatrix() const;
271
272   /**
273    * Updates view and projection matrices.
274    * Called by the render task using the camera
275    * @param[in] updateBufferIndex The buffer to read from.
276    */
277   void Update(BufferIndex updateBufferIndex);
278
279   /**
280    * @return true if the view matrix of camera is updated this or the previous frame
281    */
282   bool ViewMatrixUpdated();
283
284 private:
285   /**
286    * Constructor
287    */
288   Camera();
289
290   // Non copyable
291   // Undefined
292   Camera(const Camera&);
293   // Undefined
294   Camera& operator=(const Camera& rhs);
295
296   /**
297    * Recalculates the view matrix.
298    * @param[in] bufferIndex The current update buffer index.
299    * @return count how many frames ago the matrix was changed.
300    */
301   uint32_t UpdateViewMatrix(BufferIndex updateBufferIndex);
302
303   /**
304    * Recalculates the projection matrix.
305    * @param[in] bufferIndex The current update buffer index.
306    * @return count how many frames ago the matrix was changed.
307    */
308   uint32_t UpdateProjection(BufferIndex updateBufferIndex);
309
310 private:
311   /**
312    * @brief Extracts the frustum planes.
313    *
314    * @param[in] bufferIndex The current update buffer index.
315    * @param[in] normalize will normalize plane equation coefficients by default.
316    */
317   void UpdateFrustum(BufferIndex updateBufferIndex, bool normalize = true);
318
319   /**
320    * Adjust near plane for reflection
321    * @param perspective Perspective matrix
322    * @param clipPlane Clipping plane
323    */
324   void AdjustNearPlaneForPerspective(Matrix& perspective, const Vector4& clipPlane);
325
326   uint32_t    mUpdateViewFlag;       ///< This is non-zero if the view matrix requires an update
327   uint32_t    mUpdateProjectionFlag; ///< This is non-zero if the projection matrix requires an update
328   int         mProjectionRotation;   ///< The rotaion angle of the projection
329   const Node* mNode;                 ///< The node this scene graph camera belongs to
330
331 public:                                                             // PROPERTIES
332   Dali::Camera::Type                          mType;                // Non-animatable
333   Dali::Camera::ProjectionMode                mProjectionMode;      // Non-animatable
334   Dali::DevelCameraActor::ProjectionDirection mProjectionDirection; // Non-animatable
335   bool                                        mInvertYAxis;         // Non-animatable
336
337   float   mFieldOfView;
338   float   mAspectRatio;
339   float   mLeftClippingPlane;
340   float   mRightClippingPlane;
341   float   mTopClippingPlane;
342   float   mBottomClippingPlane;
343   float   mNearClippingPlane;
344   float   mFarClippingPlane;
345   Vector3 mTargetPosition;
346
347   Dali::Matrix  mReflectionMtx;
348   Dali::Vector4 mReflectionPlane;
349   Dali::Vector4 mReflectionEye;
350   bool          mUseReflection{false};
351   bool          mUseReflectionClip{false};
352
353   InheritedMatrix mViewMatrix;       ///< The viewMatrix; this is double buffered for input handling.
354   InheritedMatrix mProjectionMatrix; ///< The projectionMatrix; this is double buffered for input handling.
355
356   DoubleBuffered<FrustumPlanes> mFrustum;               ///< Clipping frustum; double buffered for input handling
357   DoubleBuffered<Matrix>        mInverseViewProjection; ///< Inverted viewprojection; double buffered for input handling
358   DoubleBuffered<Matrix>        mFinalProjection;       ///< Final projection matrix; double buffered for input handling
359 };
360
361 // Messages for Camera
362
363 inline void SetTypeMessage(EventThreadServices& eventThreadServices, const Camera& camera, Dali::Camera::Type parameter)
364 {
365   using LocalType = MessageValue1<Camera, Dali::Camera::Type>;
366
367   // Reserve some memory inside the message queue
368   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
369
370   // Construct message in the message queue memory; note that delete should not be called on the return value
371   new(slot) LocalType(&camera, &Camera::SetType, parameter);
372 }
373
374 inline void SetProjectionModeMessage(EventThreadServices& eventThreadServices, const Camera& camera, Dali::Camera::ProjectionMode parameter)
375 {
376   using LocalProjectionMode = MessageValue1<Camera, Dali::Camera::ProjectionMode>;
377
378   // Reserve some memory inside the message queue
379   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalProjectionMode));
380
381   // Construct message in the message queue memory; note that delete should not be called on the return value
382   new(slot) LocalProjectionMode(&camera, &Camera::SetProjectionMode, parameter);
383 }
384
385 inline void SetProjectionDirectionMessage(EventThreadServices& eventThreadServices, const Camera& camera, Dali::DevelCameraActor::ProjectionDirection parameter)
386 {
387   using LocalProjectionDirection = MessageValue1<Camera, Dali::DevelCameraActor::ProjectionDirection>;
388
389   // Reserve some memory inside the message queue
390   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalProjectionDirection));
391
392   // Construct message in the message queue memory; note that delete should not be called on the return value
393   new(slot) LocalProjectionDirection(&camera, &Camera::SetProjectionDirection, parameter);
394 }
395
396 inline void SetFieldOfViewMessage(EventThreadServices& eventThreadServices, const Camera& camera, float parameter)
397 {
398   using LocalType = MessageValue1<Camera, float>;
399
400   // Reserve some memory inside the message queue
401   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
402
403   // Construct message in the message queue memory; note that delete should not be called on the return value
404   new(slot) LocalType(&camera, &Camera::SetFieldOfView, parameter);
405 }
406
407 inline void SetAspectRatioMessage(EventThreadServices& eventThreadServices, const Camera& camera, float parameter)
408 {
409   using LocalType = MessageValue1<Camera, float>;
410
411   // Reserve some memory inside the message queue
412   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
413
414   // Construct message in the message queue memory; note that delete should not be called on the return value
415   new(slot) LocalType(&camera, &Camera::SetAspectRatio, parameter);
416 }
417
418 inline void SetLeftClippingPlaneMessage(EventThreadServices& eventThreadServices, const Camera& camera, float parameter)
419 {
420   using LocalType = MessageValue1<Camera, float>;
421
422   // Reserve some memory inside the message queue
423   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
424
425   // Construct message in the message queue memory; note that delete should not be called on the return value
426   new(slot) LocalType(&camera, &Camera::SetLeftClippingPlane, parameter);
427 }
428
429 inline void SetRightClippingPlaneMessage(EventThreadServices& eventThreadServices, const Camera& camera, float parameter)
430 {
431   using LocalType = MessageValue1<Camera, float>;
432
433   // Reserve some memory inside the message queue
434   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
435
436   // Construct message in the message queue memory; note that delete should not be called on the return value
437   new(slot) LocalType(&camera, &Camera::SetRightClippingPlane, parameter);
438 }
439
440 inline void SetTopClippingPlaneMessage(EventThreadServices& eventThreadServices, const Camera& camera, float parameter)
441 {
442   using LocalType = MessageValue1<Camera, float>;
443
444   // Reserve some memory inside the message queue
445   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
446
447   // Construct message in the message queue memory; note that delete should not be called on the return value
448   new(slot) LocalType(&camera, &Camera::SetTopClippingPlane, parameter);
449 }
450
451 inline void SetBottomClippingPlaneMessage(EventThreadServices& eventThreadServices, const Camera& camera, float parameter)
452 {
453   using LocalType = MessageValue1<Camera, float>;
454
455   // Reserve some memory inside the message queue
456   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
457
458   // Construct message in the message queue memory; note that delete should not be called on the return value
459   new(slot) LocalType(&camera, &Camera::SetBottomClippingPlane, parameter);
460 }
461
462 inline void SetNearClippingPlaneMessage(EventThreadServices& eventThreadServices, const Camera& camera, float parameter)
463 {
464   using LocalType = MessageValue1<Camera, float>;
465
466   // Reserve some memory inside the message queue
467   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
468
469   // Construct message in the message queue memory; note that delete should not be called on the return value
470   new(slot) LocalType(&camera, &Camera::SetNearClippingPlane, parameter);
471 }
472
473 inline void SetFarClippingPlaneMessage(EventThreadServices& eventThreadServices, const Camera& camera, float parameter)
474 {
475   using LocalType = MessageValue1<Camera, float>;
476
477   // Reserve some memory inside the message queue
478   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
479
480   // Construct message in the message queue memory; note that delete should not be called on the return value
481   new(slot) LocalType(&camera, &Camera::SetFarClippingPlane, parameter);
482 }
483
484 inline void SetReflectByPlaneMessage(EventThreadServices& eventThreadServices, const Camera& camera, const Vector4& plane)
485 {
486   using LocalType = MessageValue1<Camera, Vector4>;
487
488   // Reserve some memory inside the message queue
489   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
490
491   // Construct message in the message queue memory; note that delete should not be called on the return value
492   new(slot) LocalType(&camera, &Camera::SetReflectByPlane, plane);
493 }
494
495 inline void SetTargetPositionMessage(EventThreadServices& eventThreadServices, const Camera& camera, const Vector3& parameter)
496 {
497   using LocalType = MessageValue1<Camera, Vector3>;
498
499   // Reserve some memory inside the message queue
500   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
501
502   // Construct message in the message queue memory; note that delete should not be called on the return value
503   new(slot) LocalType(&camera, &Camera::SetTargetPosition, parameter);
504 }
505
506 inline void SetInvertYAxisMessage(EventThreadServices& eventThreadServices, const Camera& camera, bool parameter)
507 {
508   using LocalType = MessageValue1<Camera, bool>;
509
510   // Reserve some memory inside the message queue
511   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
512
513   // Construct message in the message queue memory; note that delete should not be called on the return value
514   new(slot) LocalType(&camera, &Camera::SetInvertYAxis, parameter);
515 }
516
517 inline void RotateProjectionMessage(EventThreadServices& eventThreadServices, const Camera& camera, int parameter)
518 {
519   typedef MessageValue1<Camera, int> LocalType;
520
521   // Reserve some memory inside the message queue
522   unsigned int* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
523
524   // Construct message in the message queue memory; note that delete should not be called on the return value
525   new(slot) LocalType(&camera, &Camera::RotateProjection, parameter);
526 }
527
528 } // namespace SceneGraph
529
530 } // namespace Internal
531
532 } // namespace Dali
533
534 #endif // DALI_INTERNAL_SCENE_GRAPH_CAMERA_H