2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali/internal/update/render-tasks/scene-graph-camera.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/internal/update/nodes/node.h>
27 #include <dali/public-api/common/dali-common.h>
28 #include <dali/public-api/math/math-utils.h>
30 namespace // unnamed namespace
32 const uint32_t UPDATE_COUNT = 2u; // Update projection or view matrix this many frames after a change
33 const uint32_t COPY_PREVIOUS_MATRIX = 1u; // Copy view or projection matrix from previous frame
35 //For reflection and clipping plane
36 const float REFLECTION_NORMALIZED_DEVICE_COORDINATE_PARAMETER_A = 2.0f;
37 const float REFLECTION_NORMALIZED_DEVICE_COORDINATE_PARAMETER_D = 1.0f;
51 return T(T(0) < value) - T(value < T(0));
54 void LookAt(Matrix& result, const Vector3& eye, const Vector3& target, const Vector3& up)
56 Vector3 vZ = target - eye;
59 Vector3 vX = up.Cross(vZ);
62 Vector3 vY = vZ.Cross(vX);
65 result.SetInverseTransformComponents(vX, vY, vZ, eye);
68 void Frustum(Matrix& result, float left, float right, float bottom, float top, float near, float far, bool invertYAxis)
70 float deltaZ = far - near;
71 if((near <= 0.0f) || (far <= 0.0f) || Equals(right, left) || Equals(bottom, top) || (deltaZ <= 0.0f))
73 DALI_LOG_ERROR("Invalid parameters passed into Frustum!\n");
74 DALI_ASSERT_DEBUG("Invalid parameters passed into Frustum!");
78 float deltaX = right - left;
79 float deltaY = invertYAxis ? bottom - top : top - bottom;
83 float* m = result.AsFloat();
84 m[0] = -2.0f * near / deltaX;
85 m[1] = m[2] = m[3] = 0.0f;
87 m[5] = -2.0f * near / deltaY;
88 m[4] = m[6] = m[7] = 0.0f;
90 m[8] = (right + left) / deltaX;
91 m[9] = (top + bottom) / deltaY;
92 m[10] = (near + far) / deltaZ;
95 m[14] = -2.0f * near * far / deltaZ;
96 m[12] = m[13] = m[15] = 0.0f;
99 void Perspective(Matrix& result, float fovy, float aspect, float near, float far, bool invertYAxis)
101 float frustumH = tanf(fovy * 0.5f) * near;
102 float frustumW = frustumH * aspect;
104 Frustum(result, -frustumW, frustumW, -frustumH, frustumH, near, far, invertYAxis);
107 void Orthographic(Matrix& result, float left, float right, float bottom, float top, float near, float far, bool invertYAxis)
109 if(Equals(right, left) || Equals(top, bottom) || Equals(far, near))
111 DALI_LOG_ERROR("Cannot create orthographic projection matrix with a zero dimension.\n");
112 DALI_ASSERT_DEBUG("Cannot create orthographic projection matrix with a zero dimension.");
116 float deltaX = right - left;
117 float deltaY = invertYAxis ? bottom - top : top - bottom;
118 float deltaZ = far - near;
120 float* m = result.AsFloat();
121 m[0] = -2.0f / deltaX;
127 m[5] = -2.0f / deltaY;
133 m[10] = 2.0f / deltaZ;
135 m[12] = -(right + left) / deltaX;
136 m[13] = -(top + bottom) / deltaY;
137 m[14] = -(near + far) / deltaZ;
141 } // unnamed namespace
143 const Dali::Camera::Type Camera::DEFAULT_TYPE(Dali::Camera::FREE_LOOK);
144 const Dali::Camera::ProjectionMode Camera::DEFAULT_MODE(Dali::Camera::PERSPECTIVE_PROJECTION);
145 const bool Camera::DEFAULT_INVERT_Y_AXIS(false);
146 const float Camera::DEFAULT_FIELD_OF_VIEW(45.0f * (Math::PI / 180.0f));
147 const float Camera::DEFAULT_ASPECT_RATIO(4.0f / 3.0f);
148 const float Camera::DEFAULT_LEFT_CLIPPING_PLANE(-240.0f);
149 const float Camera::DEFAULT_RIGHT_CLIPPING_PLANE(240.0f);
150 const float Camera::DEFAULT_TOP_CLIPPING_PLANE(-400.0f);
151 const float Camera::DEFAULT_BOTTOM_CLIPPING_PLANE(400.0f);
152 const float Camera::DEFAULT_NEAR_CLIPPING_PLANE(800.0f); // default height of the screen
153 const float Camera::DEFAULT_FAR_CLIPPING_PLANE(DEFAULT_NEAR_CLIPPING_PLANE + 2.f * DEFAULT_NEAR_CLIPPING_PLANE);
154 const Vector3 Camera::DEFAULT_TARGET_POSITION(0.0f, 0.0f, 0.0f);
157 : mUpdateViewFlag(UPDATE_COUNT),
158 mUpdateProjectionFlag(UPDATE_COUNT),
159 mProjectionRotation(0),
162 mProjectionMode(DEFAULT_MODE),
163 mInvertYAxis(DEFAULT_INVERT_Y_AXIS),
164 mFieldOfView(DEFAULT_FIELD_OF_VIEW),
165 mAspectRatio(DEFAULT_ASPECT_RATIO),
166 mLeftClippingPlane(DEFAULT_LEFT_CLIPPING_PLANE),
167 mRightClippingPlane(DEFAULT_RIGHT_CLIPPING_PLANE),
168 mTopClippingPlane(DEFAULT_TOP_CLIPPING_PLANE),
169 mBottomClippingPlane(DEFAULT_BOTTOM_CLIPPING_PLANE),
170 mNearClippingPlane(DEFAULT_NEAR_CLIPPING_PLANE),
171 mFarClippingPlane(DEFAULT_FAR_CLIPPING_PLANE),
172 mTargetPosition(DEFAULT_TARGET_POSITION),
175 mInverseViewProjection(Matrix::IDENTITY),
176 mFinalProjection(Matrix::IDENTITY)
180 Camera* Camera::New()
185 Camera::~Camera() = default;
187 void Camera::SetNode(const Node* node)
192 const Node* Camera::GetNode() const
197 void Camera::SetType(Dali::Camera::Type type)
202 void Camera::SetProjectionMode(Dali::Camera::ProjectionMode mode)
204 mProjectionMode = mode;
205 mUpdateProjectionFlag = UPDATE_COUNT;
208 void Camera::SetInvertYAxis(bool invertYAxis)
210 mInvertYAxis = invertYAxis;
211 mUpdateProjectionFlag = UPDATE_COUNT;
214 void Camera::SetFieldOfView(float fieldOfView)
216 mFieldOfView = fieldOfView;
217 mUpdateProjectionFlag = UPDATE_COUNT;
220 void Camera::SetAspectRatio(float aspectRatio)
222 mAspectRatio = aspectRatio;
223 mUpdateProjectionFlag = UPDATE_COUNT;
226 void Camera::SetLeftClippingPlane(float leftClippingPlane)
228 mLeftClippingPlane = leftClippingPlane;
229 mUpdateProjectionFlag = UPDATE_COUNT;
232 void Camera::SetRightClippingPlane(float rightClippingPlane)
234 mRightClippingPlane = rightClippingPlane;
235 mUpdateProjectionFlag = UPDATE_COUNT;
238 void Camera::SetTopClippingPlane(float topClippingPlane)
240 mTopClippingPlane = topClippingPlane;
241 mUpdateProjectionFlag = UPDATE_COUNT;
244 void Camera::SetBottomClippingPlane(float bottomClippingPlane)
246 mBottomClippingPlane = bottomClippingPlane;
247 mUpdateProjectionFlag = UPDATE_COUNT;
250 void Camera::SetNearClippingPlane(float nearClippingPlane)
252 mNearClippingPlane = nearClippingPlane;
253 mUpdateProjectionFlag = UPDATE_COUNT;
256 void Camera::SetFarClippingPlane(float farClippingPlane)
258 mFarClippingPlane = farClippingPlane;
259 mUpdateProjectionFlag = UPDATE_COUNT;
262 void Camera::SetTargetPosition(const Vector3& targetPosition)
264 mTargetPosition = targetPosition;
265 mUpdateViewFlag = UPDATE_COUNT;
268 void VectorReflectedByPlane(Vector4& out, Vector4& in, Vector4& plane)
270 float d = float(2.0) * plane.Dot(in);
271 out.x = static_cast<float>(in.x - plane.x * d);
272 out.y = static_cast<float>(in.y - plane.y * d);
273 out.z = static_cast<float>(in.z - plane.z * d);
274 out.w = static_cast<float>(in.w - plane.w * d);
277 void Camera::AdjustNearPlaneForPerspective(Matrix& perspective, const Vector4& clipPlane)
280 float* v = perspective.AsFloat();
282 q.x = (Sign(clipPlane.x) + v[8]) / v[0];
283 q.y = (Sign(clipPlane.y) + v[9]) / v[5];
285 q.w = (1.0f + v[10]) / v[14];
287 // Calculate the scaled plane vector
288 Vector4 c = clipPlane * (REFLECTION_NORMALIZED_DEVICE_COORDINATE_PARAMETER_A / q.Dot(clipPlane));
290 // Replace the third row of the projection v
293 v[10] = c.z + REFLECTION_NORMALIZED_DEVICE_COORDINATE_PARAMETER_D;
297 void Camera::SetReflectByPlane(const Vector4& plane)
299 float* v = mReflectionMtx.AsFloat();
300 float _2ab = -2.0f * plane.x * plane.y;
301 float _2ac = -2.0f * plane.x * plane.z;
302 float _2bc = -2.0f * plane.y * plane.z;
304 v[0] = 1.0f - 2.0f * plane.x * plane.x;
310 v[5] = 1.0f - 2.0f * plane.y * plane.y;
316 v[10] = 1.0f - 2.0f * plane.z * plane.z;
319 v[12] = -2 * plane.x * plane.w;
320 v[13] = -2 * plane.y * plane.w;
321 v[14] = -2 * plane.z * plane.w;
324 mUseReflection = true;
325 mReflectionPlane = plane;
326 mUpdateViewFlag = UPDATE_COUNT;
329 void Camera::RotateProjection(int rotationAngle)
331 mProjectionRotation = rotationAngle;
332 mUpdateViewFlag = UPDATE_COUNT;
335 const Matrix& Camera::GetProjectionMatrix(BufferIndex bufferIndex) const
337 return mProjectionMatrix[bufferIndex];
340 const Matrix& Camera::GetViewMatrix(BufferIndex bufferIndex) const
342 return mViewMatrix[bufferIndex];
345 const Matrix& Camera::GetInverseViewProjectionMatrix(BufferIndex bufferIndex) const
347 return mInverseViewProjection[bufferIndex];
350 const Matrix& Camera::GetFinalProjectionMatrix(BufferIndex bufferIndex) const
352 return mFinalProjection[bufferIndex];
355 const PropertyInputImpl* Camera::GetProjectionMatrix() const
357 return &mProjectionMatrix;
360 const PropertyInputImpl* Camera::GetViewMatrix() const
365 void Camera::Update(BufferIndex updateBufferIndex)
367 // if owning node has changes in world position we need to update camera for next 2 frames
368 if(mNode->IsLocalMatrixDirty())
370 mUpdateViewFlag = UPDATE_COUNT;
372 if(mNode->GetDirtyFlags() & NodePropertyFlags::VISIBLE)
374 // If the visibility changes, the projection matrix needs to be re-calculated.
375 // It may happen the first time an actor is rendered it's rendered only once and becomes invisible,
376 // in the following update the node will be skipped leaving the projection matrix (double buffered)
377 // with the Identity.
378 mUpdateProjectionFlag = UPDATE_COUNT;
381 // if either matrix changed, we need to recalculate the inverse matrix for hit testing to work
382 uint32_t viewUpdateCount = UpdateViewMatrix(updateBufferIndex);
383 uint32_t projectionUpdateCount = UpdateProjection(updateBufferIndex);
385 // if model or view matrix changed we need to either recalculate the inverse VP or copy previous
386 if(viewUpdateCount > COPY_PREVIOUS_MATRIX || projectionUpdateCount > COPY_PREVIOUS_MATRIX)
388 // either has actually changed so recalculate
389 Matrix::Multiply(mInverseViewProjection[updateBufferIndex], mViewMatrix[updateBufferIndex], mProjectionMatrix[updateBufferIndex]);
390 UpdateFrustum(updateBufferIndex);
392 // ignore the error, if the view projection is incorrect (non inversible) then you will have tough times anyways
393 static_cast<void>(mInverseViewProjection[updateBufferIndex].Invert());
395 else if(viewUpdateCount == COPY_PREVIOUS_MATRIX || projectionUpdateCount == COPY_PREVIOUS_MATRIX)
397 // neither has actually changed, but we might copied previous frames value so need to
398 // copy the previous inverse and frustum as well
399 mInverseViewProjection[updateBufferIndex] = mInverseViewProjection[updateBufferIndex ? 0 : 1];
400 mFrustum[updateBufferIndex] = mFrustum[updateBufferIndex ? 0 : 1];
404 bool Camera::ViewMatrixUpdated()
406 return 0u != mUpdateViewFlag;
409 uint32_t Camera::UpdateViewMatrix(BufferIndex updateBufferIndex)
411 uint32_t retval(mUpdateViewFlag);
412 if(0u != mUpdateViewFlag)
414 if(COPY_PREVIOUS_MATRIX == mUpdateViewFlag)
416 // The projection matrix was updated in the previous frame; copy it
417 mViewMatrix.CopyPrevious(updateBufferIndex);
419 else // UPDATE_COUNT == mUpdateViewFlag
423 // camera orientation taken from node - i.e. look in abitrary, unconstrained direction
424 case Dali::Camera::FREE_LOOK:
426 Matrix& viewMatrix = mViewMatrix.Get(updateBufferIndex);
427 viewMatrix = mNode->GetWorldMatrix(updateBufferIndex);
431 const Matrix& owningNodeMatrix(mNode->GetWorldMatrix(updateBufferIndex));
432 Vector3 position{}, scale{};
433 Quaternion orientation{};
434 owningNodeMatrix.GetTransformComponents(position, orientation, scale);
435 mReflectionEye = position;
436 mUseReflectionClip = true;
438 Matrix& viewMatrix = mViewMatrix.Get(updateBufferIndex);
439 Matrix oldViewMatrix(viewMatrix);
440 Matrix::Multiply(viewMatrix, oldViewMatrix, mReflectionMtx);
444 mViewMatrix.SetDirty(updateBufferIndex);
448 // camera orientation constrained to look at a target
449 case Dali::Camera::LOOK_AT_TARGET:
451 const Matrix& owningNodeMatrix(mNode->GetWorldMatrix(updateBufferIndex));
452 Vector3 position, scale;
453 Quaternion orientation;
454 owningNodeMatrix.GetTransformComponents(position, orientation, scale);
455 Matrix& viewMatrix = mViewMatrix.Get(updateBufferIndex);
459 Vector3 up = orientation.Rotate(Vector3::YAXIS);
460 Vector4 position4 = Vector4(position);
461 Vector4 target4 = Vector4(mTargetPosition);
462 Vector4 up4 = Vector4(up);
466 Vector3 positionNew3;
467 Vector3 targetNewVector3;
471 VectorReflectedByPlane(positionNew, position4, mReflectionPlane);
472 VectorReflectedByPlane(targetNew, target4, mReflectionPlane);
473 VectorReflectedByPlane(upNew, up4, mReflectionPlane);
475 positionNew3 = Vector3(positionNew);
476 targetNewVector3 = Vector3(targetNew);
477 upNew3 = Vector3(upNew);
478 LookAt(viewMatrix, positionNew3, targetNewVector3, upNew3);
480 Matrix oldViewMatrix(viewMatrix);
482 tmp.SetIdentityAndScale(Vector3(-1.0, 1.0, 1.0));
483 Matrix::Multiply(viewMatrix, oldViewMatrix, tmp);
485 mReflectionEye = positionNew;
486 mUseReflectionClip = true;
490 LookAt(viewMatrix, position, mTargetPosition, orientation.Rotate(Vector3::YAXIS));
492 mViewMatrix.SetDirty(updateBufferIndex);
502 void Camera::UpdateFrustum(BufferIndex updateBufferIndex, bool normalize)
504 // Extract the clip matrix planes
506 Matrix::Multiply(clipMatrix, mViewMatrix[updateBufferIndex], mProjectionMatrix[updateBufferIndex]);
508 const float* cm = clipMatrix.AsFloat();
509 FrustumPlanes& planes = mFrustum[updateBufferIndex];
512 planes.mPlanes[0].mNormal.x = cm[3] + cm[0]; // column 4 + column 1
513 planes.mPlanes[0].mNormal.y = cm[7] + cm[4];
514 planes.mPlanes[0].mNormal.z = cm[11] + cm[8];
515 planes.mPlanes[0].mDistance = cm[15] + cm[12];
518 planes.mPlanes[1].mNormal.x = cm[3] - cm[0]; // column 4 - column 1
519 planes.mPlanes[1].mNormal.y = cm[7] - cm[4];
520 planes.mPlanes[1].mNormal.z = cm[11] - cm[8];
521 planes.mPlanes[1].mDistance = cm[15] - cm[12];
524 planes.mPlanes[2].mNormal.x = cm[3] + cm[1]; // column 4 + column 2
525 planes.mPlanes[2].mNormal.y = cm[7] + cm[5];
526 planes.mPlanes[2].mNormal.z = cm[11] + cm[9];
527 planes.mPlanes[2].mDistance = cm[15] + cm[13];
530 planes.mPlanes[3].mNormal.x = cm[3] - cm[1]; // column 4 - column 2
531 planes.mPlanes[3].mNormal.y = cm[7] - cm[5];
532 planes.mPlanes[3].mNormal.z = cm[11] - cm[9];
533 planes.mPlanes[3].mDistance = cm[15] - cm[13];
536 planes.mPlanes[4].mNormal.x = cm[3] + cm[2]; // column 4 + column 3
537 planes.mPlanes[4].mNormal.y = cm[7] + cm[6];
538 planes.mPlanes[4].mNormal.z = cm[11] + cm[10];
539 planes.mPlanes[4].mDistance = cm[15] + cm[14];
542 planes.mPlanes[5].mNormal.x = cm[3] - cm[2]; // column 4 - column 3
543 planes.mPlanes[5].mNormal.y = cm[7] - cm[6];
544 planes.mPlanes[5].mNormal.z = cm[11] - cm[10];
545 planes.mPlanes[5].mDistance = cm[15] - cm[14];
549 for(uint32_t i = 0; i < 6; ++i)
551 // Normalize planes to ensure correct bounding distance checking
552 Plane& plane = planes.mPlanes[i];
553 float l = 1.0f / plane.mNormal.Length();
555 plane.mDistance *= l;
557 planes.mSign[i] = Vector3(Sign(plane.mNormal.x), Sign(plane.mNormal.y), Sign(plane.mNormal.z));
562 for(uint32_t i = 0; i < 6; ++i)
564 planes.mSign[i] = Vector3(Sign(planes.mPlanes[i].mNormal.x), Sign(planes.mPlanes[i].mNormal.y), Sign(planes.mPlanes[i].mNormal.z));
567 mFrustum[updateBufferIndex ? 0 : 1] = planes;
570 bool Camera::CheckSphereInFrustum(BufferIndex bufferIndex, const Vector3& origin, float radius)
572 const FrustumPlanes& planes = mFrustum[bufferIndex];
573 for(uint32_t i = 0; i < 6; ++i)
575 if((planes.mPlanes[i].mDistance + planes.mPlanes[i].mNormal.Dot(origin)) < -radius)
583 bool Camera::CheckAABBInFrustum(BufferIndex bufferIndex, const Vector3& origin, const Vector3& halfExtents)
585 const FrustumPlanes& planes = mFrustum[bufferIndex];
586 for(uint32_t i = 0; i < 6; ++i)
588 if(planes.mPlanes[i].mNormal.Dot(origin + (halfExtents * planes.mSign[i])) > -(planes.mPlanes[i].mDistance))
598 uint32_t Camera::UpdateProjection(BufferIndex updateBufferIndex)
600 uint32_t retval(mUpdateProjectionFlag);
601 // Early-exit if no update required
602 if(0u != mUpdateProjectionFlag)
604 Matrix& finalProjection = mFinalProjection[updateBufferIndex];
605 finalProjection.SetIdentity();
607 if(COPY_PREVIOUS_MATRIX == mUpdateProjectionFlag)
609 // The projection matrix was updated in the previous frame; copy it
610 mProjectionMatrix.CopyPrevious(updateBufferIndex);
612 finalProjection = mProjectionMatrix[updateBufferIndex];
614 else // UPDATE_COUNT == mUpdateProjectionFlag
616 switch(mProjectionMode)
618 case Dali::Camera::PERSPECTIVE_PROJECTION:
620 Matrix& projectionMatrix = mProjectionMatrix.Get(updateBufferIndex);
621 Perspective(projectionMatrix,
628 //need to apply custom clipping plane
629 if(mUseReflectionClip)
631 Matrix& viewMatrix = mViewMatrix.Get(updateBufferIndex);
632 Matrix viewInv = viewMatrix;
636 Dali::Vector4 adjReflectPlane = mReflectionPlane;
637 float d = mReflectionPlane.Dot(mReflectionEye);
640 adjReflectPlane.w = -adjReflectPlane.w;
643 Vector4 customClipping = viewInv * adjReflectPlane;
644 AdjustNearPlaneForPerspective(projectionMatrix, customClipping);
649 float* vZ = matZ.AsFloat();
651 Matrix::Multiply(projectionMatrix, projectionMatrix, matZ);
655 case Dali::Camera::ORTHOGRAPHIC_PROJECTION:
657 Matrix& projectionMatrix = mProjectionMatrix.Get(updateBufferIndex);
658 Orthographic(projectionMatrix,
661 mBottomClippingPlane,
670 mProjectionMatrix.SetDirty(updateBufferIndex);
672 Quaternion rotationAngle;
673 switch(mProjectionRotation)
677 rotationAngle = Quaternion(Dali::ANGLE_90, Vector3::ZAXIS);
682 rotationAngle = Quaternion(Dali::ANGLE_180, Vector3::ZAXIS);
687 rotationAngle = Quaternion(Dali::ANGLE_270, Vector3::ZAXIS);
691 rotationAngle = Quaternion(Dali::ANGLE_0, Vector3::ZAXIS);
696 rotation.SetIdentity();
697 rotation.SetTransformComponents(Vector3(1.0f, 1.0f, 1.0f), rotationAngle, Vector3(0.0f, 0.0f, 0.0f));
699 Matrix::Multiply(finalProjection, mProjectionMatrix.Get(updateBufferIndex), rotation);
701 --mUpdateProjectionFlag;
706 } // namespace SceneGraph
708 } // namespace Internal