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
48 template< typename T >
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 );
158 : mUpdateViewFlag( UPDATE_COUNT ),
159 mUpdateProjectionFlag( UPDATE_COUNT ),
161 mType( DEFAULT_TYPE ),
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 )
179 Camera* Camera::New()
188 void Camera::SetNode( const Node* node )
193 void Camera::SetType( Dali::Camera::Type type )
198 void Camera::SetProjectionMode( Dali::Camera::ProjectionMode mode )
200 mProjectionMode = mode;
201 mUpdateProjectionFlag = UPDATE_COUNT;
204 void Camera::SetInvertYAxis( bool invertYAxis )
206 mInvertYAxis = invertYAxis;
207 mUpdateProjectionFlag = UPDATE_COUNT;
210 void Camera::SetFieldOfView( float fieldOfView )
212 mFieldOfView = fieldOfView;
213 mUpdateProjectionFlag = UPDATE_COUNT;
216 void Camera::SetAspectRatio( float aspectRatio )
218 mAspectRatio = aspectRatio;
219 mUpdateProjectionFlag = UPDATE_COUNT;
222 void Camera::SetLeftClippingPlane( float leftClippingPlane )
224 mLeftClippingPlane = leftClippingPlane;
225 mUpdateProjectionFlag = UPDATE_COUNT;
228 void Camera::SetRightClippingPlane( float rightClippingPlane )
230 mRightClippingPlane = rightClippingPlane;
231 mUpdateProjectionFlag = UPDATE_COUNT;
234 void Camera::SetTopClippingPlane( float topClippingPlane )
236 mTopClippingPlane = topClippingPlane;
237 mUpdateProjectionFlag = UPDATE_COUNT;
240 void Camera::SetBottomClippingPlane( float bottomClippingPlane )
242 mBottomClippingPlane = bottomClippingPlane;
243 mUpdateProjectionFlag = UPDATE_COUNT;
246 void Camera::SetNearClippingPlane( float nearClippingPlane )
248 mNearClippingPlane = nearClippingPlane;
249 mUpdateProjectionFlag = UPDATE_COUNT;
252 void Camera::SetFarClippingPlane( float farClippingPlane )
254 mFarClippingPlane = farClippingPlane;
255 mUpdateProjectionFlag = UPDATE_COUNT;
258 void Camera::SetTargetPosition( const Vector3& targetPosition )
260 mTargetPosition = targetPosition;
261 mUpdateViewFlag = UPDATE_COUNT;
264 const Matrix& Camera::GetProjectionMatrix( BufferIndex bufferIndex ) const
266 return mProjectionMatrix[ bufferIndex ];
269 const Matrix& Camera::GetViewMatrix( BufferIndex bufferIndex ) const
271 return mViewMatrix[ bufferIndex ];
274 const Matrix& Camera::GetInverseViewProjectionMatrix( BufferIndex bufferIndex ) const
276 return mInverseViewProjection[ bufferIndex ];
279 const PropertyInputImpl* Camera::GetProjectionMatrix() const
281 return &mProjectionMatrix;
284 const PropertyInputImpl* Camera::GetViewMatrix() const
289 void Camera::Update( BufferIndex updateBufferIndex )
291 // if owning node has changes in world position we need to update camera for next 2 frames
292 if( mNode->IsLocalMatrixDirty() )
294 mUpdateViewFlag = UPDATE_COUNT;
296 if( mNode->GetDirtyFlags() & NodePropertyFlags::VISIBLE )
298 // If the visibility changes, the projection matrix needs to be re-calculated.
299 // It may happen the first time an actor is rendered it's rendered only once and becomes invisible,
300 // in the following update the node will be skipped leaving the projection matrix (double buffered)
301 // with the Identity.
302 mUpdateProjectionFlag = UPDATE_COUNT;
305 // if either matrix changed, we need to recalculate the inverse matrix for hit testing to work
306 uint32_t viewUpdateCount = UpdateViewMatrix( updateBufferIndex );
307 uint32_t projectionUpdateCount = UpdateProjection( updateBufferIndex );
309 // if model or view matrix changed we need to either recalculate the inverse VP or copy previous
310 if( viewUpdateCount > COPY_PREVIOUS_MATRIX || projectionUpdateCount > COPY_PREVIOUS_MATRIX )
312 // either has actually changed so recalculate
313 Matrix::Multiply( mInverseViewProjection[ updateBufferIndex ], mViewMatrix[ updateBufferIndex ], mProjectionMatrix[ updateBufferIndex ] );
314 UpdateFrustum( updateBufferIndex );
316 // ignore the error, if the view projection is incorrect (non inversible) then you will have tough times anyways
317 static_cast< void >( mInverseViewProjection[ updateBufferIndex ].Invert() );
319 else if( viewUpdateCount == COPY_PREVIOUS_MATRIX || projectionUpdateCount == COPY_PREVIOUS_MATRIX )
321 // neither has actually changed, but we might copied previous frames value so need to
322 // copy the previous inverse and frustum as well
323 mInverseViewProjection[updateBufferIndex] = mInverseViewProjection[updateBufferIndex ? 0 : 1];
324 mFrustum[ updateBufferIndex ] = mFrustum[ updateBufferIndex ? 0 : 1 ];
328 bool Camera::ViewMatrixUpdated()
330 return 0u != mUpdateViewFlag;
333 uint32_t Camera::UpdateViewMatrix( BufferIndex updateBufferIndex )
335 uint32_t retval( mUpdateViewFlag );
336 if( 0u != mUpdateViewFlag )
338 if( COPY_PREVIOUS_MATRIX == mUpdateViewFlag )
340 // The projection matrix was updated in the previous frame; copy it
341 mViewMatrix.CopyPrevious( updateBufferIndex );
343 else // UPDATE_COUNT == mUpdateViewFlag
347 // camera orientation taken from node - i.e. look in abitrary, unconstrained direction
348 case Dali::Camera::FREE_LOOK:
350 Matrix& viewMatrix = mViewMatrix.Get( updateBufferIndex );
351 viewMatrix = mNode->GetWorldMatrix( updateBufferIndex );
353 mViewMatrix.SetDirty( updateBufferIndex );
356 // camera orientation constrained to look at a target
357 case Dali::Camera::LOOK_AT_TARGET:
359 const Matrix& owningNodeMatrix( mNode->GetWorldMatrix( updateBufferIndex ) );
360 Vector3 position, scale;
361 Quaternion orientation;
362 owningNodeMatrix.GetTransformComponents( position, orientation, scale );
363 Matrix& viewMatrix = mViewMatrix.Get( updateBufferIndex );
364 LookAt( viewMatrix, position, mTargetPosition, orientation.Rotate( Vector3::YAXIS ) );
365 mViewMatrix.SetDirty( updateBufferIndex );
375 void Camera::UpdateFrustum( BufferIndex updateBufferIndex, bool normalize )
378 // Extract the clip matrix planes
380 Matrix::Multiply( clipMatrix, mViewMatrix[ updateBufferIndex ], mProjectionMatrix[ updateBufferIndex ] );
382 const float* cm = clipMatrix.AsFloat();
383 FrustumPlanes& planes = mFrustum[ updateBufferIndex ];
386 planes.mPlanes[ 0 ].mNormal.x = cm[ 3 ] + cm[ 0 ]; // column 4 + column 1
387 planes.mPlanes[ 0 ].mNormal.y = cm[ 7 ] + cm[ 4 ];
388 planes.mPlanes[ 0 ].mNormal.z = cm[ 11 ] + cm[ 8 ];
389 planes.mPlanes[ 0 ].mDistance = cm[ 15 ] + cm[ 12 ];
392 planes.mPlanes[ 1 ].mNormal.x = cm[ 3 ] - cm[ 0 ]; // column 4 - column 1
393 planes.mPlanes[ 1 ].mNormal.y = cm[ 7 ] - cm[ 4 ];
394 planes.mPlanes[ 1 ].mNormal.z = cm[ 11 ] - cm[ 8 ];
395 planes.mPlanes[ 1 ].mDistance = cm[ 15 ] - cm[ 12 ];
398 planes.mPlanes[ 2 ].mNormal.x = cm[ 3 ] + cm[ 1 ]; // column 4 + column 2
399 planes.mPlanes[ 2 ].mNormal.y = cm[ 7 ] + cm[ 5 ];
400 planes.mPlanes[ 2 ].mNormal.z = cm[ 11 ] + cm[ 9 ];
401 planes.mPlanes[ 2 ].mDistance = cm[ 15 ] + cm[ 13 ];
404 planes.mPlanes[ 3 ].mNormal.x = cm[ 3 ] - cm[ 1 ]; // column 4 - column 2
405 planes.mPlanes[ 3 ].mNormal.y = cm[ 7 ] - cm[ 5 ];
406 planes.mPlanes[ 3 ].mNormal.z = cm[ 11 ] - cm[ 9 ];
407 planes.mPlanes[ 3 ].mDistance = cm[ 15 ] - cm[ 13 ];
410 planes.mPlanes[ 4 ].mNormal.x = cm[ 3 ] + cm[ 2 ]; // column 4 + column 3
411 planes.mPlanes[ 4 ].mNormal.y = cm[ 7 ] + cm[ 6 ];
412 planes.mPlanes[ 4 ].mNormal.z = cm[ 11 ] + cm[ 10 ];
413 planes.mPlanes[ 4 ].mDistance = cm[ 15 ] + cm[ 14 ];
416 planes.mPlanes[ 5 ].mNormal.x = cm[ 3 ] - cm[ 2 ]; // column 4 - column 3
417 planes.mPlanes[ 5 ].mNormal.y = cm[ 7 ] - cm[ 6 ];
418 planes.mPlanes[ 5 ].mNormal.z = cm[ 11 ] - cm[ 10 ];
419 planes.mPlanes[ 5 ].mDistance = cm[ 15 ] - cm[ 14 ];
423 for ( uint32_t i = 0; i < 6; ++i )
425 // Normalize planes to ensure correct bounding distance checking
426 Plane& plane = planes.mPlanes[ i ];
427 float l = 1.0f / plane.mNormal.Length();
429 plane.mDistance *= l;
431 planes.mSign[i] = Vector3( Sign(plane.mNormal.x), Sign(plane.mNormal.y), Sign(plane.mNormal.z) );
436 for ( uint32_t i = 0; i < 6; ++i )
438 planes.mSign[i] = Vector3( Sign(planes.mPlanes[ i ].mNormal.x), Sign(planes.mPlanes[ i ].mNormal.y), Sign(planes.mPlanes[ i ].mNormal.z) );
441 mFrustum[ updateBufferIndex ? 0 : 1 ] = planes;
444 bool Camera::CheckSphereInFrustum( BufferIndex bufferIndex, const Vector3& origin, float radius )
446 const FrustumPlanes& planes = mFrustum[ bufferIndex ];
447 for ( uint32_t i = 0; i < 6; ++i )
449 if ( ( planes.mPlanes[ i ].mDistance + planes.mPlanes[ i ].mNormal.Dot( origin ) ) < -radius )
457 bool Camera::CheckAABBInFrustum( BufferIndex bufferIndex, const Vector3& origin, const Vector3& halfExtents )
459 const FrustumPlanes& planes = mFrustum[ bufferIndex ];
460 for ( uint32_t i = 0; i < 6; ++i )
462 if( planes.mPlanes[ i ].mNormal.Dot( origin + (halfExtents * planes.mSign[i]) ) > -(planes.mPlanes[ i ].mDistance) )
472 uint32_t Camera::UpdateProjection( BufferIndex updateBufferIndex )
474 uint32_t retval( mUpdateProjectionFlag );
475 // Early-exit if no update required
476 if ( 0u != mUpdateProjectionFlag )
478 if ( COPY_PREVIOUS_MATRIX == mUpdateProjectionFlag )
480 // The projection matrix was updated in the previous frame; copy it
481 mProjectionMatrix.CopyPrevious( updateBufferIndex );
483 else // UPDATE_COUNT == mUpdateProjectionFlag
485 switch( mProjectionMode )
487 case Dali::Camera::PERSPECTIVE_PROJECTION:
489 Matrix &projectionMatrix = mProjectionMatrix.Get( updateBufferIndex );
490 Perspective( projectionMatrix,
498 case Dali::Camera::ORTHOGRAPHIC_PROJECTION:
500 Matrix &projectionMatrix = mProjectionMatrix.Get( updateBufferIndex );
501 Orthographic( projectionMatrix,
502 mLeftClippingPlane, mRightClippingPlane,
503 mBottomClippingPlane, mTopClippingPlane,
504 mNearClippingPlane, mFarClippingPlane,
510 mProjectionMatrix.SetDirty( updateBufferIndex );
512 --mUpdateProjectionFlag;
517 } // namespace SceneGraph
519 } // namespace Internal