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::ConnectToSceneGraph( SceneController& sceneController, BufferIndex updateBufferIndex )
185 void CameraAttachment::OnDestroy()
190 CameraAttachment::~CameraAttachment()
194 RenderableAttachment* CameraAttachment::GetRenderable()
199 void CameraAttachment::SetType( Dali::Camera::Type type )
204 void CameraAttachment::SetProjectionMode( Dali::Camera::ProjectionMode mode )
206 mProjectionMode = mode;
207 mUpdateProjectionFlag = UPDATE_COUNT;
210 void CameraAttachment::SetInvertYAxis( bool invertYAxis )
212 mInvertYAxis = invertYAxis;
213 mUpdateProjectionFlag = UPDATE_COUNT;
216 void CameraAttachment::SetFieldOfView( float fieldOfView )
218 mFieldOfView = fieldOfView;
219 mUpdateProjectionFlag = UPDATE_COUNT;
222 void CameraAttachment::SetAspectRatio( float aspectRatio )
224 mAspectRatio = aspectRatio;
225 mUpdateProjectionFlag = UPDATE_COUNT;
228 void CameraAttachment::SetStereoBias( const Vector2& stereoBias )
230 mStereoBias = stereoBias;
231 mUpdateProjectionFlag = UPDATE_COUNT;
234 void CameraAttachment::SetLeftClippingPlane( float leftClippingPlane )
236 mLeftClippingPlane = leftClippingPlane;
237 mUpdateProjectionFlag = UPDATE_COUNT;
240 void CameraAttachment::SetRightClippingPlane( float rightClippingPlane )
242 mRightClippingPlane = rightClippingPlane;
243 mUpdateProjectionFlag = UPDATE_COUNT;
246 void CameraAttachment::SetTopClippingPlane( float topClippingPlane )
248 mTopClippingPlane = topClippingPlane;
249 mUpdateProjectionFlag = UPDATE_COUNT;
252 void CameraAttachment::SetBottomClippingPlane( float bottomClippingPlane )
254 mBottomClippingPlane = bottomClippingPlane;
255 mUpdateProjectionFlag = UPDATE_COUNT;
258 void CameraAttachment::SetNearClippingPlane( float nearClippingPlane )
260 mNearClippingPlane = nearClippingPlane;
261 mUpdateProjectionFlag = UPDATE_COUNT;
264 void CameraAttachment::SetFarClippingPlane( float farClippingPlane )
266 mFarClippingPlane = farClippingPlane;
267 mUpdateProjectionFlag = UPDATE_COUNT;
270 void CameraAttachment::SetTargetPosition( const Vector3& targetPosition )
272 mTargetPosition = targetPosition;
273 mUpdateViewFlag = UPDATE_COUNT;
276 const Matrix& CameraAttachment::GetProjectionMatrix( BufferIndex bufferIndex ) const
278 return mProjectionMatrix[ bufferIndex ];
281 const Matrix& CameraAttachment::GetViewMatrix( BufferIndex bufferIndex ) const
283 return mViewMatrix[ bufferIndex ];
286 const Matrix& CameraAttachment::GetInverseViewProjectionMatrix( BufferIndex bufferIndex ) const
288 return mInverseViewProjection[ bufferIndex ];
291 const PropertyInputImpl* CameraAttachment::GetProjectionMatrix() const
293 return &mProjectionMatrix;
296 const PropertyInputImpl* CameraAttachment::GetViewMatrix() const
301 void CameraAttachment::Update( BufferIndex updateBufferIndex, const Node& owningNode, int nodeDirtyFlags )
303 // if owning node has changes in world position we need to update camera for next 2 frames
304 if( nodeDirtyFlags & TransformFlag )
306 mUpdateViewFlag = UPDATE_COUNT;
308 if( nodeDirtyFlags & VisibleFlag )
310 // If the visibility changes, the projection matrix needs to be re-calculated.
311 // It may happen the first time an actor is rendered it's rendered only once and becomes invisible,
312 // in the following update the node will be skipped leaving the projection matrix (double buffered)
313 // with the Identity.
314 mUpdateProjectionFlag = UPDATE_COUNT;
317 // if either matrix changed, we need to recalculate the inverse matrix for hit testing to work
318 unsigned int viewUpdateCount = UpdateViewMatrix( updateBufferIndex, owningNode );
319 unsigned int projectionUpdateCount = UpdateProjection( updateBufferIndex );
321 // if model or view matrix changed we need to either recalculate the inverse VP or copy previous
322 if( viewUpdateCount > COPY_PREVIOUS_MATRIX || projectionUpdateCount > COPY_PREVIOUS_MATRIX )
324 // either has actually changed so recalculate
325 Matrix::Multiply( mInverseViewProjection[ updateBufferIndex ], mViewMatrix[ updateBufferIndex ], mProjectionMatrix[ updateBufferIndex ] );
326 // ignore the error, if the view projection is incorrect (non inversible) then you will have tough times anyways
327 static_cast< void >( mInverseViewProjection[ updateBufferIndex ].Invert() );
329 else if( viewUpdateCount == COPY_PREVIOUS_MATRIX || projectionUpdateCount == COPY_PREVIOUS_MATRIX )
331 // neither has actually changed, but we might copied previous frames value so need to
332 // copy the previous inverse as well
333 mInverseViewProjection[updateBufferIndex] = mInverseViewProjection[updateBufferIndex ? 0 : 1];
337 bool CameraAttachment::ViewMatrixUpdated()
339 return 0u != mUpdateViewFlag;
342 unsigned int CameraAttachment::UpdateViewMatrix( BufferIndex updateBufferIndex, const Node& owningNode )
344 unsigned int 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.SetInverseTransformComponents( Vector3::ONE, owningNode.GetWorldOrientation( updateBufferIndex ),
361 owningNode.GetWorldPosition( updateBufferIndex ) );
362 mViewMatrix.SetDirty( updateBufferIndex );
365 // camera orientation constrained to look at a target
366 case Dali::Camera::LOOK_AT_TARGET:
368 Matrix& viewMatrix = mViewMatrix.Get( updateBufferIndex );
369 LookAt( viewMatrix, owningNode.GetWorldPosition( updateBufferIndex ), mTargetPosition,
370 owningNode.GetWorldOrientation( updateBufferIndex ).Rotate( Vector3::YAXIS ) );
371 mViewMatrix.SetDirty( updateBufferIndex );
381 unsigned int CameraAttachment::UpdateProjection( BufferIndex updateBufferIndex )
383 unsigned int retval( mUpdateProjectionFlag );
384 // Early-exit if no update required
385 if ( 0u != mUpdateProjectionFlag )
387 if ( COPY_PREVIOUS_MATRIX == mUpdateProjectionFlag )
389 // The projection matrix was updated in the previous frame; copy it
390 mProjectionMatrix.CopyPrevious( updateBufferIndex );
392 else // UPDATE_COUNT == mUpdateProjectionFlag
394 switch( mProjectionMode )
396 case Dali::Camera::PERSPECTIVE_PROJECTION:
398 Matrix &projectionMatrix = mProjectionMatrix.Get(updateBufferIndex);
399 Perspective( projectionMatrix,
408 case Dali::Camera::ORTHOGRAPHIC_PROJECTION:
410 Matrix &projectionMatrix = mProjectionMatrix.Get(updateBufferIndex);
411 Orthographic( projectionMatrix,
412 mLeftClippingPlane, mRightClippingPlane,
413 mBottomClippingPlane, mTopClippingPlane,
414 mNearClippingPlane, mFarClippingPlane,
420 mProjectionMatrix.SetDirty(updateBufferIndex);
422 --mUpdateProjectionFlag;
427 } // namespace SceneGraph
429 } // namespace Internal