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 * Retrieve the view-matrix; this is double buffered for input handling.
164 * @param[in] bufferIndex The buffer to read from.
165 * @return The view-matrix.
167 const Matrix& GetViewMatrix( BufferIndex bufferIndex ) const;
170 * @brief Check to see if a sphere lies within the view frustum.
172 * @param bufferIndex The buffer to read from.
173 * @param origin The world position center of the sphere to check.
174 * @param radius The length of the sphere radius in world scale.
176 * @return false if the sphere lies outside of the frustum.
178 bool CheckSphereInFrustum( BufferIndex bufferIndex, const Vector3& origin, float radius );
181 * @brief Check to see if a bounding box lies within the view frustum.
183 * @param bufferIndex The buffer to read from.
184 * @param origin the world position center of the cubeoid to check.
185 * @param halfExtents The half length of the cubeoid in world co-ordinates in each axis.
187 * @return false if the cubeoid lies completely outside of the frustum, true otherwise
189 bool CheckAABBInFrustum( BufferIndex bufferIndex, const Vector3& origin, const Vector3& halfExtents );
192 * Retrieve the projection-matrix; this is double buffered for input handling.
193 * @param[in] bufferIndex The buffer to read from.
194 * @return The projection-matrix.
196 const Matrix& GetProjectionMatrix( BufferIndex bufferIndex ) const;
199 * Retrieve the inverted view-projection-matrix; this is double buffered for input handling.
200 * @param[in] bufferIndex The buffer to read from.
201 * @return The inverse view-projection-matrix.
203 const Matrix& GetInverseViewProjectionMatrix( BufferIndex bufferIndex ) const;
206 * Retrieve the projection-matrix property querying interface.
207 * @pre The camera is on-stage.
208 * @return The projection-matrix property querying interface.
210 const PropertyInputImpl* GetProjectionMatrix() const;
213 * Retrieve the viewMatrix property querying interface.
214 * @pre The camera is on-stage.
215 * @return The viewMatrix property querying interface.
217 const PropertyInputImpl* GetViewMatrix() const;
220 * Updates view and projection matrices.
221 * Called by the render task using the camera
222 * @param[in] updateBufferIndex The buffer to read from.
224 void Update( BufferIndex updateBufferIndex );
227 * @return true if the view matrix of camera is updated this or the previous frame
229 bool ViewMatrixUpdated();
240 Camera(const Camera&);
242 Camera& operator=(const Camera& rhs);
245 * Recalculates the view matrix.
246 * @param[in] bufferIndex The current update buffer index.
247 * @return count how many frames ago the matrix was changed.
249 uint32_t UpdateViewMatrix( BufferIndex updateBufferIndex );
252 * Recalculates the projection matrix.
253 * @param[in] bufferIndex The current update buffer index.
254 * @return count how many frames ago the matrix was changed.
256 uint32_t UpdateProjection( BufferIndex updateBufferIndex );
261 * @brief Extracts the frustum planes.
263 * @param[in] bufferIndex The current update buffer index.
264 * @param[in] normalize will normalize plane equation coefficients by default.
266 void UpdateFrustum( BufferIndex updateBufferIndex, bool normalize = true );
268 uint32_t mUpdateViewFlag; ///< This is non-zero if the view matrix requires an update
269 uint32_t mUpdateProjectionFlag; ///< This is non-zero if the projection matrix requires an update
270 const Node* mNode; ///< The node this scene graph camera belongs to
272 public: // PROPERTIES
273 Dali::Camera::Type mType; // Non-animatable
274 Dali::Camera::ProjectionMode mProjectionMode; // Non-animatable
275 bool mInvertYAxis; // Non-animatable
279 float mLeftClippingPlane;
280 float mRightClippingPlane;
281 float mTopClippingPlane;
282 float mBottomClippingPlane;
283 float mNearClippingPlane;
284 float mFarClippingPlane;
285 Vector3 mTargetPosition;
287 InheritedMatrix mViewMatrix; ///< The viewMatrix; this is double buffered for input handling.
288 InheritedMatrix mProjectionMatrix; ///< The projectionMatrix; this is double buffered for input handling.
290 DoubleBuffered< FrustumPlanes > mFrustum; ///< Clipping frustum; double buffered for input handling
291 DoubleBuffered< Matrix > mInverseViewProjection; ///< Inverted viewprojection; double buffered for input handling
295 // Messages for Camera
297 inline void SetTypeMessage( EventThreadServices& eventThreadServices, const Camera& camera, Dali::Camera::Type parameter )
299 typedef MessageValue1< Camera, Dali::Camera::Type > LocalType;
301 // Reserve some memory inside the message queue
302 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
304 // Construct message in the message queue memory; note that delete should not be called on the return value
305 new (slot) LocalType( &camera, &Camera::SetType, parameter );
308 inline void SetProjectionModeMessage( EventThreadServices& eventThreadServices, const Camera& camera, Dali::Camera::ProjectionMode parameter )
310 typedef MessageValue1< Camera, Dali::Camera::ProjectionMode > LocalProjectionMode;
312 // Reserve some memory inside the message queue
313 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalProjectionMode ) );
315 // Construct message in the message queue memory; note that delete should not be called on the return value
316 new (slot) LocalProjectionMode( &camera, &Camera::SetProjectionMode, parameter );
319 inline void SetFieldOfViewMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
321 typedef MessageValue1< Camera, float > LocalType;
323 // Reserve some memory inside the message queue
324 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
326 // Construct message in the message queue memory; note that delete should not be called on the return value
327 new (slot) LocalType( &camera, &Camera::SetFieldOfView, parameter );
330 inline void SetAspectRatioMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
332 typedef MessageValue1< Camera, float > LocalType;
334 // Reserve some memory inside the message queue
335 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
337 // Construct message in the message queue memory; note that delete should not be called on the return value
338 new (slot) LocalType( &camera, &Camera::SetAspectRatio, parameter );
341 inline void SetLeftClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
343 typedef MessageValue1< Camera, float > LocalType;
345 // Reserve some memory inside the message queue
346 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
348 // Construct message in the message queue memory; note that delete should not be called on the return value
349 new (slot) LocalType( &camera, &Camera::SetLeftClippingPlane, parameter );
352 inline void SetRightClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
354 typedef MessageValue1< Camera, float > LocalType;
356 // Reserve some memory inside the message queue
357 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
359 // Construct message in the message queue memory; note that delete should not be called on the return value
360 new (slot) LocalType( &camera, &Camera::SetRightClippingPlane, parameter );
363 inline void SetTopClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
365 typedef MessageValue1< Camera, float > LocalType;
367 // Reserve some memory inside the message queue
368 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
370 // Construct message in the message queue memory; note that delete should not be called on the return value
371 new (slot) LocalType( &camera, &Camera::SetTopClippingPlane, parameter );
374 inline void SetBottomClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
376 typedef MessageValue1< Camera, float > LocalType;
378 // Reserve some memory inside the message queue
379 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
381 // Construct message in the message queue memory; note that delete should not be called on the return value
382 new (slot) LocalType( &camera, &Camera::SetBottomClippingPlane, parameter );
385 inline void SetNearClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
387 typedef MessageValue1< Camera, float > LocalType;
389 // Reserve some memory inside the message queue
390 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
392 // Construct message in the message queue memory; note that delete should not be called on the return value
393 new (slot) LocalType( &camera, &Camera::SetNearClippingPlane, parameter );
396 inline void SetFarClippingPlaneMessage( EventThreadServices& eventThreadServices, const Camera& camera, float parameter )
398 typedef MessageValue1< Camera, float > LocalType;
400 // Reserve some memory inside the message queue
401 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
403 // Construct message in the message queue memory; note that delete should not be called on the return value
404 new (slot) LocalType( &camera, &Camera::SetFarClippingPlane, parameter );
407 inline void SetTargetPositionMessage( EventThreadServices& eventThreadServices, const Camera& camera, const Vector3& parameter )
409 typedef MessageValue1< Camera, Vector3 > LocalType;
411 // Reserve some memory inside the message queue
412 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
414 // Construct message in the message queue memory; note that delete should not be called on the return value
415 new (slot) LocalType( &camera, &Camera::SetTargetPosition, parameter );
418 inline void SetInvertYAxisMessage( EventThreadServices& eventThreadServices, const Camera& camera, bool parameter )
420 typedef MessageValue1< Camera, bool > LocalType;
422 // Reserve some memory inside the message queue
423 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
425 // Construct message in the message queue memory; note that delete should not be called on the return value
426 new (slot) LocalType( &camera, &Camera::SetInvertYAxis, parameter );
429 } // namespace SceneGraph
431 } // namespace Internal
435 #endif // DALI_INTERNAL_SCENE_GRAPH_CAMERA_H