2 * Copyright (c) 2016 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 unsigned int UPDATE_COUNT = 2u; // Update projection or view matrix this many frames after a change
33 const unsigned int COPY_PREVIOUS_MATRIX = 1u; // Copy view or projection matrix from previous frame
48 void LookAt(Matrix& result, const Vector3& eye, const Vector3& target, const Vector3& up)
50 Vector3 vZ = target - eye;
53 Vector3 vX = up.Cross(vZ);
56 Vector3 vY = vZ.Cross(vX);
59 result.SetInverseTransformComponents(vX, vY, vZ, eye);
63 void Frustum(Matrix& result, float left, float right, float bottom, float top, float near, float far, bool invertYAxis)
65 float deltaZ = far - near;
66 if ((near <= 0.0f) || (far <= 0.0f) || Equals(right, left) || Equals(bottom, top) || (deltaZ <= 0.0f))
68 DALI_LOG_ERROR("Invalid parameters passed into Frustum!\n");
69 DALI_ASSERT_DEBUG("Invalid parameters passed into Frustum!");
73 float deltaX = right - left;
74 float deltaY = invertYAxis ? bottom - top : top - bottom;
78 float* m = result.AsFloat();
79 m[0] = -2.0f * near / deltaX;
80 m[1] = m[2] = m[3] = 0.0f;
82 m[5] = -2.0f * near / deltaY;
83 m[4] = m[6] = m[7] = 0.0f;
85 m[8] = (right + left) / deltaX;
86 m[9] = (top + bottom) / deltaY;
87 m[10] = (near + far) / deltaZ;
90 m[14] = -2.0f * near * far / deltaZ;
91 m[12] = m[13] = m[15] = 0.0f;
94 void Perspective(Matrix& result, float fovy, float aspect, float near, float far, bool invertYAxis, const Vector2& stereoBias )
96 float frustumH = tanf( fovy * 0.5f ) * near;
97 float frustumW = frustumH * aspect;
98 Vector2 bias = stereoBias * 0.5f;
100 Frustum(result, -(frustumW + bias.x), frustumW - bias.x, -(frustumH + bias.y), frustumH - bias.y, near, far, invertYAxis);
103 void Orthographic(Matrix& result, float left, float right, float bottom, float top, float near, float far, bool invertYAxis)
105 if ( Equals(right, left) || Equals(top, bottom) || Equals(far, near) )
107 DALI_LOG_ERROR( "Cannot create orthographic projection matrix with a zero dimension.\n" );
108 DALI_ASSERT_DEBUG( "Cannot create orthographic projection matrix with a zero dimension." );
112 float deltaX = right - left;
113 float deltaY = invertYAxis ? bottom - top : top - bottom;
114 float deltaZ = far - near;
116 float *m = result.AsFloat();
117 m[0] = -2.0f / deltaX;
123 m[5] = -2.0f / deltaY;
129 m[10] = 2.0f / deltaZ;
131 m[12] = -(right + left) / deltaX;
132 m[13] = -(top + bottom) / deltaY;
133 m[14] = -(near + far) / deltaZ;
137 } // unnamed namespace
139 const Dali::Camera::Type Camera::DEFAULT_TYPE( Dali::Camera::FREE_LOOK );
140 const Dali::Camera::ProjectionMode Camera::DEFAULT_MODE( Dali::Camera::PERSPECTIVE_PROJECTION );
141 const bool Camera::DEFAULT_INVERT_Y_AXIS( false );
142 const float Camera::DEFAULT_FIELD_OF_VIEW( 45.0f*(M_PI/180.0f) );
143 const float Camera::DEFAULT_ASPECT_RATIO( 4.0f/3.0f );
144 const float Camera::DEFAULT_LEFT_CLIPPING_PLANE(-240.0f);
145 const float Camera::DEFAULT_RIGHT_CLIPPING_PLANE(240.0f);
146 const float Camera::DEFAULT_TOP_CLIPPING_PLANE(-400.0f);
147 const float Camera::DEFAULT_BOTTOM_CLIPPING_PLANE(400.0f);
148 const float Camera::DEFAULT_NEAR_CLIPPING_PLANE( 800.0f ); // default height of the screen
149 const float Camera::DEFAULT_FAR_CLIPPING_PLANE( DEFAULT_NEAR_CLIPPING_PLANE + 2.f * DEFAULT_NEAR_CLIPPING_PLANE );
150 const Vector2 Camera::DEFAULT_STEREO_BIAS( 0.0f, 0.0f );
151 const Vector3 Camera::DEFAULT_TARGET_POSITION( 0.0f, 0.0f, 0.0f );
155 : mUpdateViewFlag( UPDATE_COUNT ),
156 mUpdateProjectionFlag( UPDATE_COUNT ),
157 mType( DEFAULT_TYPE ),
158 mProjectionMode( DEFAULT_MODE ),
159 mInvertYAxis( DEFAULT_INVERT_Y_AXIS ),
160 mFieldOfView( DEFAULT_FIELD_OF_VIEW ),
161 mAspectRatio( DEFAULT_ASPECT_RATIO ),
162 mLeftClippingPlane( DEFAULT_LEFT_CLIPPING_PLANE ),
163 mRightClippingPlane( DEFAULT_RIGHT_CLIPPING_PLANE ),
164 mTopClippingPlane( DEFAULT_TOP_CLIPPING_PLANE ),
165 mBottomClippingPlane( DEFAULT_BOTTOM_CLIPPING_PLANE ),
166 mNearClippingPlane( DEFAULT_NEAR_CLIPPING_PLANE ),
167 mFarClippingPlane( DEFAULT_FAR_CLIPPING_PLANE ),
168 mStereoBias( DEFAULT_STEREO_BIAS ),
169 mTargetPosition( DEFAULT_TARGET_POSITION ),
172 mInverseViewProjection( Matrix::IDENTITY )
176 Camera* Camera::New()
185 void Camera::SetType( Dali::Camera::Type type )
190 void Camera::SetProjectionMode( Dali::Camera::ProjectionMode mode )
192 mProjectionMode = mode;
193 mUpdateProjectionFlag = UPDATE_COUNT;
196 void Camera::SetInvertYAxis( bool invertYAxis )
198 mInvertYAxis = invertYAxis;
199 mUpdateProjectionFlag = UPDATE_COUNT;
202 void Camera::SetFieldOfView( float fieldOfView )
204 mFieldOfView = fieldOfView;
205 mUpdateProjectionFlag = UPDATE_COUNT;
208 void Camera::SetAspectRatio( float aspectRatio )
210 mAspectRatio = aspectRatio;
211 mUpdateProjectionFlag = UPDATE_COUNT;
214 void Camera::SetStereoBias( const Vector2& stereoBias )
216 mStereoBias = stereoBias;
217 mUpdateProjectionFlag = UPDATE_COUNT;
220 void Camera::SetLeftClippingPlane( float leftClippingPlane )
222 mLeftClippingPlane = leftClippingPlane;
223 mUpdateProjectionFlag = UPDATE_COUNT;
226 void Camera::SetRightClippingPlane( float rightClippingPlane )
228 mRightClippingPlane = rightClippingPlane;
229 mUpdateProjectionFlag = UPDATE_COUNT;
232 void Camera::SetTopClippingPlane( float topClippingPlane )
234 mTopClippingPlane = topClippingPlane;
235 mUpdateProjectionFlag = UPDATE_COUNT;
238 void Camera::SetBottomClippingPlane( float bottomClippingPlane )
240 mBottomClippingPlane = bottomClippingPlane;
241 mUpdateProjectionFlag = UPDATE_COUNT;
244 void Camera::SetNearClippingPlane( float nearClippingPlane )
246 mNearClippingPlane = nearClippingPlane;
247 mUpdateProjectionFlag = UPDATE_COUNT;
250 void Camera::SetFarClippingPlane( float farClippingPlane )
252 mFarClippingPlane = farClippingPlane;
253 mUpdateProjectionFlag = UPDATE_COUNT;
256 void Camera::SetTargetPosition( const Vector3& targetPosition )
258 mTargetPosition = targetPosition;
259 mUpdateViewFlag = UPDATE_COUNT;
262 const Matrix& Camera::GetProjectionMatrix( BufferIndex bufferIndex ) const
264 return mProjectionMatrix[ bufferIndex ];
267 const Matrix& Camera::GetViewMatrix( BufferIndex bufferIndex ) const
269 return mViewMatrix[ bufferIndex ];
272 const Matrix& Camera::GetInverseViewProjectionMatrix( BufferIndex bufferIndex ) const
274 return mInverseViewProjection[ bufferIndex ];
277 const PropertyInputImpl* Camera::GetProjectionMatrix() const
279 return &mProjectionMatrix;
282 const PropertyInputImpl* Camera::GetViewMatrix() const
287 void Camera::Update( BufferIndex updateBufferIndex, const Node& owningNode )
289 // if owning node has changes in world position we need to update camera for next 2 frames
290 if( owningNode.IsLocalMatrixDirty() )
292 mUpdateViewFlag = UPDATE_COUNT;
294 if( owningNode.GetDirtyFlags() & VisibleFlag )
296 // If the visibility changes, the projection matrix needs to be re-calculated.
297 // It may happen the first time an actor is rendered it's rendered only once and becomes invisible,
298 // in the following update the node will be skipped leaving the projection matrix (double buffered)
299 // with the Identity.
300 mUpdateProjectionFlag = UPDATE_COUNT;
303 // if either matrix changed, we need to recalculate the inverse matrix for hit testing to work
304 unsigned int viewUpdateCount = UpdateViewMatrix( updateBufferIndex, owningNode );
305 unsigned int projectionUpdateCount = UpdateProjection( updateBufferIndex );
307 // if model or view matrix changed we need to either recalculate the inverse VP or copy previous
308 if( viewUpdateCount > COPY_PREVIOUS_MATRIX || projectionUpdateCount > COPY_PREVIOUS_MATRIX )
310 // either has actually changed so recalculate
311 Matrix::Multiply( mInverseViewProjection[ updateBufferIndex ], mViewMatrix[ updateBufferIndex ], mProjectionMatrix[ updateBufferIndex ] );
312 UpdateFrustum( updateBufferIndex );
314 // ignore the error, if the view projection is incorrect (non inversible) then you will have tough times anyways
315 static_cast< void >( mInverseViewProjection[ updateBufferIndex ].Invert() );
317 else if( viewUpdateCount == COPY_PREVIOUS_MATRIX || projectionUpdateCount == COPY_PREVIOUS_MATRIX )
319 // neither has actually changed, but we might copied previous frames value so need to
320 // copy the previous inverse and frustum as well
321 mInverseViewProjection[updateBufferIndex] = mInverseViewProjection[updateBufferIndex ? 0 : 1];
322 mFrustum[ updateBufferIndex ] = mFrustum[ updateBufferIndex ? 0 : 1 ];
326 bool Camera::ViewMatrixUpdated()
328 return 0u != mUpdateViewFlag;
331 unsigned int Camera::UpdateViewMatrix( BufferIndex updateBufferIndex, const Node& owningNode )
333 unsigned int retval( mUpdateViewFlag );
334 if( 0u != mUpdateViewFlag )
336 if( COPY_PREVIOUS_MATRIX == mUpdateViewFlag )
338 // The projection matrix was updated in the previous frame; copy it
339 mViewMatrix.CopyPrevious( updateBufferIndex );
341 else // UPDATE_COUNT == mUpdateViewFlag
345 // camera orientation taken from node - i.e. look in abitrary, unconstrained direction
346 case Dali::Camera::FREE_LOOK:
348 Matrix& viewMatrix = mViewMatrix.Get( updateBufferIndex );
349 viewMatrix = owningNode.GetWorldMatrix( updateBufferIndex );
351 mViewMatrix.SetDirty( updateBufferIndex );
354 // camera orientation constrained to look at a target
355 case Dali::Camera::LOOK_AT_TARGET:
357 const Matrix& owningNodeMatrix( owningNode.GetWorldMatrix( updateBufferIndex ) );
358 Vector3 position, scale;
359 Quaternion orientation;
360 owningNodeMatrix.GetTransformComponents( position, orientation, scale );
361 Matrix& viewMatrix = mViewMatrix.Get( updateBufferIndex );
362 LookAt( viewMatrix, position, mTargetPosition, orientation.Rotate( Vector3::YAXIS ) );
363 mViewMatrix.SetDirty( updateBufferIndex );
373 void Camera::UpdateFrustum( BufferIndex updateBufferIndex, bool normalize )
376 // Extract the clip matrix planes
378 Matrix::Multiply( clipMatrix, mViewMatrix[ updateBufferIndex ], mProjectionMatrix[ updateBufferIndex ] );
380 const float* cm = clipMatrix.AsFloat();
381 FrustumPlanes& planes = mFrustum[ updateBufferIndex ];
384 planes.mPlanes[ 0 ].mNormal.x = cm[ 3 ] + cm[ 0 ]; // column 4 + column 1
385 planes.mPlanes[ 0 ].mNormal.y = cm[ 7 ] + cm[ 4 ];
386 planes.mPlanes[ 0 ].mNormal.z = cm[ 11 ] + cm[ 8 ];
387 planes.mPlanes[ 0 ].mDistance = cm[ 15 ] + cm[ 12 ];
390 planes.mPlanes[ 1 ].mNormal.x = cm[ 3 ] - cm[ 0 ]; // column 4 - column 1
391 planes.mPlanes[ 1 ].mNormal.y = cm[ 7 ] - cm[ 4 ];
392 planes.mPlanes[ 1 ].mNormal.z = cm[ 11 ] - cm[ 8 ];
393 planes.mPlanes[ 1 ].mDistance = cm[ 15 ] - cm[ 12 ];
396 planes.mPlanes[ 2 ].mNormal.x = cm[ 3 ] + cm[ 1 ]; // column 4 + column 2
397 planes.mPlanes[ 2 ].mNormal.y = cm[ 7 ] + cm[ 5 ];
398 planes.mPlanes[ 2 ].mNormal.z = cm[ 11 ] + cm[ 9 ];
399 planes.mPlanes[ 2 ].mDistance = cm[ 15 ] + cm[ 13 ];
402 planes.mPlanes[ 3 ].mNormal.x = cm[ 3 ] - cm[ 1 ]; // column 4 - column 2
403 planes.mPlanes[ 3 ].mNormal.y = cm[ 7 ] - cm[ 5 ];
404 planes.mPlanes[ 3 ].mNormal.z = cm[ 11 ] - cm[ 9 ];
405 planes.mPlanes[ 3 ].mDistance = cm[ 15 ] - cm[ 13 ];
408 planes.mPlanes[ 4 ].mNormal.x = cm[ 3 ] + cm[ 2 ]; // column 4 + column 3
409 planes.mPlanes[ 4 ].mNormal.y = cm[ 7 ] + cm[ 6 ];
410 planes.mPlanes[ 4 ].mNormal.z = cm[ 11 ] + cm[ 10 ];
411 planes.mPlanes[ 4 ].mDistance = cm[ 15 ] + cm[ 14 ];
414 planes.mPlanes[ 5 ].mNormal.x = cm[ 3 ] - cm[ 2 ]; // column 4 - column 3
415 planes.mPlanes[ 5 ].mNormal.y = cm[ 7 ] - cm[ 6 ];
416 planes.mPlanes[ 5 ].mNormal.z = cm[ 11 ] - cm[ 10 ];
417 planes.mPlanes[ 5 ].mDistance = cm[ 15 ] - cm[ 14 ];
421 for ( unsigned int i = 0; i < 6; ++i )
423 // Normalize planes to ensure correct bounding distance checking
424 Plane& plane = planes.mPlanes[ i ];
425 float l = 1.0f / plane.mNormal.Length();
427 plane.mDistance *= l;
429 planes.mSign[i] = Vector3( Sign(plane.mNormal.x), Sign(plane.mNormal.y), Sign(plane.mNormal.z) );
434 for ( unsigned int i = 0; i < 6; ++i )
436 planes.mSign[i] = Vector3( Sign(planes.mPlanes[ i ].mNormal.x), Sign(planes.mPlanes[ i ].mNormal.y), Sign(planes.mPlanes[ i ].mNormal.z) );
439 mFrustum[ updateBufferIndex ? 0 : 1 ] = planes;
442 bool Camera::CheckSphereInFrustum( BufferIndex bufferIndex, const Vector3& origin, float radius )
444 const FrustumPlanes& planes = mFrustum[ bufferIndex ];
445 for ( uint32_t i = 0; i < 6; ++i )
447 if ( ( planes.mPlanes[ i ].mDistance + planes.mPlanes[ i ].mNormal.Dot( origin ) ) < -radius )
455 bool Camera::CheckAABBInFrustum( BufferIndex bufferIndex, const Vector3& origin, const Vector3& halfExtents )
457 const FrustumPlanes& planes = mFrustum[ bufferIndex ];
458 for ( uint32_t i = 0; i < 6; ++i )
460 if( planes.mPlanes[ i ].mNormal.Dot( origin + (halfExtents * planes.mSign[i]) ) > -(planes.mPlanes[ i ].mDistance) )
470 unsigned int Camera::UpdateProjection( BufferIndex updateBufferIndex )
472 unsigned int retval( mUpdateProjectionFlag );
473 // Early-exit if no update required
474 if ( 0u != mUpdateProjectionFlag )
476 if ( COPY_PREVIOUS_MATRIX == mUpdateProjectionFlag )
478 // The projection matrix was updated in the previous frame; copy it
479 mProjectionMatrix.CopyPrevious( updateBufferIndex );
481 else // UPDATE_COUNT == mUpdateProjectionFlag
483 switch( mProjectionMode )
485 case Dali::Camera::PERSPECTIVE_PROJECTION:
487 Matrix &projectionMatrix = mProjectionMatrix.Get( updateBufferIndex );
488 Perspective( projectionMatrix,
497 case Dali::Camera::ORTHOGRAPHIC_PROJECTION:
499 Matrix &projectionMatrix = mProjectionMatrix.Get( updateBufferIndex );
500 Orthographic( projectionMatrix,
501 mLeftClippingPlane, mRightClippingPlane,
502 mBottomClippingPlane, mTopClippingPlane,
503 mNearClippingPlane, mFarClippingPlane,
509 mProjectionMatrix.SetDirty( updateBufferIndex );
511 --mUpdateProjectionFlag;
516 } // namespace SceneGraph
518 } // namespace Internal