2 * Copyright (c) 2018 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/public-api/common/dali-common.h>
27 #include <dali/public-api/math/math-utils.h>
28 #include <dali/internal/update/nodes/node.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;
52 template< typename T >
55 return T( T(0) < value ) - T( value < T(0) );
58 void LookAt(Matrix& result, const Vector3& eye, const Vector3& target, const Vector3& up)
60 Vector3 vZ = target - eye;
63 Vector3 vX = up.Cross(vZ);
66 Vector3 vY = vZ.Cross(vX);
69 result.SetInverseTransformComponents(vX, vY, vZ, eye);
72 void Frustum(Matrix& result, float left, float right, float bottom, float top, float near, float far, bool invertYAxis)
74 float deltaZ = far - near;
75 if ((near <= 0.0f) || (far <= 0.0f) || Equals(right, left) || Equals(bottom, top) || (deltaZ <= 0.0f))
77 DALI_LOG_ERROR("Invalid parameters passed into Frustum!\n");
78 DALI_ASSERT_DEBUG("Invalid parameters passed into Frustum!");
82 float deltaX = right - left;
83 float deltaY = invertYAxis ? bottom - top : top - bottom;
87 float* m = result.AsFloat();
88 m[0] = -2.0f * near / deltaX;
89 m[1] = m[2] = m[3] = 0.0f;
91 m[5] = -2.0f * near / deltaY;
92 m[4] = m[6] = m[7] = 0.0f;
94 m[8] = (right + left) / deltaX;
95 m[9] = (top + bottom) / deltaY;
96 m[10] = (near + far) / deltaZ;
99 m[14] = -2.0f * near * far / deltaZ;
100 m[12] = m[13] = m[15] = 0.0f;
103 void Perspective(Matrix& result, float fovy, float aspect, float near, float far, bool invertYAxis )
105 float frustumH = tanf( fovy * 0.5f ) * near;
106 float frustumW = frustumH * aspect;
108 Frustum(result, -frustumW, frustumW, -frustumH, frustumH, near, far, invertYAxis);
111 void Orthographic(Matrix& result, float left, float right, float bottom, float top, float near, float far, bool invertYAxis)
113 if ( Equals(right, left) || Equals(top, bottom) || Equals(far, near) )
115 DALI_LOG_ERROR( "Cannot create orthographic projection matrix with a zero dimension.\n" );
116 DALI_ASSERT_DEBUG( "Cannot create orthographic projection matrix with a zero dimension." );
120 float deltaX = right - left;
121 float deltaY = invertYAxis ? bottom - top : top - bottom;
122 float deltaZ = far - near;
124 float *m = result.AsFloat();
125 m[0] = -2.0f / deltaX;
131 m[5] = -2.0f / deltaY;
137 m[10] = 2.0f / deltaZ;
139 m[12] = -(right + left) / deltaX;
140 m[13] = -(top + bottom) / deltaY;
141 m[14] = -(near + far) / deltaZ;
145 } // unnamed namespace
147 const Dali::Camera::Type Camera::DEFAULT_TYPE( Dali::Camera::FREE_LOOK );
148 const Dali::Camera::ProjectionMode Camera::DEFAULT_MODE( Dali::Camera::PERSPECTIVE_PROJECTION );
149 const bool Camera::DEFAULT_INVERT_Y_AXIS( false );
150 const float Camera::DEFAULT_FIELD_OF_VIEW( 45.0f*(Math::PI/180.0f) );
151 const float Camera::DEFAULT_ASPECT_RATIO( 4.0f/3.0f );
152 const float Camera::DEFAULT_LEFT_CLIPPING_PLANE(-240.0f);
153 const float Camera::DEFAULT_RIGHT_CLIPPING_PLANE(240.0f);
154 const float Camera::DEFAULT_TOP_CLIPPING_PLANE(-400.0f);
155 const float Camera::DEFAULT_BOTTOM_CLIPPING_PLANE(400.0f);
156 const float Camera::DEFAULT_NEAR_CLIPPING_PLANE( 800.0f ); // default height of the screen
157 const float Camera::DEFAULT_FAR_CLIPPING_PLANE( DEFAULT_NEAR_CLIPPING_PLANE + 2.f * DEFAULT_NEAR_CLIPPING_PLANE );
158 const Vector3 Camera::DEFAULT_TARGET_POSITION( 0.0f, 0.0f, 0.0f );
162 : mUpdateViewFlag( UPDATE_COUNT ),
163 mUpdateProjectionFlag( UPDATE_COUNT ),
165 mType( DEFAULT_TYPE ),
166 mProjectionMode( DEFAULT_MODE ),
167 mInvertYAxis( DEFAULT_INVERT_Y_AXIS ),
168 mFieldOfView( DEFAULT_FIELD_OF_VIEW ),
169 mAspectRatio( DEFAULT_ASPECT_RATIO ),
170 mLeftClippingPlane( DEFAULT_LEFT_CLIPPING_PLANE ),
171 mRightClippingPlane( DEFAULT_RIGHT_CLIPPING_PLANE ),
172 mTopClippingPlane( DEFAULT_TOP_CLIPPING_PLANE ),
173 mBottomClippingPlane( DEFAULT_BOTTOM_CLIPPING_PLANE ),
174 mNearClippingPlane( DEFAULT_NEAR_CLIPPING_PLANE ),
175 mFarClippingPlane( DEFAULT_FAR_CLIPPING_PLANE ),
176 mTargetPosition( DEFAULT_TARGET_POSITION ),
179 mInverseViewProjection( Matrix::IDENTITY )
183 Camera* Camera::New()
192 void Camera::SetNode( const Node* node )
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;
270 void VectorReflectedByPlane(Vector4 &out, Vector4 &in, Vector4 &plane)
272 float d = float(2.0) * plane.Dot(in);
273 out.x = static_cast<float>(in.x - plane.x*d);
274 out.y = static_cast<float>(in.y - plane.y*d);
275 out.z = static_cast<float>(in.z - plane.z*d);
276 out.w = static_cast<float>(in.w - plane.w*d);
279 void Camera::AdjustNearPlaneForPerspective( Matrix& perspective, const Vector4& clipPlane )
282 float* v = perspective.AsFloat();
284 q.x = (Sign(clipPlane.x) + v[8]) / v[0];
285 q.y = (Sign(clipPlane.y) + v[9]) / v[5];
287 q.w = (1.0f + v[10]) / v[14];
289 // Calculate the scaled plane vector
290 Vector4 c = clipPlane * (REFLECTION_NORMALIZED_DEVICE_COORDINATE_PARAMETER_A / q.Dot( clipPlane));
292 // Replace the third row of the projection v
295 v[10] = c.z + REFLECTION_NORMALIZED_DEVICE_COORDINATE_PARAMETER_D;
299 void Camera::SetReflectByPlane( const Vector4& plane )
301 float* v = mReflectionMtx.AsFloat();
302 float _2ab = -2.0f * plane.x * plane.y;
303 float _2ac = -2.0f * plane.x * plane.z;
304 float _2bc = -2.0f * plane.y * plane.z;
306 v[0] = 1.0f - 2.0f * plane.x * plane.x;
312 v[5] = 1.0f - 2.0f * plane.y * plane.y;
318 v[10] = 1.0f - 2.0f * plane.z * plane.z;
321 v[12] = - 2 * plane.x * plane.w;
322 v[13] = - 2 * plane.y * plane.w;
323 v[14] = - 2 * plane.z * plane.w;
326 mUseReflection = true;
327 mReflectionPlane = plane;
328 mUpdateViewFlag = UPDATE_COUNT;
331 const Matrix& Camera::GetProjectionMatrix( BufferIndex bufferIndex ) const
333 return mProjectionMatrix[ bufferIndex ];
336 const Matrix& Camera::GetViewMatrix( BufferIndex bufferIndex ) const
338 return mViewMatrix[ bufferIndex ];
341 const Matrix& Camera::GetInverseViewProjectionMatrix( BufferIndex bufferIndex ) const
343 return mInverseViewProjection[ bufferIndex ];
346 const PropertyInputImpl* Camera::GetProjectionMatrix() const
348 return &mProjectionMatrix;
351 const PropertyInputImpl* Camera::GetViewMatrix() const
356 void Camera::Update( BufferIndex updateBufferIndex )
358 // if owning node has changes in world position we need to update camera for next 2 frames
359 if( mNode->IsLocalMatrixDirty() )
361 mUpdateViewFlag = UPDATE_COUNT;
363 if( mNode->GetDirtyFlags() & NodePropertyFlags::VISIBLE )
365 // If the visibility changes, the projection matrix needs to be re-calculated.
366 // It may happen the first time an actor is rendered it's rendered only once and becomes invisible,
367 // in the following update the node will be skipped leaving the projection matrix (double buffered)
368 // with the Identity.
369 mUpdateProjectionFlag = UPDATE_COUNT;
372 // if either matrix changed, we need to recalculate the inverse matrix for hit testing to work
373 uint32_t viewUpdateCount = UpdateViewMatrix( updateBufferIndex );
374 uint32_t projectionUpdateCount = UpdateProjection( updateBufferIndex );
376 // if model or view matrix changed we need to either recalculate the inverse VP or copy previous
377 if( viewUpdateCount > COPY_PREVIOUS_MATRIX || projectionUpdateCount > COPY_PREVIOUS_MATRIX )
379 // either has actually changed so recalculate
380 Matrix::Multiply( mInverseViewProjection[ updateBufferIndex ], mViewMatrix[ updateBufferIndex ], mProjectionMatrix[ updateBufferIndex ] );
381 UpdateFrustum( updateBufferIndex );
383 // ignore the error, if the view projection is incorrect (non inversible) then you will have tough times anyways
384 static_cast< void >( mInverseViewProjection[ updateBufferIndex ].Invert() );
386 else if( viewUpdateCount == COPY_PREVIOUS_MATRIX || projectionUpdateCount == COPY_PREVIOUS_MATRIX )
388 // neither has actually changed, but we might copied previous frames value so need to
389 // copy the previous inverse and frustum as well
390 mInverseViewProjection[updateBufferIndex] = mInverseViewProjection[updateBufferIndex ? 0 : 1];
391 mFrustum[ updateBufferIndex ] = mFrustum[ updateBufferIndex ? 0 : 1 ];
395 bool Camera::ViewMatrixUpdated()
397 return 0u != mUpdateViewFlag;
400 uint32_t Camera::UpdateViewMatrix( BufferIndex updateBufferIndex )
402 uint32_t retval( mUpdateViewFlag );
403 if( 0u != mUpdateViewFlag )
405 if( COPY_PREVIOUS_MATRIX == mUpdateViewFlag )
407 // The projection matrix was updated in the previous frame; copy it
408 mViewMatrix.CopyPrevious( updateBufferIndex );
410 else // UPDATE_COUNT == mUpdateViewFlag
414 // camera orientation taken from node - i.e. look in abitrary, unconstrained direction
415 case Dali::Camera::FREE_LOOK:
417 Matrix& viewMatrix = mViewMatrix.Get( updateBufferIndex );
418 viewMatrix = mNode->GetWorldMatrix( updateBufferIndex );
422 const Matrix& owningNodeMatrix( mNode->GetWorldMatrix( updateBufferIndex ) );
423 Vector3 position{}, scale{};
424 Quaternion orientation{};
425 owningNodeMatrix.GetTransformComponents( position, orientation, scale );
426 mReflectionEye = position;
427 mUseReflectionClip = true;
429 Matrix& viewMatrix = mViewMatrix.Get( updateBufferIndex );
430 Matrix oldViewMatrix( viewMatrix );
431 Matrix::Multiply(viewMatrix, oldViewMatrix, mReflectionMtx);
435 mViewMatrix.SetDirty( updateBufferIndex );
439 // camera orientation constrained to look at a target
440 case Dali::Camera::LOOK_AT_TARGET:
442 const Matrix& owningNodeMatrix( mNode->GetWorldMatrix( updateBufferIndex ) );
443 Vector3 position, scale;
444 Quaternion orientation;
445 owningNodeMatrix.GetTransformComponents( position, orientation, scale );
446 Matrix& viewMatrix = mViewMatrix.Get( updateBufferIndex );
450 Vector3 up = orientation.Rotate( Vector3::YAXIS );
451 Vector4 position4 = Vector4(position);
452 Vector4 target4 = Vector4(mTargetPosition);
453 Vector4 up4 = Vector4(up);
457 Vector3 positionNew3;
458 Vector3 targetNewVector3;
462 VectorReflectedByPlane(positionNew, position4, mReflectionPlane);
463 VectorReflectedByPlane(targetNew, target4, mReflectionPlane);
464 VectorReflectedByPlane(upNew, up4, mReflectionPlane);
466 positionNew3 = Vector3(positionNew);
467 targetNewVector3 = Vector3(targetNew);
468 upNew3 = Vector3(upNew);
469 LookAt(viewMatrix, positionNew3, targetNewVector3, upNew3 );
471 Matrix oldViewMatrix( viewMatrix );
473 tmp.SetIdentityAndScale(Vector3(-1.0, 1.0,1.0));
474 Matrix::Multiply(viewMatrix, oldViewMatrix, tmp);
476 mReflectionEye = positionNew;
477 mUseReflectionClip = true;
481 LookAt( viewMatrix, position, mTargetPosition, orientation.Rotate( Vector3::YAXIS ) );
483 mViewMatrix.SetDirty( updateBufferIndex );
493 void Camera::UpdateFrustum( BufferIndex updateBufferIndex, bool normalize )
496 // Extract the clip matrix planes
498 Matrix::Multiply( clipMatrix, mViewMatrix[ updateBufferIndex ], mProjectionMatrix[ updateBufferIndex ] );
500 const float* cm = clipMatrix.AsFloat();
501 FrustumPlanes& planes = mFrustum[ updateBufferIndex ];
504 planes.mPlanes[ 0 ].mNormal.x = cm[ 3 ] + cm[ 0 ]; // column 4 + column 1
505 planes.mPlanes[ 0 ].mNormal.y = cm[ 7 ] + cm[ 4 ];
506 planes.mPlanes[ 0 ].mNormal.z = cm[ 11 ] + cm[ 8 ];
507 planes.mPlanes[ 0 ].mDistance = cm[ 15 ] + cm[ 12 ];
510 planes.mPlanes[ 1 ].mNormal.x = cm[ 3 ] - cm[ 0 ]; // column 4 - column 1
511 planes.mPlanes[ 1 ].mNormal.y = cm[ 7 ] - cm[ 4 ];
512 planes.mPlanes[ 1 ].mNormal.z = cm[ 11 ] - cm[ 8 ];
513 planes.mPlanes[ 1 ].mDistance = cm[ 15 ] - cm[ 12 ];
516 planes.mPlanes[ 2 ].mNormal.x = cm[ 3 ] + cm[ 1 ]; // column 4 + column 2
517 planes.mPlanes[ 2 ].mNormal.y = cm[ 7 ] + cm[ 5 ];
518 planes.mPlanes[ 2 ].mNormal.z = cm[ 11 ] + cm[ 9 ];
519 planes.mPlanes[ 2 ].mDistance = cm[ 15 ] + cm[ 13 ];
522 planes.mPlanes[ 3 ].mNormal.x = cm[ 3 ] - cm[ 1 ]; // column 4 - column 2
523 planes.mPlanes[ 3 ].mNormal.y = cm[ 7 ] - cm[ 5 ];
524 planes.mPlanes[ 3 ].mNormal.z = cm[ 11 ] - cm[ 9 ];
525 planes.mPlanes[ 3 ].mDistance = cm[ 15 ] - cm[ 13 ];
528 planes.mPlanes[ 4 ].mNormal.x = cm[ 3 ] + cm[ 2 ]; // column 4 + column 3
529 planes.mPlanes[ 4 ].mNormal.y = cm[ 7 ] + cm[ 6 ];
530 planes.mPlanes[ 4 ].mNormal.z = cm[ 11 ] + cm[ 10 ];
531 planes.mPlanes[ 4 ].mDistance = cm[ 15 ] + cm[ 14 ];
534 planes.mPlanes[ 5 ].mNormal.x = cm[ 3 ] - cm[ 2 ]; // column 4 - column 3
535 planes.mPlanes[ 5 ].mNormal.y = cm[ 7 ] - cm[ 6 ];
536 planes.mPlanes[ 5 ].mNormal.z = cm[ 11 ] - cm[ 10 ];
537 planes.mPlanes[ 5 ].mDistance = cm[ 15 ] - cm[ 14 ];
541 for ( uint32_t i = 0; i < 6; ++i )
543 // Normalize planes to ensure correct bounding distance checking
544 Plane& plane = planes.mPlanes[ i ];
545 float l = 1.0f / plane.mNormal.Length();
547 plane.mDistance *= l;
549 planes.mSign[i] = Vector3( Sign(plane.mNormal.x), Sign(plane.mNormal.y), Sign(plane.mNormal.z) );
554 for ( uint32_t i = 0; i < 6; ++i )
556 planes.mSign[i] = Vector3( Sign(planes.mPlanes[ i ].mNormal.x), Sign(planes.mPlanes[ i ].mNormal.y), Sign(planes.mPlanes[ i ].mNormal.z) );
559 mFrustum[ updateBufferIndex ? 0 : 1 ] = planes;
562 bool Camera::CheckSphereInFrustum( BufferIndex bufferIndex, const Vector3& origin, float radius )
564 const FrustumPlanes& planes = mFrustum[ bufferIndex ];
565 for ( uint32_t i = 0; i < 6; ++i )
567 if ( ( planes.mPlanes[ i ].mDistance + planes.mPlanes[ i ].mNormal.Dot( origin ) ) < -radius )
575 bool Camera::CheckAABBInFrustum( BufferIndex bufferIndex, const Vector3& origin, const Vector3& halfExtents )
577 const FrustumPlanes& planes = mFrustum[ bufferIndex ];
578 for ( uint32_t i = 0; i < 6; ++i )
580 if( planes.mPlanes[ i ].mNormal.Dot( origin + (halfExtents * planes.mSign[i]) ) > -(planes.mPlanes[ i ].mDistance) )
590 uint32_t Camera::UpdateProjection( BufferIndex updateBufferIndex )
592 uint32_t retval( mUpdateProjectionFlag );
593 // Early-exit if no update required
594 if ( 0u != mUpdateProjectionFlag )
596 if ( COPY_PREVIOUS_MATRIX == mUpdateProjectionFlag )
598 // The projection matrix was updated in the previous frame; copy it
599 mProjectionMatrix.CopyPrevious( updateBufferIndex );
601 else // UPDATE_COUNT == mUpdateProjectionFlag
603 switch( mProjectionMode )
605 case Dali::Camera::PERSPECTIVE_PROJECTION:
607 Matrix &projectionMatrix = mProjectionMatrix.Get( updateBufferIndex );
608 Perspective( projectionMatrix,
615 //need to apply custom clipping plane
616 if (mUseReflectionClip)
618 Matrix& viewMatrix = mViewMatrix.Get( updateBufferIndex );
619 Matrix viewInv = viewMatrix;
623 Dali::Vector4 adjReflectPlane = mReflectionPlane;
624 float d = mReflectionPlane.Dot(mReflectionEye);
627 adjReflectPlane.w = -adjReflectPlane.w;
630 Vector4 customClipping = viewInv * adjReflectPlane;
631 AdjustNearPlaneForPerspective(projectionMatrix, customClipping);
636 float* vZ = matZ.AsFloat();
638 Matrix::Multiply(projectionMatrix, projectionMatrix , matZ);
642 case Dali::Camera::ORTHOGRAPHIC_PROJECTION:
644 Matrix &projectionMatrix = mProjectionMatrix.Get( updateBufferIndex );
645 Orthographic( projectionMatrix,
646 mLeftClippingPlane, mRightClippingPlane,
647 mBottomClippingPlane, mTopClippingPlane,
648 mNearClippingPlane, mFarClippingPlane,
654 mProjectionMatrix.SetDirty( updateBufferIndex );
656 --mUpdateProjectionFlag;
661 } // namespace SceneGraph
663 } // namespace Internal