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::SetTarget
166 void SetTargetPosition( const Vector3& targetPosition );
169 * Sets the reflection plane
170 * @param[in] plane reflection plane
172 void SetReflectByPlane( const Vector4& plane );
175 * Tests whether reflection is used
176 * @return True if used, False otherwise
178 bool GetReflectionUsed() const
180 return mUseReflection;
184 * Retrieve the view-matrix; this is double buffered for input handling.
185 * @param[in] bufferIndex The buffer to read from.
186 * @return The view-matrix.
188 const Matrix& GetViewMatrix( BufferIndex bufferIndex ) const;
191 * @brief Check to see if a sphere lies within the view frustum.
193 * @param bufferIndex The buffer to read from.
194 * @param origin The world position center of the sphere to check.
195 * @param radius The length of the sphere radius in world scale.
197 * @return false if the sphere lies outside of the frustum.
199 bool CheckSphereInFrustum( BufferIndex bufferIndex, const Vector3& origin, float radius );
202 * @brief Check to see if a bounding box lies within the view frustum.
204 * @param bufferIndex The buffer to read from.
205 * @param origin the world position center of the cubeoid to check.
206 * @param halfExtents The half length of the cubeoid in world co-ordinates in each axis.
208 * @return false if the cubeoid lies completely outside of the frustum, true otherwise
210 bool CheckAABBInFrustum( BufferIndex bufferIndex, const Vector3& origin, const Vector3& halfExtents );
213 * Retrieve the projection-matrix; this is double buffered for input handling.
214 * @param[in] bufferIndex The buffer to read from.
215 * @return The projection-matrix.
217 const Matrix& GetProjectionMatrix( BufferIndex bufferIndex ) const;
220 * Retrieve the inverted view-projection-matrix; this is double buffered for input handling.
221 * @param[in] bufferIndex The buffer to read from.
222 * @return The inverse view-projection-matrix.
224 const Matrix& GetInverseViewProjectionMatrix( BufferIndex bufferIndex ) const;
227 * Retrieve the projection-matrix property querying interface.
228 * @pre The camera is on-stage.
229 * @return The projection-matrix property querying interface.
231 const PropertyInputImpl* GetProjectionMatrix() const;
234 * Retrieve the viewMatrix property querying interface.
235 * @pre The camera is on-stage.
236 * @return The viewMatrix property querying interface.
238 const PropertyInputImpl* GetViewMatrix() const;
241 * Updates view and projection matrices.
242 * Called by the render task using the camera
243 * @param[in] updateBufferIndex The buffer to read from.
245 void Update( BufferIndex updateBufferIndex );
248 * @return true if the view matrix of camera is updated this or the previous frame
250 bool ViewMatrixUpdated();
261 Camera(const Camera&);
263 Camera& operator=(const Camera& rhs);
266 * Recalculates the view matrix.
267 * @param[in] bufferIndex The current update buffer index.
268 * @return count how many frames ago the matrix was changed.
270 uint32_t UpdateViewMatrix( BufferIndex updateBufferIndex );
273 * Recalculates the projection matrix.
274 * @param[in] bufferIndex The current update buffer index.
275 * @return count how many frames ago the matrix was changed.
277 uint32_t UpdateProjection( BufferIndex updateBufferIndex );
282 * @brief Extracts the frustum planes.
284 * @param[in] bufferIndex The current update buffer index.
285 * @param[in] normalize will normalize plane equation coefficients by default.
287 void UpdateFrustum( BufferIndex updateBufferIndex, bool normalize = true );
290 * Adjust near plane for reflection
291 * @param perspective Perspective matrix
292 * @param clipPlane Clipping plane
294 void AdjustNearPlaneForPerspective( Matrix& perspective, const Vector4& clipPlane );
296 uint32_t mUpdateViewFlag; ///< This is non-zero if the view matrix requires an update
297 uint32_t mUpdateProjectionFlag; ///< This is non-zero if the projection matrix requires an update
298 const Node* mNode; ///< The node this scene graph camera belongs to
300 public: // PROPERTIES
301 Dali::Camera::Type mType; // Non-animatable
302 Dali::Camera::ProjectionMode mProjectionMode; // Non-animatable
303 bool mInvertYAxis; // Non-animatable
307 float mLeftClippingPlane;
308 float mRightClippingPlane;
309 float mTopClippingPlane;
310 float mBottomClippingPlane;
311 float mNearClippingPlane;
312 float mFarClippingPlane;
313 Vector3 mTargetPosition;
315 Dali::Matrix mReflectionMtx;
316 Dali::Vector4 mReflectionPlane;
317 Dali::Vector4 mReflectionEye;
318 bool mUseReflection{ false };
319 bool mUseReflectionClip{ false };
321 InheritedMatrix mViewMatrix; ///< The viewMatrix; this is double buffered for input handling.
322 InheritedMatrix mProjectionMatrix; ///< The projectionMatrix; this is double buffered for input handling.
324 DoubleBuffered< FrustumPlanes > mFrustum; ///< Clipping frustum; double buffered for input handling
325 DoubleBuffered< Matrix > mInverseViewProjection; ///< Inverted viewprojection; double buffered for input handling
329 // Messages for Camera
331 inline void SetTypeMessage( EventThreadServices& eventThreadServices, const Camera& camera, Dali::Camera::Type parameter )
333 typedef MessageValue1< Camera, Dali::Camera::Type > LocalType;
335 // Reserve some memory inside the message queue
336 uint32_t* 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::SetType, parameter );
342 inline void SetProjectionModeMessage( EventThreadServices& eventThreadServices, const Camera& camera, Dali::Camera::ProjectionMode parameter )
344 typedef MessageValue1< Camera, Dali::Camera::ProjectionMode > LocalProjectionMode;
346 // Reserve some memory inside the message queue
347 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalProjectionMode ) );
349 // Construct message in the message queue memory; note that delete should not be called on the return value
350 new (slot) LocalProjectionMode( &camera, &Camera::SetProjectionMode, parameter );
353 inline void SetFieldOfViewMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
355 typedef MessageValue1< Camera, float > LocalType;
357 // Reserve some memory inside the message queue
358 uint32_t* 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::SetFieldOfView, parameter );
364 inline void SetAspectRatioMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
366 typedef MessageValue1< Camera, float > LocalType;
368 // Reserve some memory inside the message queue
369 uint32_t* 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::SetAspectRatio, parameter );
375 inline void SetLeftClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
377 typedef MessageValue1< Camera, float > LocalType;
379 // Reserve some memory inside the message queue
380 uint32_t* 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::SetLeftClippingPlane, parameter );
386 inline void SetRightClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
388 typedef MessageValue1< Camera, float > LocalType;
390 // Reserve some memory inside the message queue
391 uint32_t* 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::SetRightClippingPlane, parameter );
397 inline void SetTopClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
399 typedef MessageValue1< Camera, float > LocalType;
401 // Reserve some memory inside the message queue
402 uint32_t* 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::SetTopClippingPlane, parameter );
408 inline void SetBottomClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
410 typedef MessageValue1< Camera, float > LocalType;
412 // Reserve some memory inside the message queue
413 uint32_t* 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::SetBottomClippingPlane, parameter );
419 inline void SetNearClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
421 typedef MessageValue1< Camera, float > LocalType;
423 // Reserve some memory inside the message queue
424 uint32_t* 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::SetNearClippingPlane, parameter );
430 inline void SetFarClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
432 typedef MessageValue1< Camera, float > LocalType;
434 // Reserve some memory inside the message queue
435 uint32_t* 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::SetFarClippingPlane, parameter );
441 inline void SetTargetPositionMessage( EventThreadServices& eventThreadServices, const Camera& camera, const Vector3& parameter )
443 typedef MessageValue1< Camera, Vector3 > LocalType;
445 // Reserve some memory inside the message queue
446 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
448 // Construct message in the message queue memory; note that delete should not be called on the return value
449 new (slot) LocalType( &camera, &Camera::SetTargetPosition, parameter );
452 inline void SetInvertYAxisMessage( EventThreadServices& eventThreadServices, const Camera& camera, bool parameter )
454 typedef MessageValue1< Camera, bool > LocalType;
456 // Reserve some memory inside the message queue
457 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
459 // Construct message in the message queue memory; note that delete should not be called on the return value
460 new (slot) LocalType( &camera, &Camera::SetInvertYAxis, parameter );
463 } // namespace SceneGraph
465 } // namespace Internal
469 #endif // DALI_INTERNAL_SCENE_GRAPH_CAMERA_H