1 #ifndef DALI_INTERNAL_SCENE_GRAPH_CAMERA_H
2 #define DALI_INTERNAL_SCENE_GRAPH_CAMERA_H
5 * Copyright (c) 2018 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 * Set the node this scene graph camera belongs to.
98 * @param[in] node The owning node.
100 void SetNode( const Node* node );
103 * @copydoc Dali::Internal::CameraActor::SetType
105 void SetType( Dali::Camera::Type type );
108 * @copydoc Dali::Internal::CameraActor::SetInvertYAxis
110 void SetInvertYAxis( bool invertYAxis );
113 * @copydoc Dali::Internal::CameraActor::SetProjectionMode
115 void SetProjectionMode( Dali::Camera::ProjectionMode projectionMode );
118 * @copydoc Dali::Internal::CameraActor::SetFieldOfView
120 void SetFieldOfView( float fieldOfView );
123 * @copydoc Dali::Internal::CameraActor::SetAspectRatio
125 void SetAspectRatio( float aspectRatio );
128 * @copydoc Dali::Internal::CameraActor::SetLeftClippingPlane
130 void SetLeftClippingPlane( float leftClippingPlane );
133 * @copydoc Dali::Internal::CameraActor::SetRightClippingPlane
135 void SetRightClippingPlane( float rightClippingPlane );
138 * @copydoc Dali::Internal::CameraActor::SetTopClippingPlane
140 void SetTopClippingPlane( float topClippingPlane );
143 * @copydoc Dali::Internal::CameraActor::SetBottomClippingPlane
145 void SetBottomClippingPlane( float bottomClippingPlane );
148 * @copydoc Dali::Internal::CameraActor::SetNearClippingPlane
150 void SetNearClippingPlane( float nearClippingPlane );
153 * @copydoc Dali::Internal::CameraActor::SetFarClippingPlane
155 void SetFarClippingPlane( float farClippingPlane );
158 * @copydoc Dali::Internal::CameraActor::RotateProjection
160 void RotateProjection( int rotationAngle );
163 * @copydoc Dali::Internal::CameraActor::SetTarget
165 void SetTargetPosition( const Vector3& targetPosition );
168 * Retrieve the view-matrix; this is double buffered for input handling.
169 * @param[in] bufferIndex The buffer to read from.
170 * @return The view-matrix.
172 const Matrix& GetViewMatrix( BufferIndex bufferIndex ) const;
175 * @brief Check to see if a sphere lies within the view frustum.
177 * @param bufferIndex The buffer to read from.
178 * @param origin The world position center of the sphere to check.
179 * @param radius The length of the sphere radius in world scale.
181 * @return false if the sphere lies outside of the frustum.
183 bool CheckSphereInFrustum( BufferIndex bufferIndex, const Vector3& origin, float radius );
186 * @brief Check to see if a bounding box lies within the view frustum.
188 * @param bufferIndex The buffer to read from.
189 * @param origin the world position center of the cubeoid to check.
190 * @param halfExtents The half length of the cubeoid in world co-ordinates in each axis.
192 * @return false if the cubeoid lies completely outside of the frustum, true otherwise
194 bool CheckAABBInFrustum( BufferIndex bufferIndex, const Vector3& origin, const Vector3& halfExtents );
197 * Retrieve the projection-matrix; this is double buffered for input handling.
198 * @param[in] bufferIndex The buffer to read from.
199 * @return The projection-matrix.
201 const Matrix& GetProjectionMatrix( BufferIndex bufferIndex ) const;
204 * Retrieve the inverted view-projection-matrix; this is double buffered for input handling.
205 * @param[in] bufferIndex The buffer to read from.
206 * @return The inverse view-projection-matrix.
208 const Matrix& GetInverseViewProjectionMatrix( BufferIndex bufferIndex ) const;
211 * Retrieve the final projection-matrix; this is double buffered for input handling.
212 * @param[in] bufferIndex The buffer to read from.
213 * @return The projection-matrix that should be used to render.
215 const Matrix& GetFinalProjectionMatrix( BufferIndex bufferIndex ) const;
218 * Retrieve the projection-matrix property querying interface.
219 * @pre The camera is on-stage.
220 * @return The projection-matrix property querying interface.
222 const PropertyInputImpl* GetProjectionMatrix() const;
225 * Retrieve the viewMatrix property querying interface.
226 * @pre The camera is on-stage.
227 * @return The viewMatrix property querying interface.
229 const PropertyInputImpl* GetViewMatrix() const;
232 * Updates view and projection matrices.
233 * Called by the render task using the camera
234 * @param[in] updateBufferIndex The buffer to read from.
236 void Update( BufferIndex updateBufferIndex );
239 * @return true if the view matrix of camera is updated this or the previous frame
241 bool ViewMatrixUpdated();
252 Camera(const Camera&);
254 Camera& operator=(const Camera& rhs);
257 * Recalculates the view matrix.
258 * @param[in] bufferIndex The current update buffer index.
259 * @return count how many frames ago the matrix was changed.
261 uint32_t UpdateViewMatrix( BufferIndex updateBufferIndex );
264 * Recalculates the projection matrix.
265 * @param[in] bufferIndex The current update buffer index.
266 * @return count how many frames ago the matrix was changed.
268 uint32_t UpdateProjection( BufferIndex updateBufferIndex );
273 * @brief Extracts the frustum planes.
275 * @param[in] bufferIndex The current update buffer index.
276 * @param[in] normalize will normalize plane equation coefficients by default.
278 void UpdateFrustum( BufferIndex updateBufferIndex, bool normalize = true );
280 uint32_t mUpdateViewFlag; ///< This is non-zero if the view matrix requires an update
281 uint32_t mUpdateProjectionFlag; ///< This is non-zero if the projection matrix requires an update
282 int mProjectionRotation; ///< The rotaion angle of the projection
283 const Node* mNode; ///< The node this scene graph camera belongs to
285 public: // PROPERTIES
286 Dali::Camera::Type mType; // Non-animatable
287 Dali::Camera::ProjectionMode mProjectionMode; // Non-animatable
288 bool mInvertYAxis; // Non-animatable
292 float mLeftClippingPlane;
293 float mRightClippingPlane;
294 float mTopClippingPlane;
295 float mBottomClippingPlane;
296 float mNearClippingPlane;
297 float mFarClippingPlane;
298 Vector3 mTargetPosition;
300 InheritedMatrix mViewMatrix; ///< The viewMatrix; this is double buffered for input handling.
301 InheritedMatrix mProjectionMatrix; ///< The projectionMatrix; this is double buffered for input handling.
303 DoubleBuffered< FrustumPlanes > mFrustum; ///< Clipping frustum; double buffered for input handling
304 DoubleBuffered< Matrix > mInverseViewProjection; ///< Inverted viewprojection; double buffered for input handling
305 DoubleBuffered< Matrix > mFinalProjection; ///< Final projection matrix; double buffered for input handling
309 // Messages for Camera
311 inline void SetTypeMessage( EventThreadServices& eventThreadServices, const Camera& camera, Dali::Camera::Type parameter )
313 typedef MessageValue1< Camera, Dali::Camera::Type > LocalType;
315 // Reserve some memory inside the message queue
316 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
318 // Construct message in the message queue memory; note that delete should not be called on the return value
319 new (slot) LocalType( &camera, &Camera::SetType, parameter );
322 inline void SetProjectionModeMessage( EventThreadServices& eventThreadServices, const Camera& camera, Dali::Camera::ProjectionMode parameter )
324 typedef MessageValue1< Camera, Dali::Camera::ProjectionMode > LocalProjectionMode;
326 // Reserve some memory inside the message queue
327 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalProjectionMode ) );
329 // Construct message in the message queue memory; note that delete should not be called on the return value
330 new (slot) LocalProjectionMode( &camera, &Camera::SetProjectionMode, parameter );
333 inline void SetFieldOfViewMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
335 typedef MessageValue1< Camera, float > LocalType;
337 // Reserve some memory inside the message queue
338 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
340 // Construct message in the message queue memory; note that delete should not be called on the return value
341 new (slot) LocalType( &camera, &Camera::SetFieldOfView, parameter );
344 inline void SetAspectRatioMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
346 typedef MessageValue1< Camera, float > LocalType;
348 // Reserve some memory inside the message queue
349 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
351 // Construct message in the message queue memory; note that delete should not be called on the return value
352 new (slot) LocalType( &camera, &Camera::SetAspectRatio, parameter );
355 inline void SetLeftClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
357 typedef MessageValue1< Camera, float > LocalType;
359 // Reserve some memory inside the message queue
360 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
362 // Construct message in the message queue memory; note that delete should not be called on the return value
363 new (slot) LocalType( &camera, &Camera::SetLeftClippingPlane, parameter );
366 inline void SetRightClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
368 typedef MessageValue1< Camera, float > LocalType;
370 // Reserve some memory inside the message queue
371 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
373 // Construct message in the message queue memory; note that delete should not be called on the return value
374 new (slot) LocalType( &camera, &Camera::SetRightClippingPlane, parameter );
377 inline void SetTopClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
379 typedef MessageValue1< Camera, float > LocalType;
381 // Reserve some memory inside the message queue
382 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
384 // Construct message in the message queue memory; note that delete should not be called on the return value
385 new (slot) LocalType( &camera, &Camera::SetTopClippingPlane, parameter );
388 inline void SetBottomClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
390 typedef MessageValue1< Camera, float > LocalType;
392 // Reserve some memory inside the message queue
393 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
395 // Construct message in the message queue memory; note that delete should not be called on the return value
396 new (slot) LocalType( &camera, &Camera::SetBottomClippingPlane, parameter );
399 inline void SetNearClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
401 typedef MessageValue1< Camera, float > LocalType;
403 // Reserve some memory inside the message queue
404 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
406 // Construct message in the message queue memory; note that delete should not be called on the return value
407 new (slot) LocalType( &camera, &Camera::SetNearClippingPlane, parameter );
410 inline void SetFarClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
412 typedef MessageValue1< Camera, float > LocalType;
414 // Reserve some memory inside the message queue
415 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
417 // Construct message in the message queue memory; note that delete should not be called on the return value
418 new (slot) LocalType( &camera, &Camera::SetFarClippingPlane, parameter );
421 inline void SetTargetPositionMessage( EventThreadServices& eventThreadServices, const Camera& camera, const Vector3& parameter )
423 typedef MessageValue1< Camera, Vector3 > LocalType;
425 // Reserve some memory inside the message queue
426 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
428 // Construct message in the message queue memory; note that delete should not be called on the return value
429 new (slot) LocalType( &camera, &Camera::SetTargetPosition, parameter );
432 inline void SetInvertYAxisMessage( EventThreadServices& eventThreadServices, const Camera& camera, bool parameter )
434 typedef MessageValue1< Camera, bool > LocalType;
436 // Reserve some memory inside the message queue
437 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
439 // Construct message in the message queue memory; note that delete should not be called on the return value
440 new (slot) LocalType( &camera, &Camera::SetInvertYAxis, parameter );
443 inline void RotateProjectionMessage( EventThreadServices& eventThreadServices, const Camera& camera, int parameter )
445 typedef MessageValue1< Camera, int > LocalType;
447 // Reserve some memory inside the message queue
448 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
450 // Construct message in the message queue memory; note that delete should not be called on the return value
451 new (slot) LocalType( &camera, &Camera::RotateProjection, parameter );
454 } // namespace SceneGraph
456 } // namespace Internal
460 #endif // DALI_INTERNAL_SCENE_GRAPH_CAMERA_H