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, const Vector2& stereoBias )
101 float frustumH = tanf( fovy * 0.5f ) * near;
102 float frustumW = frustumH * aspect;
103 Vector2 bias = stereoBias * 0.5f;
105 Frustum(result, -(frustumW + bias.x), frustumW - bias.x, -(frustumH + bias.y), frustumH - bias.y, near, far, invertYAxis);
108 void Orthographic(Matrix& result, float left, float right, float bottom, float top, float near, float far, bool invertYAxis)
110 if ( Equals(right, left) || Equals(top, bottom) || Equals(far, near) )
112 DALI_LOG_ERROR( "Cannot create orthographic projection matrix with a zero dimension.\n" );
113 DALI_ASSERT_DEBUG( "Cannot create orthographic projection matrix with a zero dimension." );
117 float deltaX = right - left;
118 float deltaY = invertYAxis ? bottom - top : top - bottom;
119 float deltaZ = far - near;
121 float *m = result.AsFloat();
122 m[0] = -2.0f / deltaX;
128 m[5] = -2.0f / deltaY;
134 m[10] = 2.0f / deltaZ;
136 m[12] = -(right + left) / deltaX;
137 m[13] = -(top + bottom) / deltaY;
138 m[14] = -(near + far) / deltaZ;
142 } // unnamed namespace
144 const Dali::Camera::Type Camera::DEFAULT_TYPE( Dali::Camera::FREE_LOOK );
145 const Dali::Camera::ProjectionMode Camera::DEFAULT_MODE( Dali::Camera::PERSPECTIVE_PROJECTION );
146 const bool Camera::DEFAULT_INVERT_Y_AXIS( false );
147 const float Camera::DEFAULT_FIELD_OF_VIEW( 45.0f*(Math::PI/180.0f) );
148 const float Camera::DEFAULT_ASPECT_RATIO( 4.0f/3.0f );
149 const float Camera::DEFAULT_LEFT_CLIPPING_PLANE(-240.0f);
150 const float Camera::DEFAULT_RIGHT_CLIPPING_PLANE(240.0f);
151 const float Camera::DEFAULT_TOP_CLIPPING_PLANE(-400.0f);
152 const float Camera::DEFAULT_BOTTOM_CLIPPING_PLANE(400.0f);
153 const float Camera::DEFAULT_NEAR_CLIPPING_PLANE( 800.0f ); // default height of the screen
154 const float Camera::DEFAULT_FAR_CLIPPING_PLANE( DEFAULT_NEAR_CLIPPING_PLANE + 2.f * DEFAULT_NEAR_CLIPPING_PLANE );
155 const Vector2 Camera::DEFAULT_STEREO_BIAS( 0.0f, 0.0f );
156 const Vector3 Camera::DEFAULT_TARGET_POSITION( 0.0f, 0.0f, 0.0f );
160 : mUpdateViewFlag( UPDATE_COUNT ),
161 mUpdateProjectionFlag( UPDATE_COUNT ),
163 mType( DEFAULT_TYPE ),
164 mProjectionMode( DEFAULT_MODE ),
165 mInvertYAxis( DEFAULT_INVERT_Y_AXIS ),
166 mFieldOfView( DEFAULT_FIELD_OF_VIEW ),
167 mAspectRatio( DEFAULT_ASPECT_RATIO ),
168 mLeftClippingPlane( DEFAULT_LEFT_CLIPPING_PLANE ),
169 mRightClippingPlane( DEFAULT_RIGHT_CLIPPING_PLANE ),
170 mTopClippingPlane( DEFAULT_TOP_CLIPPING_PLANE ),
171 mBottomClippingPlane( DEFAULT_BOTTOM_CLIPPING_PLANE ),
172 mNearClippingPlane( DEFAULT_NEAR_CLIPPING_PLANE ),
173 mFarClippingPlane( DEFAULT_FAR_CLIPPING_PLANE ),
174 mStereoBias( DEFAULT_STEREO_BIAS ),
175 mTargetPosition( DEFAULT_TARGET_POSITION ),
178 mInverseViewProjection( Matrix::IDENTITY )
182 Camera* Camera::New()
191 void Camera::SetNode( const Node* node )
196 void Camera::SetType( Dali::Camera::Type type )
201 void Camera::SetProjectionMode( Dali::Camera::ProjectionMode mode )
203 mProjectionMode = mode;
204 mUpdateProjectionFlag = UPDATE_COUNT;
207 void Camera::SetInvertYAxis( bool invertYAxis )
209 mInvertYAxis = invertYAxis;
210 mUpdateProjectionFlag = UPDATE_COUNT;
213 void Camera::SetFieldOfView( float fieldOfView )
215 mFieldOfView = fieldOfView;
216 mUpdateProjectionFlag = UPDATE_COUNT;
219 void Camera::SetAspectRatio( float aspectRatio )
221 mAspectRatio = aspectRatio;
222 mUpdateProjectionFlag = UPDATE_COUNT;
225 void Camera::SetStereoBias( const Vector2& stereoBias )
227 mStereoBias = stereoBias;
228 mUpdateProjectionFlag = UPDATE_COUNT;
231 void Camera::SetLeftClippingPlane( float leftClippingPlane )
233 mLeftClippingPlane = leftClippingPlane;
234 mUpdateProjectionFlag = UPDATE_COUNT;
237 void Camera::SetRightClippingPlane( float rightClippingPlane )
239 mRightClippingPlane = rightClippingPlane;
240 mUpdateProjectionFlag = UPDATE_COUNT;
243 void Camera::SetTopClippingPlane( float topClippingPlane )
245 mTopClippingPlane = topClippingPlane;
246 mUpdateProjectionFlag = UPDATE_COUNT;
249 void Camera::SetBottomClippingPlane( float bottomClippingPlane )
251 mBottomClippingPlane = bottomClippingPlane;
252 mUpdateProjectionFlag = UPDATE_COUNT;
255 void Camera::SetNearClippingPlane( float nearClippingPlane )
257 mNearClippingPlane = nearClippingPlane;
258 mUpdateProjectionFlag = UPDATE_COUNT;
261 void Camera::SetFarClippingPlane( float farClippingPlane )
263 mFarClippingPlane = farClippingPlane;
264 mUpdateProjectionFlag = UPDATE_COUNT;
267 void Camera::SetTargetPosition( const Vector3& targetPosition )
269 mTargetPosition = targetPosition;
270 mUpdateViewFlag = UPDATE_COUNT;
273 const Matrix& Camera::GetProjectionMatrix( BufferIndex bufferIndex ) const
275 return mProjectionMatrix[ bufferIndex ];
278 const Matrix& Camera::GetViewMatrix( BufferIndex bufferIndex ) const
280 return mViewMatrix[ bufferIndex ];
283 const Matrix& Camera::GetInverseViewProjectionMatrix( BufferIndex bufferIndex ) const
285 return mInverseViewProjection[ bufferIndex ];
288 const PropertyInputImpl* Camera::GetProjectionMatrix() const
290 return &mProjectionMatrix;
293 const PropertyInputImpl* Camera::GetViewMatrix() const
298 void Camera::Update( BufferIndex updateBufferIndex )
300 // if owning node has changes in world position we need to update camera for next 2 frames
301 if( mNode->IsLocalMatrixDirty() )
303 mUpdateViewFlag = UPDATE_COUNT;
305 if( mNode->GetDirtyFlags() & NodePropertyFlags::VISIBLE )
307 // If the visibility changes, the projection matrix needs to be re-calculated.
308 // It may happen the first time an actor is rendered it's rendered only once and becomes invisible,
309 // in the following update the node will be skipped leaving the projection matrix (double buffered)
310 // with the Identity.
311 mUpdateProjectionFlag = UPDATE_COUNT;
314 // if either matrix changed, we need to recalculate the inverse matrix for hit testing to work
315 uint32_t viewUpdateCount = UpdateViewMatrix( updateBufferIndex );
316 uint32_t projectionUpdateCount = UpdateProjection( updateBufferIndex );
318 // if model or view matrix changed we need to either recalculate the inverse VP or copy previous
319 if( viewUpdateCount > COPY_PREVIOUS_MATRIX || projectionUpdateCount > COPY_PREVIOUS_MATRIX )
321 // either has actually changed so recalculate
322 Matrix::Multiply( mInverseViewProjection[ updateBufferIndex ], mViewMatrix[ updateBufferIndex ], mProjectionMatrix[ updateBufferIndex ] );
323 UpdateFrustum( updateBufferIndex );
325 // ignore the error, if the view projection is incorrect (non inversible) then you will have tough times anyways
326 static_cast< void >( mInverseViewProjection[ updateBufferIndex ].Invert() );
328 else if( viewUpdateCount == COPY_PREVIOUS_MATRIX || projectionUpdateCount == COPY_PREVIOUS_MATRIX )
330 // neither has actually changed, but we might copied previous frames value so need to
331 // copy the previous inverse and frustum as well
332 mInverseViewProjection[updateBufferIndex] = mInverseViewProjection[updateBufferIndex ? 0 : 1];
333 mFrustum[ updateBufferIndex ] = mFrustum[ updateBufferIndex ? 0 : 1 ];
337 bool Camera::ViewMatrixUpdated()
339 return 0u != mUpdateViewFlag;
342 uint32_t Camera::UpdateViewMatrix( BufferIndex updateBufferIndex )
344 uint32_t retval( mUpdateViewFlag );
345 if( 0u != mUpdateViewFlag )
347 if( COPY_PREVIOUS_MATRIX == mUpdateViewFlag )
349 // The projection matrix was updated in the previous frame; copy it
350 mViewMatrix.CopyPrevious( updateBufferIndex );
352 else // UPDATE_COUNT == mUpdateViewFlag
356 // camera orientation taken from node - i.e. look in abitrary, unconstrained direction
357 case Dali::Camera::FREE_LOOK:
359 Matrix& viewMatrix = mViewMatrix.Get( updateBufferIndex );
360 viewMatrix = mNode->GetWorldMatrix( updateBufferIndex );
362 mViewMatrix.SetDirty( updateBufferIndex );
365 // camera orientation constrained to look at a target
366 case Dali::Camera::LOOK_AT_TARGET:
368 const Matrix& owningNodeMatrix( mNode->GetWorldMatrix( updateBufferIndex ) );
369 Vector3 position, scale;
370 Quaternion orientation;
371 owningNodeMatrix.GetTransformComponents( position, orientation, scale );
372 Matrix& viewMatrix = mViewMatrix.Get( updateBufferIndex );
373 LookAt( viewMatrix, position, mTargetPosition, orientation.Rotate( Vector3::YAXIS ) );
374 mViewMatrix.SetDirty( updateBufferIndex );
384 void Camera::UpdateFrustum( BufferIndex updateBufferIndex, bool normalize )
387 // Extract the clip matrix planes
389 Matrix::Multiply( clipMatrix, mViewMatrix[ updateBufferIndex ], mProjectionMatrix[ updateBufferIndex ] );
391 const float* cm = clipMatrix.AsFloat();
392 FrustumPlanes& planes = mFrustum[ updateBufferIndex ];
395 planes.mPlanes[ 0 ].mNormal.x = cm[ 3 ] + cm[ 0 ]; // column 4 + column 1
396 planes.mPlanes[ 0 ].mNormal.y = cm[ 7 ] + cm[ 4 ];
397 planes.mPlanes[ 0 ].mNormal.z = cm[ 11 ] + cm[ 8 ];
398 planes.mPlanes[ 0 ].mDistance = cm[ 15 ] + cm[ 12 ];
401 planes.mPlanes[ 1 ].mNormal.x = cm[ 3 ] - cm[ 0 ]; // column 4 - column 1
402 planes.mPlanes[ 1 ].mNormal.y = cm[ 7 ] - cm[ 4 ];
403 planes.mPlanes[ 1 ].mNormal.z = cm[ 11 ] - cm[ 8 ];
404 planes.mPlanes[ 1 ].mDistance = cm[ 15 ] - cm[ 12 ];
407 planes.mPlanes[ 2 ].mNormal.x = cm[ 3 ] + cm[ 1 ]; // column 4 + column 2
408 planes.mPlanes[ 2 ].mNormal.y = cm[ 7 ] + cm[ 5 ];
409 planes.mPlanes[ 2 ].mNormal.z = cm[ 11 ] + cm[ 9 ];
410 planes.mPlanes[ 2 ].mDistance = cm[ 15 ] + cm[ 13 ];
413 planes.mPlanes[ 3 ].mNormal.x = cm[ 3 ] - cm[ 1 ]; // column 4 - column 2
414 planes.mPlanes[ 3 ].mNormal.y = cm[ 7 ] - cm[ 5 ];
415 planes.mPlanes[ 3 ].mNormal.z = cm[ 11 ] - cm[ 9 ];
416 planes.mPlanes[ 3 ].mDistance = cm[ 15 ] - cm[ 13 ];
419 planes.mPlanes[ 4 ].mNormal.x = cm[ 3 ] + cm[ 2 ]; // column 4 + column 3
420 planes.mPlanes[ 4 ].mNormal.y = cm[ 7 ] + cm[ 6 ];
421 planes.mPlanes[ 4 ].mNormal.z = cm[ 11 ] + cm[ 10 ];
422 planes.mPlanes[ 4 ].mDistance = cm[ 15 ] + cm[ 14 ];
425 planes.mPlanes[ 5 ].mNormal.x = cm[ 3 ] - cm[ 2 ]; // column 4 - column 3
426 planes.mPlanes[ 5 ].mNormal.y = cm[ 7 ] - cm[ 6 ];
427 planes.mPlanes[ 5 ].mNormal.z = cm[ 11 ] - cm[ 10 ];
428 planes.mPlanes[ 5 ].mDistance = cm[ 15 ] - cm[ 14 ];
432 for ( uint32_t i = 0; i < 6; ++i )
434 // Normalize planes to ensure correct bounding distance checking
435 Plane& plane = planes.mPlanes[ i ];
436 float l = 1.0f / plane.mNormal.Length();
438 plane.mDistance *= l;
440 planes.mSign[i] = Vector3( Sign(plane.mNormal.x), Sign(plane.mNormal.y), Sign(plane.mNormal.z) );
445 for ( uint32_t i = 0; i < 6; ++i )
447 planes.mSign[i] = Vector3( Sign(planes.mPlanes[ i ].mNormal.x), Sign(planes.mPlanes[ i ].mNormal.y), Sign(planes.mPlanes[ i ].mNormal.z) );
450 mFrustum[ updateBufferIndex ? 0 : 1 ] = planes;
453 bool Camera::CheckSphereInFrustum( BufferIndex bufferIndex, const Vector3& origin, float radius )
455 const FrustumPlanes& planes = mFrustum[ bufferIndex ];
456 for ( uint32_t i = 0; i < 6; ++i )
458 if ( ( planes.mPlanes[ i ].mDistance + planes.mPlanes[ i ].mNormal.Dot( origin ) ) < -radius )
466 bool Camera::CheckAABBInFrustum( BufferIndex bufferIndex, const Vector3& origin, const Vector3& halfExtents )
468 const FrustumPlanes& planes = mFrustum[ bufferIndex ];
469 for ( uint32_t i = 0; i < 6; ++i )
471 if( planes.mPlanes[ i ].mNormal.Dot( origin + (halfExtents * planes.mSign[i]) ) > -(planes.mPlanes[ i ].mDistance) )
481 uint32_t Camera::UpdateProjection( BufferIndex updateBufferIndex )
483 uint32_t retval( mUpdateProjectionFlag );
484 // Early-exit if no update required
485 if ( 0u != mUpdateProjectionFlag )
487 if ( COPY_PREVIOUS_MATRIX == mUpdateProjectionFlag )
489 // The projection matrix was updated in the previous frame; copy it
490 mProjectionMatrix.CopyPrevious( updateBufferIndex );
492 else // UPDATE_COUNT == mUpdateProjectionFlag
494 switch( mProjectionMode )
496 case Dali::Camera::PERSPECTIVE_PROJECTION:
498 Matrix &projectionMatrix = mProjectionMatrix.Get( updateBufferIndex );
499 Perspective( projectionMatrix,
508 case Dali::Camera::ORTHOGRAPHIC_PROJECTION:
510 Matrix &projectionMatrix = mProjectionMatrix.Get( updateBufferIndex );
511 Orthographic( projectionMatrix,
512 mLeftClippingPlane, mRightClippingPlane,
513 mBottomClippingPlane, mTopClippingPlane,
514 mNearClippingPlane, mFarClippingPlane,
520 mProjectionMatrix.SetDirty( updateBufferIndex );
522 --mUpdateProjectionFlag;
527 } // namespace SceneGraph
529 } // namespace Internal