1 #ifndef DALI_INTERNAL_SCENE_GRAPH_CAMERA_H
2 #define DALI_INTERNAL_SCENE_GRAPH_CAMERA_H
5 * Copyright (c) 2023 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/devel-api/actors/camera-actor-devel.h>
23 #include <dali/internal/update/common/animatable-property.h>
24 #include <dali/internal/update/common/double-buffered.h>
25 #include <dali/internal/update/common/inherited-property.h>
26 #include <dali/internal/update/nodes/node.h>
27 #include <dali/public-api/actors/camera-actor.h>
28 #include <dali/public-api/math/rect.h>
36 class SceneController;
39 * Scene-graph camera object
41 class Camera : public Node
44 static const Dali::Camera::Type DEFAULT_TYPE;
45 static const Dali::Camera::ProjectionMode DEFAULT_MODE;
46 static const Dali::DevelCameraActor::ProjectionDirection DEFAULT_PROJECTION_DIRECTION;
47 static const bool DEFAULT_INVERT_Y_AXIS;
48 static const float DEFAULT_FIELD_OF_VIEW;
49 static const float DEFAULT_ORTHOGRAPHIC_SIZE;
50 static const float DEFAULT_ASPECT_RATIO;
51 static const float DEFAULT_LEFT_CLIPPING_PLANE;
52 static const float DEFAULT_RIGHT_CLIPPING_PLANE;
53 static const float DEFAULT_TOP_CLIPPING_PLANE;
54 static const float DEFAULT_BOTTOM_CLIPPING_PLANE;
55 static const float DEFAULT_NEAR_CLIPPING_PLANE;
56 static const float DEFAULT_FAR_CLIPPING_PLANE;
57 static const Vector2 DEFAULT_STEREO_BIAS;
58 static const Vector3 DEFAULT_TARGET_POSITION;
61 * Plane equation container for a plane of the view frustum
70 * @brief Container for six planes in a view frustum
79 * Construct a new Camera.
80 * @return a new camera.
90 * Overriden delete operator
91 * Deletes the camera from its global memory pool
93 void operator delete(void* ptr);
96 * @copydoc Dali::Internal::CameraActor::SetType
98 void SetType(Dali::Camera::Type type);
101 * @copydoc Dali::Internal::CameraActor::SetInvertYAxis
103 void SetInvertYAxis(bool invertYAxis);
106 * Returns whether the Y axis is inverted.
107 * @return True if the Y axis is inverted, false otherwise.
109 bool IsYAxisInverted() const
115 * @copydoc Dali::Internal::CameraActor::SetProjectionMode
117 void SetProjectionMode(Dali::Camera::ProjectionMode projectionMode);
120 * @copydoc Dali::Internal::CameraActor::SetProjectionDirection
122 void SetProjectionDirection(Dali::DevelCameraActor::ProjectionDirection direction);
125 * @copydoc Dali::Internal::CameraActor::SetLeftClippingPlane
127 void SetLeftClippingPlane(float leftClippingPlane);
130 * @copydoc Dali::Internal::CameraActor::SetRightClippingPlane
132 void SetRightClippingPlane(float rightClippingPlane);
135 * @copydoc Dali::Internal::CameraActor::SetTopClippingPlane
137 void SetTopClippingPlane(float topClippingPlane);
140 * @copydoc Dali::Internal::CameraActor::SetBottomClippingPlane
142 void SetBottomClippingPlane(float bottomClippingPlane);
145 * @copydoc Dali::Internal::CameraActor::SetNearClippingPlane
147 void SetNearClippingPlane(float nearClippingPlane);
150 * @copydoc Dali::Internal::CameraActor::SetFarClippingPlane
152 void SetFarClippingPlane(float farClippingPlane);
155 * @copydoc Dali::Internal::CameraActor::RotateProjection
157 void RotateProjection(int rotationAngle);
160 * @copydoc Dali::Internal::CameraActor::SetTarget
162 void SetTargetPosition(const Vector3& targetPosition);
165 * @brief Bakes the field of view.
166 * @param[in] updateBufferIndex The current update buffer index.
167 * @param[in] fieldOfView The field of view.
169 void BakeFieldOfView(BufferIndex updateBufferIndex, float fieldOfView);
172 * @brief Retrieve the field of view.
173 * @param[in] bufferIndex The buffer to read from.
174 * @return The field of view.
176 float GetFieldOfView(BufferIndex bufferIndex) const
178 return mFieldOfView[bufferIndex];
182 * @brief Bakes the orthographic size.
183 * @param[in] updateBufferIndex The current update buffer index.
184 * @param[in] orthographicSize The orthographic size.
186 void BakeOrthographicSize(BufferIndex updateBufferIndex, float orthographicSize);
189 * @brief Retrieve the orthographic size.
190 * @param[in] bufferIndex The buffer to read from.
191 * @return The orthographic size.
193 float GetOrthographicSize(BufferIndex bufferIndex) const
195 return mOrthographicSize[bufferIndex];
199 * @brief Bakes the aspect ratio.
200 * @param[in] updateBufferIndex The current update buffer index.
201 * @param[in] aspectRatio The aspect ratio.
203 void BakeAspectRatio(BufferIndex updateBufferIndex, float aspectRatio);
206 * @brief Retrieve the aspect ratio.
207 * @param[in] bufferIndex The buffer to read from.
208 * @return The aspect ratio.
210 float GetAspectRatio(BufferIndex bufferIndex) const
212 return mAspectRatio[bufferIndex];
216 * Sets the reflection plane
217 * @param[in] plane reflection plane
219 void SetReflectByPlane(const Vector4& plane);
222 * Tests whether reflection is used
223 * @return True if used, False otherwise
225 bool GetReflectionUsed() const
227 return mUseReflection;
231 * Retrieve the view-matrix; this is double buffered for input handling.
232 * @param[in] bufferIndex The buffer to read from.
233 * @return The view-matrix.
235 const Matrix& GetViewMatrix(BufferIndex bufferIndex) const;
238 * @brief Check to see if a sphere lies within the view frustum.
240 * @param bufferIndex The buffer to read from.
241 * @param origin The world position center of the sphere to check.
242 * @param radius The length of the sphere radius in world scale.
244 * @return false if the sphere lies outside of the frustum.
246 bool CheckSphereInFrustum(BufferIndex bufferIndex, const Vector3& origin, float radius) const;
249 * @brief Check to see if a bounding box lies within the view frustum.
251 * @param bufferIndex The buffer to read from.
252 * @param origin the world position center of the cubeoid to check.
253 * @param halfExtents The half length of the cubeoid in world co-ordinates in each axis.
255 * @return false if the cubeoid lies completely outside of the frustum, true otherwise
257 bool CheckAABBInFrustum(BufferIndex bufferIndex, const Vector3& origin, const Vector3& halfExtents) const;
260 * @brief Calculate orthographic clipping box by this camera's orthographic size.
262 Dali::Rect<int32_t> GetOrthographicClippingBox(BufferIndex bufferIndex) const;
265 * Retrieve the projection-matrix; this is double buffered for input handling.
266 * @param[in] bufferIndex The buffer to read from.
267 * @return The projection-matrix.
269 const Matrix& GetProjectionMatrix(BufferIndex bufferIndex) const;
272 * Retrieve the inverted view-projection-matrix; this is double buffered for input handling.
273 * @param[in] bufferIndex The buffer to read from.
274 * @return The inverse view-projection-matrix.
276 const Matrix& GetInverseViewProjectionMatrix(BufferIndex bufferIndex) const;
279 * Retrieve the final projection-matrix; this is double buffered for input handling.
280 * @param[in] bufferIndex The buffer to read from.
281 * @return The projection-matrix that should be used to render.
283 const Matrix& GetFinalProjectionMatrix(BufferIndex bufferIndex) const;
286 * Retrieve the field of view property querying interface.
287 * @pre The camera is on-stage.
288 * @return The field of view property querying interface.
290 const PropertyBase* GetFieldOfView() const;
293 * Retrieve the orthographic size property querying interface.
294 * @pre The camera is on-stage.
295 * @return The orthographic size property querying interface.
297 const PropertyBase* GetOrthographicSize() const;
300 * Retrieve the aspect ratio property querying interface.
301 * @pre The camera is on-stage.
302 * @return The aspect ratio property querying interface.
304 const PropertyBase* GetAspectRatio() const;
307 * Retrieve the projection-matrix property querying interface.
308 * @pre The camera is on-stage.
309 * @return The projection-matrix property querying interface.
311 const PropertyInputImpl* GetProjectionMatrix() const;
314 * Retrieve the viewMatrix property querying interface.
315 * @pre The camera is on-stage.
316 * @return The viewMatrix property querying interface.
318 const PropertyInputImpl* GetViewMatrix() const;
321 * Updates view and projection matrices.
322 * Called by the render task using the camera
323 * @param[in] updateBufferIndex The buffer to read from.
325 void Update(BufferIndex updateBufferIndex);
328 * @return true if the view matrix of camera is updated this or the previous frame
330 bool ViewMatrixUpdated() const;
333 * @return true if the projection matrix projection matrix relative properties are animated this or the previous frame
335 bool IsProjectionMatrixAnimated() const;
343 // Delete copy and move
344 Camera(const Camera&) = delete;
345 Camera(Camera&&) = delete;
346 Camera& operator=(const Camera& rhs) = delete;
347 Camera& operator=(Camera&& rhs) = delete;
350 * Recalculates the view matrix.
351 * @param[in] bufferIndex The current update buffer index.
352 * @return count how many frames ago the matrix was changed.
354 uint32_t UpdateViewMatrix(BufferIndex updateBufferIndex);
357 * Recalculates the projection matrix.
358 * @param[in] bufferIndex The current update buffer index.
359 * @return count how many frames ago the matrix was changed.
361 uint32_t UpdateProjection(BufferIndex updateBufferIndex);
365 * @brief Extracts the frustum planes.
367 * @param[in] bufferIndex The current update buffer index.
368 * @param[in] normalize will normalize plane equation coefficients by default.
370 void UpdateFrustum(BufferIndex updateBufferIndex, bool normalize = true);
372 uint32_t mUpdateViewFlag; ///< This is non-zero if the view matrix requires an update
373 uint32_t mUpdateProjectionFlag; ///< This is non-zero if the projection matrix requires an update
374 int mProjectionRotation; ///< The rotaion angle of the projection
376 public: // PROPERTIES
377 Dali::Camera::Type mType; // Non-animatable
378 Dali::Camera::ProjectionMode mProjectionMode; // Non-animatable
379 Dali::DevelCameraActor::ProjectionDirection mProjectionDirection; // Non-animatable
380 bool mInvertYAxis; // Non-animatable
382 AnimatableProperty<float> mFieldOfView; // Animatable
383 AnimatableProperty<float> mOrthographicSize; // Animatable
384 AnimatableProperty<float> mAspectRatio; // Animatable
386 float mNearClippingPlane;
387 float mFarClippingPlane;
388 Vector3 mTargetPosition;
390 Dali::Matrix mReflectionMtx;
391 Dali::Vector4 mReflectionPlane;
392 Dali::Vector4 mReflectionEye;
393 bool mUseReflection{false};
394 bool mUseReflectionClip{false};
396 InheritedMatrix mViewMatrix; ///< The viewMatrix; this is double buffered for input handling.
397 InheritedMatrix mProjectionMatrix; ///< The projectionMatrix; this is double buffered for input handling.
399 DoubleBuffered<FrustumPlanes> mFrustum; ///< Clipping frustum; double buffered for input handling
400 DoubleBuffered<Matrix> mInverseViewProjection; ///< Inverted viewprojection; double buffered for input handling
401 DoubleBuffered<Matrix> mFinalProjection; ///< Final projection matrix; double buffered for input handling
404 } // namespace SceneGraph
405 } // namespace Internal
408 #endif // DALI_INTERNAL_SCENE_GRAPH_CAMERA_H