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