Revert "License conversion from Flora to Apache 2.0"
[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 Flora License, Version 1.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://floralicense.org/license/
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 // INTERNAL INCLUDES
21 #include <dali/public-api/math/rect.h>
22 #include <dali/public-api/actors/camera-actor.h>
23 #include <dali/internal/common/message.h>
24 #include <dali/internal/common/event-to-update.h>
25 #include <dali/internal/update/common/double-buffered.h>
26 #include <dali/internal/update/node-attachments/node-attachment.h>
27 #include <dali/internal/update/common/animatable-property.h>
28 #include <dali/internal/update/common/inherited-property.h>
29
30 namespace Dali
31 {
32
33 namespace Internal
34 {
35
36 // value types used by messages
37 template <> struct ParameterType< Dali::Camera::Type >
38 : public BasicType< Dali::Camera::Type > {};
39 template <> struct ParameterType< Dali::Camera::ProjectionMode >
40 : public BasicType< Dali::Camera::ProjectionMode > {};
41
42 namespace SceneGraph
43 {
44
45 class RenderableAttachment;
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_STEREO_BIAS;
60   static const float DEFAULT_LEFT_CLIPPING_PLANE;
61   static const float DEFAULT_RIGHT_CLIPPING_PLANE;
62   static const float DEFAULT_TOP_CLIPPING_PLANE;
63   static const float DEFAULT_BOTTOM_CLIPPING_PLANE;
64   static const float DEFAULT_NEAR_CLIPPING_PLANE;
65   static const float DEFAULT_FAR_CLIPPING_PLANE;
66   static const Vector3 DEFAULT_TARGET_POSITION;
67
68   /**
69    * Construct a new CameraAttachment.
70    * @return a new camera.
71    */
72   static CameraAttachment* New();
73
74   /**
75    * @copydoc NodeAttachment::ConnectToSceneGraph().
76    */
77   virtual void ConnectToSceneGraph( SceneController& sceneController, BufferIndex updateBufferIndex );
78
79   /**
80    * @copydoc NodeAttachment::OnDestroy().
81    */
82   virtual void OnDestroy();
83
84   /**
85    * Virtual destructor
86    */
87   virtual ~CameraAttachment();
88
89   /**
90    * @copydoc NodeAttachment::GetRenderable.
91    * @return Always NULL.
92    */
93   virtual RenderableAttachment* GetRenderable();
94
95   /**
96    * @copydoc Dali::Internal::CameraAttachment::SetType
97    */
98   void SetType( Dali::Camera::Type type );
99
100   /**
101    * @copydoc Dali::Internal::CameraAttachment::SetInvertYAxis
102    */
103   void SetInvertYAxis( bool invertYAxis );
104
105   /**
106    * @copydoc Dali::Internal::CameraAttachment::SetProjectionMode
107    */
108   void SetProjectionMode( Dali::Camera::ProjectionMode projectionMode );
109
110   /**
111    * @copydoc Dali::Internal::CameraAttachment::SetFieldOfView
112    */
113   void SetFieldOfView( float fieldOfView );
114
115   /**
116    * @copydoc Dali::Internal::CameraAttachment::SetAspectRatio
117    */
118   void SetAspectRatio( float aspectRatio );
119
120   /**
121    * @copydoc Dali::Internal::CameraAttachment::SetStereoBias
122    */
123   void SetStereoBias(float stereoBias);
124
125    /**
126    * @copydoc Dali::Internal::CameraAttachment::SetLeftClippingPlane
127    */
128   void SetLeftClippingPlane( float leftClippingPlane );
129
130   /**
131    * @copydoc Dali::Internal::CameraAttachment::SetRightClippingPlane
132    */
133   void SetRightClippingPlane( float rightClippingPlane );
134
135   /**
136    * @copydoc Dali::Internal::CameraAttachment::SetTopClippingPlane
137    */
138   void SetTopClippingPlane( float topClippingPlane );
139
140   /**
141    * @copydoc Dali::Internal::CameraAttachment::SetBottomClippingPlane
142    */
143   void SetBottomClippingPlane( float bottomClippingPlane );
144
145   /**
146    * @copydoc Dali::Internal::CameraAttachment::SetNearClippingPlane
147    */
148   void SetNearClippingPlane( float nearClippingPlane );
149
150   /**
151    * @copydoc Dali::Internal::CameraAttachment::SetFarClippingPlane
152    */
153   void SetFarClippingPlane( float farClippingPlane );
154
155   /**
156    * @copydoc Dali::Internal::CameraAttachment::SetTarget
157    */
158   void SetTargetPosition( const Vector3& targetPosition );
159
160   /**
161    * Get Camera Target
162    */
163   Vector3 GetTargetPosition() const;
164
165   /**
166    * Retrieve the view-matrix; this is double buffered for input handling.
167    * @param[in] bufferIndex The buffer to read from.
168    * @return The view-matrix.
169    */
170   const Matrix& GetViewMatrix( BufferIndex bufferIndex ) const;
171
172   /**
173    * Retrieve the projection-matrix; this is double buffered for input handling.
174    * @param[in] bufferIndex The buffer to read from.
175    * @return The projection-matrix.
176    */
177   const Matrix& GetProjectionMatrix( BufferIndex bufferIndex ) const;
178
179   /**
180    * Retrieve the inverted view-projection-matrix; this is double buffered for input handling.
181    * @param[in] bufferIndex The buffer to read from.
182    * @return The inverse view-projection-matrix.
183    */
184   const Matrix& GetInverseViewProjectionMatrix( BufferIndex bufferIndex ) const;
185
186   /**
187    * Retrieve the projection-matrix property querying interface.
188    * @pre The attachment is on-stage.
189    * @return The projection-matrix property querying interface.
190    */
191   const PropertyInputImpl* GetProjectionMatrix() const;
192
193   /**
194    * Retrieve the view-matrix property querying interface.
195    * @pre The attachment is on-stage.
196    * @return The view-matrix property querying interface.
197    */
198   const PropertyInputImpl* GetViewMatrix() const;
199
200   /**
201    * @copydoc NodeAttachment::Update
202    */
203   virtual void Update( BufferIndex updateBufferIndex, const Node& owningNode, int nodeDirtyFlags );
204
205   /**
206    * @return true if the view matrix of camera is updated this or the previous frame
207    */
208   bool ViewMatrixUpdated();
209
210 protected:
211
212   /**
213    * Protected constructor, see New().
214    */
215   CameraAttachment();
216
217 private:
218
219   // Undefined
220   CameraAttachment(const CameraAttachment&);
221
222   // Undefined
223   CameraAttachment& operator=(const CameraAttachment& rhs);
224
225   /**
226    * Recalculates the view & projection matrices.
227    * @param[in] bufferIndex The current update buffer index.
228    */
229   void UpdateProjection( BufferIndex updateBufferIndex );
230
231   /**
232    * Recalculates the inverse modelview matrix, which is used for hit testing
233    * @param[in] updateBufferIndex The current update buffer index.
234    */
235   void UpdateInverseViewProjection( BufferIndex updateBufferIndex );
236
237 private:
238   unsigned int                  mUpdateViewFlag;       ///< This is non-zero if the view matrix requires an update
239   unsigned int                  mUpdateProjectionFlag; ///< This is non-zero if the projection matrix requires an update
240
241 public:  // PROPERTIES
242   Dali::Camera::Type            mType;                 // Non-animatable
243   Dali::Camera::ProjectionMode  mProjectionMode;       // Non-animatable
244   bool                          mInvertYAxis;          // Non-animatable
245
246   float                         mFieldOfView;
247   float                         mAspectRatio;
248   float                         mStereoBias;
249   float                         mLeftClippingPlane;
250   float                         mRightClippingPlane;
251   float                         mTopClippingPlane;
252   float                         mBottomClippingPlane;
253   float                         mNearClippingPlane;
254   float                         mFarClippingPlane;
255   Vector3                       mTargetPosition;
256
257   InheritedProperty<Matrix>     mViewMatrix;           ///< The view-matrix; this is double buffered for input handling.
258   InheritedProperty<Matrix>     mProjectionMatrix;     ///< The projection-matrix; this is double buffered for input handling.
259
260   DoubleBuffered< Matrix >      mInverseViewProjection;///< Inverted viewprojection; double buffered for input handling
261
262 };
263
264 // Messages for CameraAttachment
265
266 inline void SetTypeMessage( EventToUpdate& eventToUpdate, const CameraAttachment& attachment, Dali::Camera::Type parameter )
267 {
268   typedef MessageValue1< CameraAttachment, Dali::Camera::Type > LocalType;
269
270   // Reserve some memory inside the message queue
271   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
272
273   // Construct message in the message queue memory; note that delete should not be called on the return value
274   new (slot) LocalType( &attachment, &CameraAttachment::SetType, parameter );
275 }
276
277 inline void SetProjectionModeMessage( EventToUpdate& eventToUpdate, const CameraAttachment& attachment, Dali::Camera::ProjectionMode parameter )
278 {
279   typedef MessageValue1< CameraAttachment, Dali::Camera::ProjectionMode > LocalProjectionMode;
280
281   // Reserve some memory inside the message queue
282   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalProjectionMode ) );
283
284   // Construct message in the message queue memory; note that delete should not be called on the return value
285   new (slot) LocalProjectionMode( &attachment, &CameraAttachment::SetProjectionMode, parameter );
286 }
287
288
289 inline void SetFieldOfViewMessage( EventToUpdate& eventToUpdate, const CameraAttachment& attachment, float parameter )
290 {
291   typedef MessageValue1< CameraAttachment, float > LocalType;
292
293   // Reserve some memory inside the message queue
294   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
295
296   // Construct message in the message queue memory; note that delete should not be called on the return value
297   new (slot) LocalType( &attachment, &CameraAttachment::SetFieldOfView, parameter );
298 }
299
300 inline void SetAspectRatioMessage( EventToUpdate& eventToUpdate, const CameraAttachment& attachment, float parameter )
301 {
302   typedef MessageValue1< CameraAttachment, float > LocalType;
303
304   // Reserve some memory inside the message queue
305   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
306
307   // Construct message in the message queue memory; note that delete should not be called on the return value
308   new (slot) LocalType( &attachment, &CameraAttachment::SetAspectRatio, parameter );
309 }
310
311 inline void SetStereoBiasMessage( EventToUpdate& eventToUpdate, const CameraAttachment& attachment, float parameter )
312 {
313   typedef MessageValue1< CameraAttachment, float > LocalType;
314
315   // Reserve some memory inside the message queue
316   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
317
318   // Construct message in the message queue memory; note that delete should not be called on the return value
319   new (slot) LocalType( &attachment, &CameraAttachment::SetStereoBias, parameter );
320 }
321
322 inline void SetLeftClippingPlaneMessage( EventToUpdate& eventToUpdate, const CameraAttachment& attachment, float parameter )
323 {
324   typedef MessageValue1< CameraAttachment, float > LocalType;
325
326   // Reserve some memory inside the message queue
327   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
328
329   // Construct message in the message queue memory; note that delete should not be called on the return value
330   new (slot) LocalType( &attachment, &CameraAttachment::SetLeftClippingPlane, parameter );
331 }
332
333 inline void SetRightClippingPlaneMessage( EventToUpdate& eventToUpdate, const CameraAttachment& attachment, float parameter )
334 {
335   typedef MessageValue1< CameraAttachment, float > LocalType;
336
337   // Reserve some memory inside the message queue
338   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
339
340   // Construct message in the message queue memory; note that delete should not be called on the return value
341   new (slot) LocalType( &attachment, &CameraAttachment::SetRightClippingPlane, parameter );
342 }
343
344 inline void SetTopClippingPlaneMessage( EventToUpdate& eventToUpdate, const CameraAttachment& attachment, float parameter )
345 {
346   typedef MessageValue1< CameraAttachment, float > LocalType;
347
348   // Reserve some memory inside the message queue
349   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
350
351   // Construct message in the message queue memory; note that delete should not be called on the return value
352   new (slot) LocalType( &attachment, &CameraAttachment::SetTopClippingPlane, parameter );
353 }
354
355 inline void SetBottomClippingPlaneMessage( EventToUpdate& eventToUpdate, const CameraAttachment& attachment, float parameter )
356 {
357   typedef MessageValue1< CameraAttachment, float > LocalType;
358
359   // Reserve some memory inside the message queue
360   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
361
362   // Construct message in the message queue memory; note that delete should not be called on the return value
363   new (slot) LocalType( &attachment, &CameraAttachment::SetBottomClippingPlane, parameter );
364 }
365
366 inline void SetNearClippingPlaneMessage( EventToUpdate& eventToUpdate, const CameraAttachment& attachment, float parameter )
367 {
368   typedef MessageValue1< CameraAttachment, float > LocalType;
369
370   // Reserve some memory inside the message queue
371   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
372
373   // Construct message in the message queue memory; note that delete should not be called on the return value
374   new (slot) LocalType( &attachment, &CameraAttachment::SetNearClippingPlane, parameter );
375 }
376
377 inline void SetFarClippingPlaneMessage( EventToUpdate& eventToUpdate, const CameraAttachment& attachment, float parameter )
378 {
379   typedef MessageValue1< CameraAttachment, float > LocalType;
380
381   // Reserve some memory inside the message queue
382   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
383
384   // Construct message in the message queue memory; note that delete should not be called on the return value
385   new (slot) LocalType( &attachment, &CameraAttachment::SetFarClippingPlane, parameter );
386 }
387
388 inline void SetTargetPositionMessage( EventToUpdate& eventToUpdate, const CameraAttachment& attachment, const Vector3& parameter )
389 {
390   typedef MessageValue1< CameraAttachment, Vector3 > LocalType;
391
392   // Reserve some memory inside the message queue
393   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
394
395   // Construct message in the message queue memory; note that delete should not be called on the return value
396   new (slot) LocalType( &attachment, &CameraAttachment::SetTargetPosition, parameter );
397 }
398
399
400 inline void SetInvertYAxisMessage( EventToUpdate& eventToUpdate, const CameraAttachment& attachment, bool parameter )
401 {
402   typedef MessageValue1< CameraAttachment, bool > LocalType;
403
404   // Reserve some memory inside the message queue
405   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
406
407   // Construct message in the message queue memory; note that delete should not be called on the return value
408   new (slot) LocalType( &attachment, &CameraAttachment::SetInvertYAxis, parameter );
409 }
410
411
412 } // namespace SceneGraph
413
414 } // namespace Internal
415
416 } // namespace Dali
417
418 #endif // __DALI_INTERNAL_SCENE_GRAPH_CAMERA_ATTACHMENT_H__