1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_CAMERA_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_CAMERA_H__
5 * Copyright (c) 2016 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/common/inherited-property.h>
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 > {};
45 class SceneController;
48 * Scene-graph camera object
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;
68 * Plane equation container for a plane of the view frustum
77 * @brief Container for six planes in a view frustum
86 * Construct a new Camera.
87 * @return a new camera.
97 * @copydoc Dali::Internal::CameraActor::SetType
99 void SetType( Dali::Camera::Type type );
102 * @copydoc Dali::Internal::CameraActor::SetInvertYAxis
104 void SetInvertYAxis( bool invertYAxis );
107 * @copydoc Dali::Internal::CameraActor::SetProjectionMode
109 void SetProjectionMode( Dali::Camera::ProjectionMode projectionMode );
112 * @copydoc Dali::Internal::CameraActor::SetFieldOfView
114 void SetFieldOfView( float fieldOfView );
117 * @copydoc Dali::Internal::CameraActor::SetAspectRatio
119 void SetAspectRatio( float aspectRatio );
122 * @copydoc Dali::Internal::CameraActor::SetStereoBias
124 void SetStereoBias(const Vector2& stereoBias);
127 * @copydoc Dali::Internal::CameraActor::SetLeftClippingPlane
129 void SetLeftClippingPlane( float leftClippingPlane );
132 * @copydoc Dali::Internal::CameraActor::SetRightClippingPlane
134 void SetRightClippingPlane( float rightClippingPlane );
137 * @copydoc Dali::Internal::CameraActor::SetTopClippingPlane
139 void SetTopClippingPlane( float topClippingPlane );
142 * @copydoc Dali::Internal::CameraActor::SetBottomClippingPlane
144 void SetBottomClippingPlane( float bottomClippingPlane );
147 * @copydoc Dali::Internal::CameraActor::SetNearClippingPlane
149 void SetNearClippingPlane( float nearClippingPlane );
152 * @copydoc Dali::Internal::CameraActor::SetFarClippingPlane
154 void SetFarClippingPlane( float farClippingPlane );
157 * @copydoc Dali::Internal::CameraActor::SetTarget
159 void SetTargetPosition( const Vector3& targetPosition );
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.
166 const Matrix& GetViewMatrix( BufferIndex bufferIndex ) const;
169 * @brief Check to see if a sphere lies within the view frustum.
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.
175 * @return false if the sphere lies outside of the frustum.
177 bool CheckSphereInFrustum( BufferIndex bufferIndex, const Vector3& origin, float radius );
180 * @brief Check to see if a bounding box lies within the view frustum.
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.
186 * @return false if the cubeoid lies completely outside of the frustum, true otherwise
188 bool CheckAABBInFrustum( BufferIndex bufferIndex, const Vector3& origin, const Vector3& halfExtents );
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.
195 const Matrix& GetProjectionMatrix( BufferIndex bufferIndex ) const;
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.
202 const Matrix& GetInverseViewProjectionMatrix( BufferIndex bufferIndex ) const;
205 * Retrieve the projection-matrix property querying interface.
206 * @pre The camera is on-stage.
207 * @return The projection-matrix property querying interface.
209 const PropertyInputImpl* GetProjectionMatrix() const;
212 * Retrieve the viewMatrix property querying interface.
213 * @pre The camera is on-stage.
214 * @return The viewMatrix property querying interface.
216 const PropertyInputImpl* GetViewMatrix() const;
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
224 void Update( BufferIndex updateBufferIndex, const Node& owningNode );
227 * @return true if the view matrix of camera is updated this or the previous frame
229 bool ViewMatrixUpdated();
240 Camera(const Camera&);
242 Camera& operator=(const Camera& rhs);
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.
250 unsigned int UpdateViewMatrix( BufferIndex updateBufferIndex, const Node& owningNode );
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.
257 unsigned int UpdateProjection( BufferIndex updateBufferIndex );
262 * @brief Extracts the frustum planes.
264 * @param[in] bufferIndex The current update buffer index.
265 * @param[in] normalize will normalize plane equation coefficients by default.
267 void UpdateFrustum( BufferIndex updateBufferIndex, bool normalize = true );
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
272 public: // PROPERTIES
273 Dali::Camera::Type mType; // Non-animatable
274 Dali::Camera::ProjectionMode mProjectionMode; // Non-animatable
275 bool mInvertYAxis; // Non-animatable
279 float mLeftClippingPlane;
280 float mRightClippingPlane;
281 float mTopClippingPlane;
282 float mBottomClippingPlane;
283 float mNearClippingPlane;
284 float mFarClippingPlane;
286 Vector3 mTargetPosition;
288 InheritedMatrix mViewMatrix; ///< The viewMatrix; this is double buffered for input handling.
289 InheritedMatrix mProjectionMatrix; ///< The projectionMatrix; this is double buffered for input handling.
291 DoubleBuffered< FrustumPlanes > mFrustum; ///< Clipping frustum; double buffered for input handling
292 DoubleBuffered< Matrix > mInverseViewProjection; ///< Inverted viewprojection; double buffered for input handling
296 // Messages for Camera
298 inline void SetTypeMessage( EventThreadServices& eventThreadServices, const Camera& camera, Dali::Camera::Type parameter )
300 typedef MessageValue1< Camera, Dali::Camera::Type > LocalType;
302 // Reserve some memory inside the message queue
303 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
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 );
309 inline void SetProjectionModeMessage( EventThreadServices& eventThreadServices, const Camera& camera, Dali::Camera::ProjectionMode parameter )
311 typedef MessageValue1< Camera, Dali::Camera::ProjectionMode > LocalProjectionMode;
313 // Reserve some memory inside the message queue
314 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalProjectionMode ) );
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 );
320 inline void SetFieldOfViewMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
322 typedef MessageValue1< Camera, float > LocalType;
324 // Reserve some memory inside the message queue
325 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
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 );
331 inline void SetAspectRatioMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
333 typedef MessageValue1< Camera, float > LocalType;
335 // Reserve some memory inside the message queue
336 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
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 );
342 inline void SetStereoBiasMessage( EventThreadServices& eventThreadServices, const Camera& camera, const Vector2& parameter )
344 typedef MessageValue1< Camera, Vector2 > LocalType;
346 // Reserve some memory inside the message queue
347 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
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 );
353 inline void SetLeftClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
355 typedef MessageValue1< Camera, float > LocalType;
357 // Reserve some memory inside the message queue
358 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
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 );
364 inline void SetRightClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
366 typedef MessageValue1< Camera, float > LocalType;
368 // Reserve some memory inside the message queue
369 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
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 );
375 inline void SetTopClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
377 typedef MessageValue1< Camera, float > LocalType;
379 // Reserve some memory inside the message queue
380 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
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 );
386 inline void SetBottomClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
388 typedef MessageValue1< Camera, float > LocalType;
390 // Reserve some memory inside the message queue
391 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
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 );
397 inline void SetNearClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
399 typedef MessageValue1< Camera, float > LocalType;
401 // Reserve some memory inside the message queue
402 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
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 );
408 inline void SetFarClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
410 typedef MessageValue1< Camera, float > LocalType;
412 // Reserve some memory inside the message queue
413 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
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 );
419 inline void SetTargetPositionMessage( EventThreadServices& eventThreadServices, const Camera& camera, const Vector3& parameter )
421 typedef MessageValue1< Camera, Vector3 > LocalType;
423 // Reserve some memory inside the message queue
424 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
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 );
430 inline void SetInvertYAxisMessage( EventThreadServices& eventThreadServices, const Camera& camera, bool parameter )
432 typedef MessageValue1< Camera, bool > LocalType;
434 // Reserve some memory inside the message queue
435 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
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 );
441 } // namespace SceneGraph
443 } // namespace Internal
447 #endif // __DALI_INTERNAL_SCENE_GRAPH_CAMERA_H__