Removal of the Scene graph node attachment
[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) 2016 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    * @copydoc Dali::Internal::CameraActor::SetType
98    */
99   void SetType( Dali::Camera::Type type );
100
101   /**
102    * @copydoc Dali::Internal::CameraActor::SetInvertYAxis
103    */
104   void SetInvertYAxis( bool invertYAxis );
105
106   /**
107    * @copydoc Dali::Internal::CameraActor::SetProjectionMode
108    */
109   void SetProjectionMode( Dali::Camera::ProjectionMode projectionMode );
110
111   /**
112    * @copydoc Dali::Internal::CameraActor::SetFieldOfView
113    */
114   void SetFieldOfView( float fieldOfView );
115
116   /**
117    * @copydoc Dali::Internal::CameraActor::SetAspectRatio
118    */
119   void SetAspectRatio( float aspectRatio );
120
121   /**
122    * @copydoc Dali::Internal::CameraActor::SetStereoBias
123    */
124   void SetStereoBias(const Vector2& stereoBias);
125
126    /**
127    * @copydoc Dali::Internal::CameraActor::SetLeftClippingPlane
128    */
129   void SetLeftClippingPlane( float leftClippingPlane );
130
131   /**
132    * @copydoc Dali::Internal::CameraActor::SetRightClippingPlane
133    */
134   void SetRightClippingPlane( float rightClippingPlane );
135
136   /**
137    * @copydoc Dali::Internal::CameraActor::SetTopClippingPlane
138    */
139   void SetTopClippingPlane( float topClippingPlane );
140
141   /**
142    * @copydoc Dali::Internal::CameraActor::SetBottomClippingPlane
143    */
144   void SetBottomClippingPlane( float bottomClippingPlane );
145
146   /**
147    * @copydoc Dali::Internal::CameraActor::SetNearClippingPlane
148    */
149   void SetNearClippingPlane( float nearClippingPlane );
150
151   /**
152    * @copydoc Dali::Internal::CameraActor::SetFarClippingPlane
153    */
154   void SetFarClippingPlane( float farClippingPlane );
155
156   /**
157    * @copydoc Dali::Internal::CameraActor::SetTarget
158    */
159   void SetTargetPosition( const Vector3& targetPosition );
160
161   /**
162    * Retrieve the view-matrix; this is double buffered for input handling.
163    * @param[in] bufferIndex The buffer to read from.
164    * @return The view-matrix.
165    */
166   const Matrix& GetViewMatrix( BufferIndex bufferIndex ) const;
167
168   /**
169    * @brief Check to see if a sphere lies within the view frustum.
170    *
171    * @param bufferIndex The buffer to read from.
172    * @param origin The world position center of the sphere to check.
173    * @param radius The length of the sphere radius in world scale.
174    *
175    * @return false if the sphere lies outside of the frustum.
176    */
177   bool CheckSphereInFrustum( BufferIndex bufferIndex, const Vector3& origin, float radius );
178
179   /**
180    * @brief Check to see if a bounding box lies within the view frustum.
181    *
182    * @param bufferIndex The buffer to read from.
183    * @param origin the world position center of the cubeoid to check.
184    * @param halfExtents The half length of the cubeoid in world co-ordinates in each axis.
185    *
186    * @return false if the cubeoid lies completely outside of the frustum, true otherwise
187    */
188   bool CheckAABBInFrustum( BufferIndex bufferIndex, const Vector3& origin, const Vector3& halfExtents );
189
190   /**
191    * Retrieve the projection-matrix; this is double buffered for input handling.
192    * @param[in] bufferIndex The buffer to read from.
193    * @return The projection-matrix.
194    */
195   const Matrix& GetProjectionMatrix( BufferIndex bufferIndex ) const;
196
197   /**
198    * Retrieve the inverted view-projection-matrix; this is double buffered for input handling.
199    * @param[in] bufferIndex The buffer to read from.
200    * @return The inverse view-projection-matrix.
201    */
202   const Matrix& GetInverseViewProjectionMatrix( BufferIndex bufferIndex ) const;
203
204   /**
205    * Retrieve the projection-matrix property querying interface.
206    * @pre The camera is on-stage.
207    * @return The projection-matrix property querying interface.
208    */
209   const PropertyInputImpl* GetProjectionMatrix() const;
210
211   /**
212    * Retrieve the viewMatrix property querying interface.
213    * @pre The camera is on-stage.
214    * @return The viewMatrix property querying interface.
215    */
216   const PropertyInputImpl* GetViewMatrix() const;
217
218   /**
219    * Updates view and projection matrices.
220    * Called by the render task using the camera
221    * @param[in] updateBufferIndex The buffer to read from.
222    * @param[in] owningNode The node that owns the camera
223    */
224   void Update( BufferIndex updateBufferIndex, const Node& owningNode );
225
226   /**
227    * @return true if the view matrix of camera is updated this or the previous frame
228    */
229   bool ViewMatrixUpdated();
230
231 private:
232
233   /**
234    * Constructor
235    */
236   Camera();
237
238   // Non copyable
239   // Undefined
240   Camera(const Camera&);
241   // Undefined
242   Camera& operator=(const Camera& rhs);
243
244   /**
245    * Recalculates the view matrix.
246    * @param[in] bufferIndex The current update buffer index.
247    * @param[in] owningNode to use for calculations.
248    * @return count how many frames ago the matrix was changed.
249    */
250   unsigned int UpdateViewMatrix( BufferIndex updateBufferIndex, const Node& owningNode );
251
252   /**
253    * Recalculates the projection matrix.
254    * @param[in] bufferIndex The current update buffer index.
255    * @return count how many frames ago the matrix was changed.
256    */
257   unsigned int UpdateProjection( BufferIndex updateBufferIndex );
258
259 private:
260
261   /**
262    * @brief Extracts the frustum planes.
263    *
264    * @param[in] bufferIndex The current update buffer index.
265    * @param[in] normalize will normalize plane equation coefficients by default.
266    */
267   void UpdateFrustum( BufferIndex updateBufferIndex, bool normalize = true );
268
269   unsigned int                  mUpdateViewFlag;       ///< This is non-zero if the view matrix requires an update
270   unsigned int                  mUpdateProjectionFlag; ///< This is non-zero if the projection matrix requires an update
271
272 public:  // PROPERTIES
273   Dali::Camera::Type            mType;                 // Non-animatable
274   Dali::Camera::ProjectionMode  mProjectionMode;       // Non-animatable
275   bool                          mInvertYAxis;          // Non-animatable
276
277   float                         mFieldOfView;
278   float                         mAspectRatio;
279   float                         mLeftClippingPlane;
280   float                         mRightClippingPlane;
281   float                         mTopClippingPlane;
282   float                         mBottomClippingPlane;
283   float                         mNearClippingPlane;
284   float                         mFarClippingPlane;
285   Vector2                       mStereoBias;
286   Vector3                       mTargetPosition;
287
288   InheritedMatrix mViewMatrix;           ///< The viewMatrix; this is double buffered for input handling.
289   InheritedMatrix mProjectionMatrix;     ///< The projectionMatrix; this is double buffered for input handling.
290
291   DoubleBuffered< FrustumPlanes > mFrustum;               ///< Clipping frustum; double buffered for input handling
292   DoubleBuffered< Matrix >        mInverseViewProjection; ///< Inverted viewprojection; double buffered for input handling
293
294 };
295
296 // Messages for Camera
297
298 inline void SetTypeMessage( EventThreadServices& eventThreadServices, const Camera& camera, Dali::Camera::Type parameter )
299 {
300   typedef MessageValue1< Camera, Dali::Camera::Type > LocalType;
301
302   // Reserve some memory inside the message queue
303   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
304
305   // Construct message in the message queue memory; note that delete should not be called on the return value
306   new (slot) LocalType( &camera, &Camera::SetType, parameter );
307 }
308
309 inline void SetProjectionModeMessage( EventThreadServices& eventThreadServices, const Camera& camera, Dali::Camera::ProjectionMode parameter )
310 {
311   typedef MessageValue1< Camera, Dali::Camera::ProjectionMode > LocalProjectionMode;
312
313   // Reserve some memory inside the message queue
314   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalProjectionMode ) );
315
316   // Construct message in the message queue memory; note that delete should not be called on the return value
317   new (slot) LocalProjectionMode( &camera, &Camera::SetProjectionMode, parameter );
318 }
319
320 inline void SetFieldOfViewMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
321 {
322   typedef MessageValue1< Camera, float > LocalType;
323
324   // Reserve some memory inside the message queue
325   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
326
327   // Construct message in the message queue memory; note that delete should not be called on the return value
328   new (slot) LocalType( &camera, &Camera::SetFieldOfView, parameter );
329 }
330
331 inline void SetAspectRatioMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
332 {
333   typedef MessageValue1< Camera, float > LocalType;
334
335   // Reserve some memory inside the message queue
336   unsigned int* 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::SetAspectRatio, parameter );
340 }
341
342 inline void SetStereoBiasMessage( EventThreadServices& eventThreadServices, const Camera& camera, const Vector2& parameter )
343 {
344   typedef MessageValue1< Camera, Vector2 > LocalType;
345
346   // Reserve some memory inside the message queue
347   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
348
349   // Construct message in the message queue memory; note that delete should not be called on the return value
350   new (slot) LocalType( &camera, &Camera::SetStereoBias, parameter );
351 }
352
353 inline void SetLeftClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
354 {
355   typedef MessageValue1< Camera, float > LocalType;
356
357   // Reserve some memory inside the message queue
358   unsigned int* 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::SetLeftClippingPlane, parameter );
362 }
363
364 inline void SetRightClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
365 {
366   typedef MessageValue1< Camera, float > LocalType;
367
368   // Reserve some memory inside the message queue
369   unsigned int* 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::SetRightClippingPlane, parameter );
373 }
374
375 inline void SetTopClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
376 {
377   typedef MessageValue1< Camera, float > LocalType;
378
379   // Reserve some memory inside the message queue
380   unsigned int* 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::SetTopClippingPlane, parameter );
384 }
385
386 inline void SetBottomClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
387 {
388   typedef MessageValue1< Camera, float > LocalType;
389
390   // Reserve some memory inside the message queue
391   unsigned int* 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::SetBottomClippingPlane, parameter );
395 }
396
397 inline void SetNearClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
398 {
399   typedef MessageValue1< Camera, float > LocalType;
400
401   // Reserve some memory inside the message queue
402   unsigned int* 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::SetNearClippingPlane, parameter );
406 }
407
408 inline void SetFarClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
409 {
410   typedef MessageValue1< Camera, float > LocalType;
411
412   // Reserve some memory inside the message queue
413   unsigned int* 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::SetFarClippingPlane, parameter );
417 }
418
419 inline void SetTargetPositionMessage( EventThreadServices& eventThreadServices, const Camera& camera, const Vector3& parameter )
420 {
421   typedef MessageValue1< Camera, Vector3 > LocalType;
422
423   // Reserve some memory inside the message queue
424   unsigned int* 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::SetTargetPosition, parameter );
428 }
429
430 inline void SetInvertYAxisMessage( EventThreadServices& eventThreadServices, const Camera& camera, bool parameter )
431 {
432   typedef MessageValue1< Camera, bool > LocalType;
433
434   // Reserve some memory inside the message queue
435   unsigned int* 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::SetInvertYAxis, parameter );
439 }
440
441 } // namespace SceneGraph
442
443 } // namespace Internal
444
445 } // namespace Dali
446
447 #endif // __DALI_INTERNAL_SCENE_GRAPH_CAMERA_H__