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 * Returns whether the Y axis is inverted.
120 * @return True if the Y axis is inverted, false otherwise.
122 bool IsYAxisInverted() const
128 * @copydoc Dali::Internal::CameraActor::SetProjectionMode
130 void SetProjectionMode( Dali::Camera::ProjectionMode projectionMode );
133 * @copydoc Dali::Internal::CameraActor::SetFieldOfView
135 void SetFieldOfView( float fieldOfView );
138 * @copydoc Dali::Internal::CameraActor::SetAspectRatio
140 void SetAspectRatio( float aspectRatio );
143 * @copydoc Dali::Internal::CameraActor::SetLeftClippingPlane
145 void SetLeftClippingPlane( float leftClippingPlane );
148 * @copydoc Dali::Internal::CameraActor::SetRightClippingPlane
150 void SetRightClippingPlane( float rightClippingPlane );
153 * @copydoc Dali::Internal::CameraActor::SetTopClippingPlane
155 void SetTopClippingPlane( float topClippingPlane );
158 * @copydoc Dali::Internal::CameraActor::SetBottomClippingPlane
160 void SetBottomClippingPlane( float bottomClippingPlane );
163 * @copydoc Dali::Internal::CameraActor::SetNearClippingPlane
165 void SetNearClippingPlane( float nearClippingPlane );
168 * @copydoc Dali::Internal::CameraActor::SetFarClippingPlane
170 void SetFarClippingPlane( float farClippingPlane );
173 * @copydoc Dali::Internal::CameraActor::RotateProjection
175 void RotateProjection(int rotationAngle);
178 * @copydoc Dali::Internal::CameraActor::SetTarget
180 void SetTargetPosition( const Vector3& targetPosition );
183 * Sets the reflection plane
184 * @param[in] plane reflection plane
186 void SetReflectByPlane( const Vector4& plane );
189 * Tests whether reflection is used
190 * @return True if used, False otherwise
192 bool GetReflectionUsed() const
194 return mUseReflection;
198 * Retrieve the view-matrix; this is double buffered for input handling.
199 * @param[in] bufferIndex The buffer to read from.
200 * @return The view-matrix.
202 const Matrix& GetViewMatrix( BufferIndex bufferIndex ) const;
205 * @brief Check to see if a sphere lies within the view frustum.
207 * @param bufferIndex The buffer to read from.
208 * @param origin The world position center of the sphere to check.
209 * @param radius The length of the sphere radius in world scale.
211 * @return false if the sphere lies outside of the frustum.
213 bool CheckSphereInFrustum( BufferIndex bufferIndex, const Vector3& origin, float radius );
216 * @brief Check to see if a bounding box lies within the view frustum.
218 * @param bufferIndex The buffer to read from.
219 * @param origin the world position center of the cubeoid to check.
220 * @param halfExtents The half length of the cubeoid in world co-ordinates in each axis.
222 * @return false if the cubeoid lies completely outside of the frustum, true otherwise
224 bool CheckAABBInFrustum( BufferIndex bufferIndex, const Vector3& origin, const Vector3& halfExtents );
227 * Retrieve the projection-matrix; this is double buffered for input handling.
228 * @param[in] bufferIndex The buffer to read from.
229 * @return The projection-matrix.
231 const Matrix& GetProjectionMatrix( BufferIndex bufferIndex ) const;
234 * Retrieve the inverted view-projection-matrix; this is double buffered for input handling.
235 * @param[in] bufferIndex The buffer to read from.
236 * @return The inverse view-projection-matrix.
238 const Matrix& GetInverseViewProjectionMatrix( BufferIndex bufferIndex ) const;
241 * Retrieve the final projection-matrix; this is double buffered for input handling.
242 * @param[in] bufferIndex The buffer to read from.
243 * @return The projection-matrix that should be used to render.
245 const Matrix& GetFinalProjectionMatrix(BufferIndex bufferIndex) const;
248 * Retrieve the projection-matrix property querying interface.
249 * @pre The camera is on-stage.
250 * @return The projection-matrix property querying interface.
252 const PropertyInputImpl* GetProjectionMatrix() const;
255 * Retrieve the viewMatrix property querying interface.
256 * @pre The camera is on-stage.
257 * @return The viewMatrix property querying interface.
259 const PropertyInputImpl* GetViewMatrix() const;
262 * Updates view and projection matrices.
263 * Called by the render task using the camera
264 * @param[in] updateBufferIndex The buffer to read from.
266 void Update( BufferIndex updateBufferIndex );
269 * @return true if the view matrix of camera is updated this or the previous frame
271 bool ViewMatrixUpdated();
282 Camera(const Camera&);
284 Camera& operator=(const Camera& rhs);
287 * Recalculates the view matrix.
288 * @param[in] bufferIndex The current update buffer index.
289 * @return count how many frames ago the matrix was changed.
291 uint32_t UpdateViewMatrix( BufferIndex updateBufferIndex );
294 * Recalculates the projection matrix.
295 * @param[in] bufferIndex The current update buffer index.
296 * @return count how many frames ago the matrix was changed.
298 uint32_t UpdateProjection( BufferIndex updateBufferIndex );
303 * @brief Extracts the frustum planes.
305 * @param[in] bufferIndex The current update buffer index.
306 * @param[in] normalize will normalize plane equation coefficients by default.
308 void UpdateFrustum( BufferIndex updateBufferIndex, bool normalize = true );
311 * Adjust near plane for reflection
312 * @param perspective Perspective matrix
313 * @param clipPlane Clipping plane
315 void AdjustNearPlaneForPerspective( Matrix& perspective, const Vector4& clipPlane );
317 uint32_t mUpdateViewFlag; ///< This is non-zero if the view matrix requires an update
318 uint32_t mUpdateProjectionFlag; ///< This is non-zero if the projection matrix requires an update
319 int mProjectionRotation; ///< The rotaion angle of the projection
320 const Node* mNode; ///< The node this scene graph camera belongs to
322 public: // PROPERTIES
323 Dali::Camera::Type mType; // Non-animatable
324 Dali::Camera::ProjectionMode mProjectionMode; // Non-animatable
325 bool mInvertYAxis; // Non-animatable
329 float mLeftClippingPlane;
330 float mRightClippingPlane;
331 float mTopClippingPlane;
332 float mBottomClippingPlane;
333 float mNearClippingPlane;
334 float mFarClippingPlane;
335 Vector3 mTargetPosition;
337 Dali::Matrix mReflectionMtx;
338 Dali::Vector4 mReflectionPlane;
339 Dali::Vector4 mReflectionEye;
340 bool mUseReflection{ false };
341 bool mUseReflectionClip{ false };
343 InheritedMatrix mViewMatrix; ///< The viewMatrix; this is double buffered for input handling.
344 InheritedMatrix mProjectionMatrix; ///< The projectionMatrix; this is double buffered for input handling.
346 DoubleBuffered< FrustumPlanes > mFrustum; ///< Clipping frustum; double buffered for input handling
347 DoubleBuffered< Matrix > mInverseViewProjection; ///< Inverted viewprojection; double buffered for input handling
348 DoubleBuffered< Matrix > mFinalProjection; ///< Final projection matrix; double buffered for input handling
352 // Messages for Camera
354 inline void SetTypeMessage( EventThreadServices& eventThreadServices, const Camera& camera, Dali::Camera::Type parameter )
356 using LocalType = MessageValue1<Camera, Dali::Camera::Type>;
358 // Reserve some memory inside the message queue
359 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
361 // Construct message in the message queue memory; note that delete should not be called on the return value
362 new (slot) LocalType( &camera, &Camera::SetType, parameter );
365 inline void SetProjectionModeMessage( EventThreadServices& eventThreadServices, const Camera& camera, Dali::Camera::ProjectionMode parameter )
367 using LocalProjectionMode = MessageValue1<Camera, Dali::Camera::ProjectionMode>;
369 // Reserve some memory inside the message queue
370 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalProjectionMode ) );
372 // Construct message in the message queue memory; note that delete should not be called on the return value
373 new (slot) LocalProjectionMode( &camera, &Camera::SetProjectionMode, parameter );
376 inline void SetFieldOfViewMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
378 using LocalType = MessageValue1<Camera, float>;
380 // Reserve some memory inside the message queue
381 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
383 // Construct message in the message queue memory; note that delete should not be called on the return value
384 new (slot) LocalType( &camera, &Camera::SetFieldOfView, parameter );
387 inline void SetAspectRatioMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
389 using LocalType = MessageValue1<Camera, float>;
391 // Reserve some memory inside the message queue
392 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
394 // Construct message in the message queue memory; note that delete should not be called on the return value
395 new (slot) LocalType( &camera, &Camera::SetAspectRatio, parameter );
398 inline void SetLeftClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
400 using LocalType = MessageValue1<Camera, float>;
402 // Reserve some memory inside the message queue
403 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
405 // Construct message in the message queue memory; note that delete should not be called on the return value
406 new (slot) LocalType( &camera, &Camera::SetLeftClippingPlane, parameter );
409 inline void SetRightClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
411 using LocalType = MessageValue1<Camera, float>;
413 // Reserve some memory inside the message queue
414 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
416 // Construct message in the message queue memory; note that delete should not be called on the return value
417 new (slot) LocalType( &camera, &Camera::SetRightClippingPlane, parameter );
420 inline void SetTopClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
422 using LocalType = MessageValue1<Camera, float>;
424 // Reserve some memory inside the message queue
425 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
427 // Construct message in the message queue memory; note that delete should not be called on the return value
428 new (slot) LocalType( &camera, &Camera::SetTopClippingPlane, parameter );
431 inline void SetBottomClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
433 using LocalType = MessageValue1<Camera, float>;
435 // Reserve some memory inside the message queue
436 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
438 // Construct message in the message queue memory; note that delete should not be called on the return value
439 new (slot) LocalType( &camera, &Camera::SetBottomClippingPlane, parameter );
442 inline void SetNearClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
444 using LocalType = MessageValue1<Camera, float>;
446 // Reserve some memory inside the message queue
447 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
449 // Construct message in the message queue memory; note that delete should not be called on the return value
450 new (slot) LocalType( &camera, &Camera::SetNearClippingPlane, parameter );
453 inline void SetFarClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
455 using LocalType = MessageValue1<Camera, float>;
457 // Reserve some memory inside the message queue
458 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
460 // Construct message in the message queue memory; note that delete should not be called on the return value
461 new (slot) LocalType( &camera, &Camera::SetFarClippingPlane, parameter );
464 inline void SetTargetPositionMessage( EventThreadServices& eventThreadServices, const Camera& camera, const Vector3& parameter )
466 using LocalType = MessageValue1<Camera, Vector3>;
468 // Reserve some memory inside the message queue
469 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
471 // Construct message in the message queue memory; note that delete should not be called on the return value
472 new (slot) LocalType( &camera, &Camera::SetTargetPosition, parameter );
475 inline void SetInvertYAxisMessage( EventThreadServices& eventThreadServices, const Camera& camera, bool parameter )
477 using LocalType = MessageValue1<Camera, bool>;
479 // Reserve some memory inside the message queue
480 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
482 // Construct message in the message queue memory; note that delete should not be called on the return value
483 new (slot) LocalType( &camera, &Camera::SetInvertYAxis, parameter );
486 inline void RotateProjectionMessage( EventThreadServices& eventThreadServices, const Camera& camera, int parameter )
488 typedef MessageValue1< Camera, int > LocalType;
490 // Reserve some memory inside the message queue
491 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
493 // Construct message in the message queue memory; note that delete should not be called on the return value
494 new (slot) LocalType( &camera, &Camera::RotateProjection, parameter );
497 } // namespace SceneGraph
499 } // namespace Internal
503 #endif // DALI_INTERNAL_SCENE_GRAPH_CAMERA_H