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