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::SetTarget
160 void SetTargetPosition( const Vector3& targetPosition );
163 * Sets the reflection plane
164 * @param[in] plane reflection plane
166 void SetReflectByPlane( const Vector4& plane );
169 * Tests whether reflection is used
170 * @return True if used, False otherwise
172 bool GetReflectionUsed() const
174 return mUseReflection;
178 * Retrieve the view-matrix; this is double buffered for input handling.
179 * @param[in] bufferIndex The buffer to read from.
180 * @return The view-matrix.
182 const Matrix& GetViewMatrix( BufferIndex bufferIndex ) const;
185 * @brief Check to see if a sphere lies within the view frustum.
187 * @param bufferIndex The buffer to read from.
188 * @param origin The world position center of the sphere to check.
189 * @param radius The length of the sphere radius in world scale.
191 * @return false if the sphere lies outside of the frustum.
193 bool CheckSphereInFrustum( BufferIndex bufferIndex, const Vector3& origin, float radius );
196 * @brief Check to see if a bounding box lies within the view frustum.
198 * @param bufferIndex The buffer to read from.
199 * @param origin the world position center of the cubeoid to check.
200 * @param halfExtents The half length of the cubeoid in world co-ordinates in each axis.
202 * @return false if the cubeoid lies completely outside of the frustum, true otherwise
204 bool CheckAABBInFrustum( BufferIndex bufferIndex, const Vector3& origin, const Vector3& halfExtents );
207 * Retrieve the projection-matrix; this is double buffered for input handling.
208 * @param[in] bufferIndex The buffer to read from.
209 * @return The projection-matrix.
211 const Matrix& GetProjectionMatrix( BufferIndex bufferIndex ) const;
214 * Retrieve the inverted view-projection-matrix; this is double buffered for input handling.
215 * @param[in] bufferIndex The buffer to read from.
216 * @return The inverse view-projection-matrix.
218 const Matrix& GetInverseViewProjectionMatrix( BufferIndex bufferIndex ) const;
221 * Retrieve the projection-matrix property querying interface.
222 * @pre The camera is on-stage.
223 * @return The projection-matrix property querying interface.
225 const PropertyInputImpl* GetProjectionMatrix() const;
228 * Retrieve the viewMatrix property querying interface.
229 * @pre The camera is on-stage.
230 * @return The viewMatrix property querying interface.
232 const PropertyInputImpl* GetViewMatrix() const;
235 * Updates view and projection matrices.
236 * Called by the render task using the camera
237 * @param[in] updateBufferIndex The buffer to read from.
239 void Update( BufferIndex updateBufferIndex );
242 * @return true if the view matrix of camera is updated this or the previous frame
244 bool ViewMatrixUpdated();
255 Camera(const Camera&);
257 Camera& operator=(const Camera& rhs);
260 * Recalculates the view matrix.
261 * @param[in] bufferIndex The current update buffer index.
262 * @return count how many frames ago the matrix was changed.
264 uint32_t UpdateViewMatrix( BufferIndex updateBufferIndex );
267 * Recalculates the projection matrix.
268 * @param[in] bufferIndex The current update buffer index.
269 * @return count how many frames ago the matrix was changed.
271 uint32_t UpdateProjection( BufferIndex updateBufferIndex );
276 * @brief Extracts the frustum planes.
278 * @param[in] bufferIndex The current update buffer index.
279 * @param[in] normalize will normalize plane equation coefficients by default.
281 void UpdateFrustum( BufferIndex updateBufferIndex, bool normalize = true );
284 * Adjust near plane for reflection
285 * @param perspective Perspective matrix
286 * @param clipPlane Clipping plane
288 void AdjustNearPlaneForPerspective( Matrix& perspective, const Vector4& clipPlane );
290 uint32_t mUpdateViewFlag; ///< This is non-zero if the view matrix requires an update
291 uint32_t mUpdateProjectionFlag; ///< This is non-zero if the projection matrix requires an update
292 const Node* mNode; ///< The node this scene graph camera belongs to
294 public: // PROPERTIES
295 Dali::Camera::Type mType; // Non-animatable
296 Dali::Camera::ProjectionMode mProjectionMode; // Non-animatable
297 bool mInvertYAxis; // Non-animatable
301 float mLeftClippingPlane;
302 float mRightClippingPlane;
303 float mTopClippingPlane;
304 float mBottomClippingPlane;
305 float mNearClippingPlane;
306 float mFarClippingPlane;
307 Vector3 mTargetPosition;
309 Dali::Matrix mReflectionMtx;
310 Dali::Vector4 mReflectionPlane;
311 Dali::Vector4 mReflectionEye;
312 bool mUseReflection{ false };
313 bool mUseReflectionClip{ false };
315 InheritedMatrix mViewMatrix; ///< The viewMatrix; this is double buffered for input handling.
316 InheritedMatrix mProjectionMatrix; ///< The projectionMatrix; this is double buffered for input handling.
318 DoubleBuffered< FrustumPlanes > mFrustum; ///< Clipping frustum; double buffered for input handling
319 DoubleBuffered< Matrix > mInverseViewProjection; ///< Inverted viewprojection; double buffered for input handling
323 // Messages for Camera
325 inline void SetTypeMessage( EventThreadServices& eventThreadServices, const Camera& camera, Dali::Camera::Type parameter )
327 typedef MessageValue1< Camera, Dali::Camera::Type > LocalType;
329 // Reserve some memory inside the message queue
330 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
332 // Construct message in the message queue memory; note that delete should not be called on the return value
333 new (slot) LocalType( &camera, &Camera::SetType, parameter );
336 inline void SetProjectionModeMessage( EventThreadServices& eventThreadServices, const Camera& camera, Dali::Camera::ProjectionMode parameter )
338 typedef MessageValue1< Camera, Dali::Camera::ProjectionMode > LocalProjectionMode;
340 // Reserve some memory inside the message queue
341 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalProjectionMode ) );
343 // Construct message in the message queue memory; note that delete should not be called on the return value
344 new (slot) LocalProjectionMode( &camera, &Camera::SetProjectionMode, parameter );
347 inline void SetFieldOfViewMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
349 typedef MessageValue1< Camera, float > LocalType;
351 // Reserve some memory inside the message queue
352 uint32_t* 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( &camera, &Camera::SetFieldOfView, parameter );
358 inline void SetAspectRatioMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
360 typedef MessageValue1< Camera, float > LocalType;
362 // Reserve some memory inside the message queue
363 uint32_t* 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( &camera, &Camera::SetAspectRatio, parameter );
369 inline void SetLeftClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
371 typedef MessageValue1< Camera, float > LocalType;
373 // Reserve some memory inside the message queue
374 uint32_t* 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( &camera, &Camera::SetLeftClippingPlane, parameter );
380 inline void SetRightClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
382 typedef MessageValue1< Camera, float > LocalType;
384 // Reserve some memory inside the message queue
385 uint32_t* 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( &camera, &Camera::SetRightClippingPlane, parameter );
391 inline void SetTopClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
393 typedef MessageValue1< Camera, float > LocalType;
395 // Reserve some memory inside the message queue
396 uint32_t* 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( &camera, &Camera::SetTopClippingPlane, parameter );
402 inline void SetBottomClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
404 typedef MessageValue1< Camera, float > LocalType;
406 // Reserve some memory inside the message queue
407 uint32_t* 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( &camera, &Camera::SetBottomClippingPlane, parameter );
413 inline void SetNearClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
415 typedef MessageValue1< Camera, float > LocalType;
417 // Reserve some memory inside the message queue
418 uint32_t* 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( &camera, &Camera::SetNearClippingPlane, parameter );
424 inline void SetFarClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
426 typedef MessageValue1< Camera, float > LocalType;
428 // Reserve some memory inside the message queue
429 uint32_t* 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( &camera, &Camera::SetFarClippingPlane, parameter );
435 inline void SetTargetPositionMessage( EventThreadServices& eventThreadServices, const Camera& camera, const Vector3& parameter )
437 typedef MessageValue1< Camera, Vector3 > LocalType;
439 // Reserve some memory inside the message queue
440 uint32_t* 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( &camera, &Camera::SetTargetPosition, parameter );
446 inline void SetInvertYAxisMessage( EventThreadServices& eventThreadServices, const Camera& camera, bool parameter )
448 typedef MessageValue1< Camera, bool > LocalType;
450 // Reserve some memory inside the message queue
451 uint32_t* 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( &camera, &Camera::SetInvertYAxis, parameter );
457 } // namespace SceneGraph
459 } // namespace Internal
463 #endif // DALI_INTERNAL_SCENE_GRAPH_CAMERA_H