2 * Copyright (c) 2014 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/node-attachments/scene-graph-camera-attachment.h>
22 #include <dali/public-api/common/dali-common.h>
23 #include <dali/internal/update/nodes/node.h>
24 #include <dali/internal/update/controllers/scene-controller.h>
25 #include <dali/internal/update/resources/resource-manager.h>
26 #include <dali/integration-api/debug.h>
28 namespace // unnamed namespace
30 const unsigned int UPDATE_COUNT = 2u; // Update projection or view matrix this many frames after a change
31 const unsigned int COPY_PREVIOUS_MATRIX = 1u; // Copy view or projection matrix from previous frame
46 void LookAt(Matrix& result, const Vector3& eye, const Vector3& target, const Vector3& up)
48 Vector3 vZ = target - eye;
51 Vector3 vX = up.Cross(vZ);
54 Vector3 vY = vZ.Cross(vX);
57 result.SetInverseTransformComponents(vX, vY, vZ, eye);
61 void Frustum(Matrix& result, float left, float right, float bottom, float top, float near, float far, bool invertYAxis)
63 float deltaZ = far - near;
64 if ((near <= 0.0f) || (far <= 0.0f) || Equals(right, left) || Equals(bottom, top) || (deltaZ <= 0.0f))
66 DALI_LOG_ERROR("Invalid parameters passed into Frustum!");
67 DALI_ASSERT_DEBUG("Invalid parameters passed into Frustum!");
71 float deltaX = right - left;
72 float deltaY = invertYAxis ? bottom - top : top - bottom;
76 float* m = result.AsFloat();
77 m[0] = -2.0f * near / deltaX;
78 m[1] = m[2] = m[3] = 0.0f;
80 m[5] = -2.0f * near / deltaY;
81 m[4] = m[6] = m[7] = 0.0f;
83 m[8] = (right + left) / deltaX;
84 m[9] = (top + bottom) / deltaY;
85 m[10] = (near + far) / deltaZ;
88 m[14] = -2.0f * near * far / deltaZ;
89 m[12] = m[13] = m[15] = 0.0f;
92 void Perspective(Matrix& result, float fovy, float aspect, float near, float far, bool invertYAxis, const Vector2& stereoBias )
94 float frustumH = tanf( fovy * 0.5f ) * near;
95 float frustumW = frustumH * aspect;
96 Vector2 bias = stereoBias * 0.5f;
98 Frustum(result, -(frustumW + bias.x), frustumW - bias.x, -(frustumH + bias.y), frustumH - bias.y, near, far, invertYAxis);
101 void Orthographic(Matrix& result, float left, float right, float bottom, float top, float near, float far, bool invertYAxis)
103 if ( Equals(right, left) || Equals(top, bottom) || Equals(far, near) )
105 DALI_LOG_ERROR( "Cannot create orthographic projection matrix with a zero dimension." );
106 DALI_ASSERT_DEBUG( "Cannot create orthographic projection matrix with a zero dimension." );
110 float deltaX = right - left;
111 float deltaY = invertYAxis ? bottom - top : top - bottom;
112 float deltaZ = far - near;
114 float *m = result.AsFloat();
115 m[0] = -2.0f / deltaX;
121 m[5] = -2.0f / deltaY;
127 m[10] = 2.0f / deltaZ;
129 m[12] = -(right + left) / deltaX;
130 m[13] = -(top + bottom) / deltaY;
131 m[14] = -(near + far) / deltaZ;
135 } // unnamed namespace
137 const Dali::Camera::Type CameraAttachment::DEFAULT_TYPE( Dali::Camera::FREE_LOOK );
138 const Dali::Camera::ProjectionMode CameraAttachment::DEFAULT_MODE( Dali::Camera::PERSPECTIVE_PROJECTION );
139 const bool CameraAttachment::DEFAULT_INVERT_Y_AXIS( false );
140 const float CameraAttachment::DEFAULT_FIELD_OF_VIEW( 45.0f*(M_PI/180.0f) );
141 const float CameraAttachment::DEFAULT_ASPECT_RATIO( 4.0f/3.0f );
142 const float CameraAttachment::DEFAULT_LEFT_CLIPPING_PLANE(-240.0f);
143 const float CameraAttachment::DEFAULT_RIGHT_CLIPPING_PLANE(240.0f);
144 const float CameraAttachment::DEFAULT_TOP_CLIPPING_PLANE(-400.0f);
145 const float CameraAttachment::DEFAULT_BOTTOM_CLIPPING_PLANE(400.0f);
146 const float CameraAttachment::DEFAULT_NEAR_CLIPPING_PLANE( 800.0f ); // default height of the screen
147 const float CameraAttachment::DEFAULT_FAR_CLIPPING_PLANE( DEFAULT_NEAR_CLIPPING_PLANE + 2.f * DEFAULT_NEAR_CLIPPING_PLANE );
148 const Vector2 CameraAttachment::DEFAULT_STEREO_BIAS( 0.0f, 0.0f );
149 const Vector3 CameraAttachment::DEFAULT_TARGET_POSITION( 0.0f, 0.0f, 0.0f );
152 CameraAttachment::CameraAttachment()
154 mUpdateViewFlag( UPDATE_COUNT ),
155 mUpdateProjectionFlag( UPDATE_COUNT ),
156 mType( DEFAULT_TYPE ),
157 mProjectionMode( DEFAULT_MODE ),
158 mInvertYAxis( DEFAULT_INVERT_Y_AXIS ),
159 mFieldOfView( DEFAULT_FIELD_OF_VIEW ),
160 mAspectRatio( DEFAULT_ASPECT_RATIO ),
161 mLeftClippingPlane( DEFAULT_LEFT_CLIPPING_PLANE ),
162 mRightClippingPlane( DEFAULT_RIGHT_CLIPPING_PLANE ),
163 mTopClippingPlane( DEFAULT_TOP_CLIPPING_PLANE ),
164 mBottomClippingPlane( DEFAULT_BOTTOM_CLIPPING_PLANE ),
165 mNearClippingPlane( DEFAULT_NEAR_CLIPPING_PLANE ),
166 mFarClippingPlane( DEFAULT_FAR_CLIPPING_PLANE ),
167 mStereoBias( DEFAULT_STEREO_BIAS ),
168 mTargetPosition( DEFAULT_TARGET_POSITION ),
171 mInverseViewProjection( Matrix::IDENTITY )
175 CameraAttachment* CameraAttachment::New()
177 return new CameraAttachment();
180 void CameraAttachment::Initialize( SceneController& sceneController, BufferIndex updateBufferIndex )
185 void CameraAttachment::OnDestroy()
190 void CameraAttachment::ConnectedToSceneGraph()
195 void CameraAttachment::DisconnectedFromSceneGraph()
200 CameraAttachment::~CameraAttachment()
204 RenderableAttachment* CameraAttachment::GetRenderable()
209 void CameraAttachment::SetType( Dali::Camera::Type type )
214 void CameraAttachment::SetProjectionMode( Dali::Camera::ProjectionMode mode )
216 mProjectionMode = mode;
217 mUpdateProjectionFlag = UPDATE_COUNT;
220 void CameraAttachment::SetInvertYAxis( bool invertYAxis )
222 mInvertYAxis = invertYAxis;
223 mUpdateProjectionFlag = UPDATE_COUNT;
226 void CameraAttachment::SetFieldOfView( float fieldOfView )
228 mFieldOfView = fieldOfView;
229 mUpdateProjectionFlag = UPDATE_COUNT;
232 void CameraAttachment::SetAspectRatio( float aspectRatio )
234 mAspectRatio = aspectRatio;
235 mUpdateProjectionFlag = UPDATE_COUNT;
238 void CameraAttachment::SetStereoBias( const Vector2& stereoBias )
240 mStereoBias = stereoBias;
241 mUpdateProjectionFlag = UPDATE_COUNT;
244 void CameraAttachment::SetLeftClippingPlane( float leftClippingPlane )
246 mLeftClippingPlane = leftClippingPlane;
247 mUpdateProjectionFlag = UPDATE_COUNT;
250 void CameraAttachment::SetRightClippingPlane( float rightClippingPlane )
252 mRightClippingPlane = rightClippingPlane;
253 mUpdateProjectionFlag = UPDATE_COUNT;
256 void CameraAttachment::SetTopClippingPlane( float topClippingPlane )
258 mTopClippingPlane = topClippingPlane;
259 mUpdateProjectionFlag = UPDATE_COUNT;
262 void CameraAttachment::SetBottomClippingPlane( float bottomClippingPlane )
264 mBottomClippingPlane = bottomClippingPlane;
265 mUpdateProjectionFlag = UPDATE_COUNT;
268 void CameraAttachment::SetNearClippingPlane( float nearClippingPlane )
270 mNearClippingPlane = nearClippingPlane;
271 mUpdateProjectionFlag = UPDATE_COUNT;
274 void CameraAttachment::SetFarClippingPlane( float farClippingPlane )
276 mFarClippingPlane = farClippingPlane;
277 mUpdateProjectionFlag = UPDATE_COUNT;
280 void CameraAttachment::SetTargetPosition( const Vector3& targetPosition )
282 mTargetPosition = targetPosition;
283 mUpdateViewFlag = UPDATE_COUNT;
286 const Matrix& CameraAttachment::GetProjectionMatrix( BufferIndex bufferIndex ) const
288 return mProjectionMatrix[ bufferIndex ];
291 const Matrix& CameraAttachment::GetViewMatrix( BufferIndex bufferIndex ) const
293 return mViewMatrix[ bufferIndex ];
296 const Matrix& CameraAttachment::GetInverseViewProjectionMatrix( BufferIndex bufferIndex ) const
298 return mInverseViewProjection[ bufferIndex ];
301 const PropertyInputImpl* CameraAttachment::GetProjectionMatrix() const
303 return &mProjectionMatrix;
306 const PropertyInputImpl* CameraAttachment::GetViewMatrix() const
311 void CameraAttachment::Update( BufferIndex updateBufferIndex, const Node& owningNode, int nodeDirtyFlags )
313 // if owning node has changes in world position we need to update camera for next 2 frames
314 if( nodeDirtyFlags & TransformFlag )
316 mUpdateViewFlag = UPDATE_COUNT;
318 if( nodeDirtyFlags & VisibleFlag )
320 // If the visibility changes, the projection matrix needs to be re-calculated.
321 // It may happen the first time an actor is rendered it's rendered only once and becomes invisible,
322 // in the following update the node will be skipped leaving the projection matrix (double buffered)
323 // with the Identity.
324 mUpdateProjectionFlag = UPDATE_COUNT;
327 // if either matrix changed, we need to recalculate the inverse matrix for hit testing to work
328 unsigned int viewUpdateCount = UpdateViewMatrix( updateBufferIndex, owningNode );
329 unsigned int projectionUpdateCount = UpdateProjection( updateBufferIndex );
331 // if model or view matrix changed we need to either recalculate the inverse VP or copy previous
332 if( viewUpdateCount > COPY_PREVIOUS_MATRIX || projectionUpdateCount > COPY_PREVIOUS_MATRIX )
334 // either has actually changed so recalculate
335 Matrix::Multiply( mInverseViewProjection[ updateBufferIndex ], mViewMatrix[ updateBufferIndex ], mProjectionMatrix[ updateBufferIndex ] );
336 // ignore the error, if the view projection is incorrect (non inversible) then you will have tough times anyways
337 static_cast< void >( mInverseViewProjection[ updateBufferIndex ].Invert() );
339 else if( viewUpdateCount == COPY_PREVIOUS_MATRIX || projectionUpdateCount == COPY_PREVIOUS_MATRIX )
341 // neither has actually changed, but we might copied previous frames value so need to
342 // copy the previous inverse as well
343 mInverseViewProjection[updateBufferIndex] = mInverseViewProjection[updateBufferIndex ? 0 : 1];
347 bool CameraAttachment::ViewMatrixUpdated()
349 return 0u != mUpdateViewFlag;
352 unsigned int CameraAttachment::UpdateViewMatrix( BufferIndex updateBufferIndex, const Node& owningNode )
354 unsigned int retval( mUpdateViewFlag );
355 if( 0u != mUpdateViewFlag )
357 if( COPY_PREVIOUS_MATRIX == mUpdateViewFlag )
359 // The projection matrix was updated in the previous frame; copy it
360 mViewMatrix.CopyPrevious( updateBufferIndex );
362 else // UPDATE_COUNT == mUpdateViewFlag
366 // camera orientation taken from node - i.e. look in abitrary, unconstrained direction
367 case Dali::Camera::FREE_LOOK:
369 Matrix& viewMatrix = mViewMatrix.Get( updateBufferIndex );
370 viewMatrix.SetInverseTransformComponents( Vector3::ONE, owningNode.GetWorldOrientation( updateBufferIndex ),
371 owningNode.GetWorldPosition( updateBufferIndex ) );
372 mViewMatrix.SetDirty( updateBufferIndex );
375 // camera orientation constrained to look at a target
376 case Dali::Camera::LOOK_AT_TARGET:
378 Matrix& viewMatrix = mViewMatrix.Get( updateBufferIndex );
379 LookAt( viewMatrix, owningNode.GetWorldPosition( updateBufferIndex ), mTargetPosition,
380 owningNode.GetWorldOrientation( updateBufferIndex ).Rotate( Vector3::YAXIS ) );
381 mViewMatrix.SetDirty( updateBufferIndex );
391 unsigned int CameraAttachment::UpdateProjection( BufferIndex updateBufferIndex )
393 unsigned int retval( mUpdateProjectionFlag );
394 // Early-exit if no update required
395 if ( 0u != mUpdateProjectionFlag )
397 if ( COPY_PREVIOUS_MATRIX == mUpdateProjectionFlag )
399 // The projection matrix was updated in the previous frame; copy it
400 mProjectionMatrix.CopyPrevious( updateBufferIndex );
402 else // UPDATE_COUNT == mUpdateProjectionFlag
404 switch( mProjectionMode )
406 case Dali::Camera::PERSPECTIVE_PROJECTION:
408 Matrix &projectionMatrix = mProjectionMatrix.Get(updateBufferIndex);
409 Perspective( projectionMatrix,
418 case Dali::Camera::ORTHOGRAPHIC_PROJECTION:
420 Matrix &projectionMatrix = mProjectionMatrix.Get(updateBufferIndex);
421 Orthographic( projectionMatrix,
422 mLeftClippingPlane, mRightClippingPlane,
423 mBottomClippingPlane, mTopClippingPlane,
424 mNearClippingPlane, mFarClippingPlane,
430 mProjectionMatrix.SetDirty(updateBufferIndex);
432 --mUpdateProjectionFlag;
437 } // namespace SceneGraph
439 } // namespace Internal