1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_CAMERA_ATTACHMENT_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_CAMERA_ATTACHMENT_H__
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
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 > {};
46 class SceneController;
49 * An attachment for camera objects and their properties.
51 class CameraAttachment : public NodeAttachment
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;
69 * Plane equation container for a plane of the view frustum
78 * @brief Container for six planes in a view frustum
87 * Construct a new CameraAttachment.
88 * @return a new camera.
90 static CameraAttachment* New();
93 * @copydoc NodeAttachment::Initialize().
95 virtual void Initialize( SceneController& sceneController, BufferIndex updateBufferIndex );
98 * @copydoc NodeAttachment::OnDestroy().
100 virtual void OnDestroy();
103 * @copydoc NodeAttachment::ConnectedToSceneGraph()
105 virtual void ConnectedToSceneGraph();
108 * @copydoc NodeAttachment::DisconnectedFromSceneGraph()
110 virtual void DisconnectedFromSceneGraph();
115 virtual ~CameraAttachment();
118 * @copydoc Dali::Internal::CameraAttachment::SetType
120 void SetType( Dali::Camera::Type type );
123 * @copydoc Dali::Internal::CameraAttachment::SetInvertYAxis
125 void SetInvertYAxis( bool invertYAxis );
128 * @copydoc Dali::Internal::CameraAttachment::SetProjectionMode
130 void SetProjectionMode( Dali::Camera::ProjectionMode projectionMode );
133 * @copydoc Dali::Internal::CameraAttachment::SetFieldOfView
135 void SetFieldOfView( float fieldOfView );
138 * @copydoc Dali::Internal::CameraAttachment::SetAspectRatio
140 void SetAspectRatio( float aspectRatio );
143 * @copydoc Dali::Internal::CameraAttachment::SetStereoBias
145 void SetStereoBias(const Vector2& stereoBias);
148 * @copydoc Dali::Internal::CameraAttachment::SetLeftClippingPlane
150 void SetLeftClippingPlane( float leftClippingPlane );
153 * @copydoc Dali::Internal::CameraAttachment::SetRightClippingPlane
155 void SetRightClippingPlane( float rightClippingPlane );
158 * @copydoc Dali::Internal::CameraAttachment::SetTopClippingPlane
160 void SetTopClippingPlane( float topClippingPlane );
163 * @copydoc Dali::Internal::CameraAttachment::SetBottomClippingPlane
165 void SetBottomClippingPlane( float bottomClippingPlane );
168 * @copydoc Dali::Internal::CameraAttachment::SetNearClippingPlane
170 void SetNearClippingPlane( float nearClippingPlane );
173 * @copydoc Dali::Internal::CameraAttachment::SetFarClippingPlane
175 void SetFarClippingPlane( float farClippingPlane );
178 * @copydoc Dali::Internal::CameraAttachment::SetTarget
180 void SetTargetPosition( const Vector3& targetPosition );
185 Vector3 GetTargetPosition() const;
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.
192 const Matrix& GetViewMatrix( BufferIndex bufferIndex ) const;
195 * @brief Check to see if a sphere lies within the view frustum.
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.
201 * @return false if the sphere lies outside of the frustum.
203 bool CheckSphereInFrustum( BufferIndex bufferIndex, const Vector3& origin, float radius );
206 * @brief Check to see if a bounding box lies within the view frustum.
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.
212 * @return false if the cubeoid lies completely outside of the frustum, true otherwise
214 bool CheckAABBInFrustum( BufferIndex bufferIndex, const Vector3& origin, const Vector3& halfExtents );
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.
221 const Matrix& GetProjectionMatrix( BufferIndex bufferIndex ) const;
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.
228 const Matrix& GetInverseViewProjectionMatrix( BufferIndex bufferIndex ) const;
231 * Retrieve the projection-matrix property querying interface.
232 * @pre The attachment is on-stage.
233 * @return The projection-matrix property querying interface.
235 const PropertyInputImpl* GetProjectionMatrix() const;
238 * Retrieve the viewMatrix property querying interface.
239 * @pre The attachment is on-stage.
240 * @return The viewMatrix property querying interface.
242 const PropertyInputImpl* GetViewMatrix() const;
245 * @copydoc NodeAttachment::Update
247 virtual void Update( BufferIndex updateBufferIndex, const Node& owningNode, int nodeDirtyFlags );
250 * @return true if the view matrix of camera is updated this or the previous frame
252 bool ViewMatrixUpdated();
257 * Protected constructor, see New().
264 CameraAttachment(const CameraAttachment&);
267 CameraAttachment& operator=(const CameraAttachment& rhs);
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.
275 unsigned int UpdateViewMatrix( BufferIndex updateBufferIndex, const Node& owningNode );
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.
282 unsigned int UpdateProjection( BufferIndex updateBufferIndex );
287 * @brief Extracts the frustum planes.
289 * @param[in] bufferIndex The current update buffer index.
290 * @param[in] normalize will normalize plane equation coefficients by default.
292 void UpdateFrustum( BufferIndex updateBufferIndex, bool normalize = true );
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
298 public: // PROPERTIES
299 Dali::Camera::Type mType; // Non-animatable
300 Dali::Camera::ProjectionMode mProjectionMode; // Non-animatable
301 bool mInvertYAxis; // Non-animatable
305 float mLeftClippingPlane;
306 float mRightClippingPlane;
307 float mTopClippingPlane;
308 float mBottomClippingPlane;
309 float mNearClippingPlane;
310 float mFarClippingPlane;
312 Vector3 mTargetPosition;
314 InheritedMatrix mViewMatrix; ///< The viewMatrix; this is double buffered for input handling.
315 InheritedMatrix mProjectionMatrix; ///< The projectionMatrix; this is double buffered for input handling.
317 DoubleBuffered< FrustumPlanes > mFrustum; ///< Clipping frustum; double buffered for input handling
318 DoubleBuffered< Matrix > mInverseViewProjection; ///< Inverted viewprojection; double buffered for input handling
322 // Messages for CameraAttachment
324 inline void SetTypeMessage( EventThreadServices& eventThreadServices, const CameraAttachment& attachment, Dali::Camera::Type parameter )
326 typedef MessageValue1< CameraAttachment, Dali::Camera::Type > LocalType;
328 // Reserve some memory inside the message queue
329 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
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 );
335 inline void SetProjectionModeMessage( EventThreadServices& eventThreadServices, const CameraAttachment& attachment, Dali::Camera::ProjectionMode parameter )
337 typedef MessageValue1< CameraAttachment, Dali::Camera::ProjectionMode > LocalProjectionMode;
339 // Reserve some memory inside the message queue
340 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalProjectionMode ) );
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 );
347 inline void SetFieldOfViewMessage( EventThreadServices& eventThreadServices, const CameraAttachment& attachment, float parameter )
349 typedef MessageValue1< CameraAttachment, float > LocalType;
351 // Reserve some memory inside the message queue
352 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
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 );
358 inline void SetAspectRatioMessage( EventThreadServices& eventThreadServices, const CameraAttachment& attachment, float parameter )
360 typedef MessageValue1< CameraAttachment, float > LocalType;
362 // Reserve some memory inside the message queue
363 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
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 );
369 inline void SetStereoBiasMessage( EventThreadServices& eventThreadServices, const CameraAttachment& attachment, const Vector2& parameter )
371 typedef MessageValue1< CameraAttachment, Vector2 > LocalType;
373 // Reserve some memory inside the message queue
374 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
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 );
380 inline void SetLeftClippingPlaneMessage( EventThreadServices& eventThreadServices, const CameraAttachment& attachment, float parameter )
382 typedef MessageValue1< CameraAttachment, float > LocalType;
384 // Reserve some memory inside the message queue
385 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
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 );
391 inline void SetRightClippingPlaneMessage( EventThreadServices& eventThreadServices, const CameraAttachment& attachment, float parameter )
393 typedef MessageValue1< CameraAttachment, float > LocalType;
395 // Reserve some memory inside the message queue
396 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
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 );
402 inline void SetTopClippingPlaneMessage( EventThreadServices& eventThreadServices, const CameraAttachment& attachment, float parameter )
404 typedef MessageValue1< CameraAttachment, float > LocalType;
406 // Reserve some memory inside the message queue
407 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
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 );
413 inline void SetBottomClippingPlaneMessage( EventThreadServices& eventThreadServices, const CameraAttachment& attachment, float parameter )
415 typedef MessageValue1< CameraAttachment, float > LocalType;
417 // Reserve some memory inside the message queue
418 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
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 );
424 inline void SetNearClippingPlaneMessage( EventThreadServices& eventThreadServices, const CameraAttachment& attachment, float parameter )
426 typedef MessageValue1< CameraAttachment, float > LocalType;
428 // Reserve some memory inside the message queue
429 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
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 );
435 inline void SetFarClippingPlaneMessage( EventThreadServices& eventThreadServices, const CameraAttachment& attachment, float parameter )
437 typedef MessageValue1< CameraAttachment, float > LocalType;
439 // Reserve some memory inside the message queue
440 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
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 );
446 inline void SetTargetPositionMessage( EventThreadServices& eventThreadServices, const CameraAttachment& attachment, const Vector3& parameter )
448 typedef MessageValue1< CameraAttachment, Vector3 > LocalType;
450 // Reserve some memory inside the message queue
451 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
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 );
458 inline void SetInvertYAxisMessage( EventThreadServices& eventThreadServices, const CameraAttachment& attachment, bool parameter )
460 typedef MessageValue1< CameraAttachment, bool > LocalType;
462 // Reserve some memory inside the message queue
463 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
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 );
470 } // namespace SceneGraph
472 } // namespace Internal
476 #endif // __DALI_INTERNAL_SCENE_GRAPH_CAMERA_ATTACHMENT_H__