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