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 * Get the node this scene graph camera belongs to.
104 * @return node The owning node.
106 const Node* GetNode() const;
109 * @copydoc Dali::Internal::CameraActor::SetType
111 void SetType( Dali::Camera::Type type );
114 * @copydoc Dali::Internal::CameraActor::SetInvertYAxis
116 void SetInvertYAxis( bool invertYAxis );
119 * @copydoc Dali::Internal::CameraActor::SetProjectionMode
121 void SetProjectionMode( Dali::Camera::ProjectionMode projectionMode );
124 * @copydoc Dali::Internal::CameraActor::SetFieldOfView
126 void SetFieldOfView( float fieldOfView );
129 * @copydoc Dali::Internal::CameraActor::SetAspectRatio
131 void SetAspectRatio( float aspectRatio );
134 * @copydoc Dali::Internal::CameraActor::SetLeftClippingPlane
136 void SetLeftClippingPlane( float leftClippingPlane );
139 * @copydoc Dali::Internal::CameraActor::SetRightClippingPlane
141 void SetRightClippingPlane( float rightClippingPlane );
144 * @copydoc Dali::Internal::CameraActor::SetTopClippingPlane
146 void SetTopClippingPlane( float topClippingPlane );
149 * @copydoc Dali::Internal::CameraActor::SetBottomClippingPlane
151 void SetBottomClippingPlane( float bottomClippingPlane );
154 * @copydoc Dali::Internal::CameraActor::SetNearClippingPlane
156 void SetNearClippingPlane( float nearClippingPlane );
159 * @copydoc Dali::Internal::CameraActor::SetFarClippingPlane
161 void SetFarClippingPlane( float farClippingPlane );
164 * @copydoc Dali::Internal::CameraActor::RotateProjection
166 void RotateProjection( int rotationAngle );
169 * @copydoc Dali::Internal::CameraActor::SetTarget
171 void SetTargetPosition( const Vector3& targetPosition );
174 * Sets the reflection plane
175 * @param[in] plane reflection plane
177 void SetReflectByPlane( const Vector4& plane );
180 * Tests whether reflection is used
181 * @return True if used, False otherwise
183 bool GetReflectionUsed() const
185 return mUseReflection;
189 * Retrieve the view-matrix; this is double buffered for input handling.
190 * @param[in] bufferIndex The buffer to read from.
191 * @return The view-matrix.
193 const Matrix& GetViewMatrix( BufferIndex bufferIndex ) const;
196 * @brief Check to see if a sphere lies within the view frustum.
198 * @param bufferIndex The buffer to read from.
199 * @param origin The world position center of the sphere to check.
200 * @param radius The length of the sphere radius in world scale.
202 * @return false if the sphere lies outside of the frustum.
204 bool CheckSphereInFrustum( BufferIndex bufferIndex, const Vector3& origin, float radius );
207 * @brief Check to see if a bounding box lies within the view frustum.
209 * @param bufferIndex The buffer to read from.
210 * @param origin the world position center of the cubeoid to check.
211 * @param halfExtents The half length of the cubeoid in world co-ordinates in each axis.
213 * @return false if the cubeoid lies completely outside of the frustum, true otherwise
215 bool CheckAABBInFrustum( BufferIndex bufferIndex, const Vector3& origin, const Vector3& halfExtents );
218 * Retrieve the projection-matrix; this is double buffered for input handling.
219 * @param[in] bufferIndex The buffer to read from.
220 * @return The projection-matrix.
222 const Matrix& GetProjectionMatrix( BufferIndex bufferIndex ) const;
225 * Retrieve the inverted view-projection-matrix; this is double buffered for input handling.
226 * @param[in] bufferIndex The buffer to read from.
227 * @return The inverse view-projection-matrix.
229 const Matrix& GetInverseViewProjectionMatrix( BufferIndex bufferIndex ) const;
232 * Retrieve the final projection-matrix; this is double buffered for input handling.
233 * @param[in] bufferIndex The buffer to read from.
234 * @return The projection-matrix that should be used to render.
236 const Matrix& GetFinalProjectionMatrix( BufferIndex bufferIndex ) const;
239 * Retrieve the projection-matrix property querying interface.
240 * @pre The camera is on-stage.
241 * @return The projection-matrix property querying interface.
243 const PropertyInputImpl* GetProjectionMatrix() const;
246 * Retrieve the viewMatrix property querying interface.
247 * @pre The camera is on-stage.
248 * @return The viewMatrix property querying interface.
250 const PropertyInputImpl* GetViewMatrix() const;
253 * Updates view and projection matrices.
254 * Called by the render task using the camera
255 * @param[in] updateBufferIndex The buffer to read from.
257 void Update( BufferIndex updateBufferIndex );
260 * @return true if the view matrix of camera is updated this or the previous frame
262 bool ViewMatrixUpdated();
273 Camera(const Camera&);
275 Camera& operator=(const Camera& rhs);
278 * Recalculates the view matrix.
279 * @param[in] bufferIndex The current update buffer index.
280 * @return count how many frames ago the matrix was changed.
282 uint32_t UpdateViewMatrix( BufferIndex updateBufferIndex );
285 * Recalculates the projection matrix.
286 * @param[in] bufferIndex The current update buffer index.
287 * @return count how many frames ago the matrix was changed.
289 uint32_t UpdateProjection( BufferIndex updateBufferIndex );
294 * @brief Extracts the frustum planes.
296 * @param[in] bufferIndex The current update buffer index.
297 * @param[in] normalize will normalize plane equation coefficients by default.
299 void UpdateFrustum( BufferIndex updateBufferIndex, bool normalize = true );
302 * Adjust near plane for reflection
303 * @param perspective Perspective matrix
304 * @param clipPlane Clipping plane
306 void AdjustNearPlaneForPerspective( Matrix& perspective, const Vector4& clipPlane );
308 uint32_t mUpdateViewFlag; ///< This is non-zero if the view matrix requires an update
309 uint32_t mUpdateProjectionFlag; ///< This is non-zero if the projection matrix requires an update
310 int mProjectionRotation; ///< The rotaion angle of the projection
311 const Node* mNode; ///< The node this scene graph camera belongs to
313 public: // PROPERTIES
314 Dali::Camera::Type mType; // Non-animatable
315 Dali::Camera::ProjectionMode mProjectionMode; // Non-animatable
316 bool mInvertYAxis; // Non-animatable
320 float mLeftClippingPlane;
321 float mRightClippingPlane;
322 float mTopClippingPlane;
323 float mBottomClippingPlane;
324 float mNearClippingPlane;
325 float mFarClippingPlane;
326 Vector3 mTargetPosition;
328 Dali::Matrix mReflectionMtx;
329 Dali::Vector4 mReflectionPlane;
330 Dali::Vector4 mReflectionEye;
331 bool mUseReflection{ false };
332 bool mUseReflectionClip{ false };
334 InheritedMatrix mViewMatrix; ///< The viewMatrix; this is double buffered for input handling.
335 InheritedMatrix mProjectionMatrix; ///< The projectionMatrix; this is double buffered for input handling.
337 DoubleBuffered< FrustumPlanes > mFrustum; ///< Clipping frustum; double buffered for input handling
338 DoubleBuffered< Matrix > mInverseViewProjection; ///< Inverted viewprojection; double buffered for input handling
339 DoubleBuffered< Matrix > mFinalProjection; ///< Final projection matrix; double buffered for input handling
343 // Messages for Camera
345 inline void SetTypeMessage( EventThreadServices& eventThreadServices, const Camera& camera, Dali::Camera::Type parameter )
347 typedef MessageValue1< Camera, Dali::Camera::Type > LocalType;
349 // Reserve some memory inside the message queue
350 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
352 // Construct message in the message queue memory; note that delete should not be called on the return value
353 new (slot) LocalType( &camera, &Camera::SetType, parameter );
356 inline void SetProjectionModeMessage( EventThreadServices& eventThreadServices, const Camera& camera, Dali::Camera::ProjectionMode parameter )
358 typedef MessageValue1< Camera, Dali::Camera::ProjectionMode > LocalProjectionMode;
360 // Reserve some memory inside the message queue
361 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalProjectionMode ) );
363 // Construct message in the message queue memory; note that delete should not be called on the return value
364 new (slot) LocalProjectionMode( &camera, &Camera::SetProjectionMode, parameter );
367 inline void SetFieldOfViewMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
369 typedef MessageValue1< Camera, float > LocalType;
371 // Reserve some memory inside the message queue
372 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
374 // Construct message in the message queue memory; note that delete should not be called on the return value
375 new (slot) LocalType( &camera, &Camera::SetFieldOfView, parameter );
378 inline void SetAspectRatioMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
380 typedef MessageValue1< Camera, float > LocalType;
382 // Reserve some memory inside the message queue
383 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
385 // Construct message in the message queue memory; note that delete should not be called on the return value
386 new (slot) LocalType( &camera, &Camera::SetAspectRatio, parameter );
389 inline void SetLeftClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
391 typedef MessageValue1< Camera, float > LocalType;
393 // Reserve some memory inside the message queue
394 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
396 // Construct message in the message queue memory; note that delete should not be called on the return value
397 new (slot) LocalType( &camera, &Camera::SetLeftClippingPlane, parameter );
400 inline void SetRightClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
402 typedef MessageValue1< Camera, float > LocalType;
404 // Reserve some memory inside the message queue
405 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
407 // Construct message in the message queue memory; note that delete should not be called on the return value
408 new (slot) LocalType( &camera, &Camera::SetRightClippingPlane, parameter );
411 inline void SetTopClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
413 typedef MessageValue1< Camera, float > LocalType;
415 // Reserve some memory inside the message queue
416 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
418 // Construct message in the message queue memory; note that delete should not be called on the return value
419 new (slot) LocalType( &camera, &Camera::SetTopClippingPlane, parameter );
422 inline void SetBottomClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
424 typedef MessageValue1< Camera, float > LocalType;
426 // Reserve some memory inside the message queue
427 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
429 // Construct message in the message queue memory; note that delete should not be called on the return value
430 new (slot) LocalType( &camera, &Camera::SetBottomClippingPlane, parameter );
433 inline void SetNearClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
435 typedef MessageValue1< Camera, float > LocalType;
437 // Reserve some memory inside the message queue
438 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
440 // Construct message in the message queue memory; note that delete should not be called on the return value
441 new (slot) LocalType( &camera, &Camera::SetNearClippingPlane, parameter );
444 inline void SetFarClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
446 typedef MessageValue1< Camera, float > LocalType;
448 // Reserve some memory inside the message queue
449 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
451 // Construct message in the message queue memory; note that delete should not be called on the return value
452 new (slot) LocalType( &camera, &Camera::SetFarClippingPlane, parameter );
455 inline void SetTargetPositionMessage( EventThreadServices& eventThreadServices, const Camera& camera, const Vector3& parameter )
457 typedef MessageValue1< Camera, Vector3 > LocalType;
459 // Reserve some memory inside the message queue
460 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
462 // Construct message in the message queue memory; note that delete should not be called on the return value
463 new (slot) LocalType( &camera, &Camera::SetTargetPosition, parameter );
466 inline void SetInvertYAxisMessage( EventThreadServices& eventThreadServices, const Camera& camera, bool parameter )
468 typedef MessageValue1< Camera, bool > LocalType;
470 // Reserve some memory inside the message queue
471 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
473 // Construct message in the message queue memory; note that delete should not be called on the return value
474 new (slot) LocalType( &camera, &Camera::SetInvertYAxis, parameter );
477 inline void RotateProjectionMessage( EventThreadServices& eventThreadServices, const Camera& camera, int parameter )
479 typedef MessageValue1< Camera, int > LocalType;
481 // Reserve some memory inside the message queue
482 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
484 // Construct message in the message queue memory; note that delete should not be called on the return value
485 new (slot) LocalType( &camera, &Camera::RotateProjection, parameter );
488 } // namespace SceneGraph
490 } // namespace Internal
494 #endif // DALI_INTERNAL_SCENE_GRAPH_CAMERA_H