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>
30 namespace // unnamed namespace
32 static unsigned int UPDATE_COUNT = 2u; // Update projection or view matrix this many frames after a change
33 static unsigned int COPY_PREVIOUS_PROJECTION = 1u; // Copy 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!");
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." );
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 CameraAttachment::DEFAULT_TYPE( Dali::Camera::FREE_LOOK );
140 const Dali::Camera::ProjectionMode CameraAttachment::DEFAULT_MODE( Dali::Camera::PERSPECTIVE_PROJECTION );
141 const bool CameraAttachment::DEFAULT_INVERT_Y_AXIS( false );
142 const float CameraAttachment::DEFAULT_FIELD_OF_VIEW( 45.0f*(M_PI/180.0f) );
143 const float CameraAttachment::DEFAULT_ASPECT_RATIO( 4.0f/3.0f );
144 const float CameraAttachment::DEFAULT_LEFT_CLIPPING_PLANE(-240.0f);
145 const float CameraAttachment::DEFAULT_RIGHT_CLIPPING_PLANE(240.0f);
146 const float CameraAttachment::DEFAULT_TOP_CLIPPING_PLANE(-400.0f);
147 const float CameraAttachment::DEFAULT_BOTTOM_CLIPPING_PLANE(400.0f);
148 const float CameraAttachment::DEFAULT_NEAR_CLIPPING_PLANE( 800.0f ); // default height of the screen
149 const float CameraAttachment::DEFAULT_FAR_CLIPPING_PLANE( DEFAULT_NEAR_CLIPPING_PLANE + 2.f * DEFAULT_NEAR_CLIPPING_PLANE );
150 const Vector2 CameraAttachment::DEFAULT_STEREO_BIAS( 0.0f, 0.0f );
151 const Vector3 CameraAttachment::DEFAULT_TARGET_POSITION( 0.0f, 0.0f, 0.0f );
154 CameraAttachment::CameraAttachment()
156 mUpdateViewFlag( UPDATE_COUNT ),
157 mUpdateProjectionFlag( UPDATE_COUNT ),
158 mType( DEFAULT_TYPE ),
159 mProjectionMode( DEFAULT_MODE ),
160 mInvertYAxis( DEFAULT_INVERT_Y_AXIS ),
161 mFieldOfView( DEFAULT_FIELD_OF_VIEW ),
162 mAspectRatio( DEFAULT_ASPECT_RATIO ),
163 mLeftClippingPlane( DEFAULT_LEFT_CLIPPING_PLANE ),
164 mRightClippingPlane( DEFAULT_RIGHT_CLIPPING_PLANE ),
165 mTopClippingPlane( DEFAULT_TOP_CLIPPING_PLANE ),
166 mBottomClippingPlane( DEFAULT_BOTTOM_CLIPPING_PLANE ),
167 mNearClippingPlane( DEFAULT_NEAR_CLIPPING_PLANE ),
168 mFarClippingPlane( DEFAULT_FAR_CLIPPING_PLANE ),
169 mStereoBias( DEFAULT_STEREO_BIAS ),
170 mTargetPosition( DEFAULT_TARGET_POSITION ),
171 mViewMatrix( Matrix::IDENTITY ),
172 mProjectionMatrix( Matrix::IDENTITY ),
173 mInverseViewProjection( Matrix::IDENTITY )
177 CameraAttachment* CameraAttachment::New()
179 return new CameraAttachment();
182 void CameraAttachment::ConnectToSceneGraph( SceneController& sceneController, BufferIndex updateBufferIndex )
187 void CameraAttachment::OnDestroy()
192 CameraAttachment::~CameraAttachment()
196 RenderableAttachment* CameraAttachment::GetRenderable()
201 void CameraAttachment::SetType( Dali::Camera::Type type )
206 void CameraAttachment::SetProjectionMode( Dali::Camera::ProjectionMode mode )
208 mProjectionMode = mode;
209 mUpdateProjectionFlag = UPDATE_COUNT;
212 void CameraAttachment::SetInvertYAxis( bool invertYAxis )
214 mInvertYAxis = invertYAxis;
215 mUpdateProjectionFlag = UPDATE_COUNT;
218 void CameraAttachment::SetFieldOfView( float fieldOfView )
220 mFieldOfView = fieldOfView;
221 mUpdateProjectionFlag = UPDATE_COUNT;
224 void CameraAttachment::SetAspectRatio( float aspectRatio )
226 mAspectRatio = aspectRatio;
227 mUpdateProjectionFlag = UPDATE_COUNT;
230 void CameraAttachment::SetStereoBias( const Vector2& stereoBias )
232 mStereoBias = stereoBias;
233 mUpdateProjectionFlag = UPDATE_COUNT;
236 void CameraAttachment::SetLeftClippingPlane( float leftClippingPlane )
238 mLeftClippingPlane = leftClippingPlane;
239 mUpdateProjectionFlag = UPDATE_COUNT;
242 void CameraAttachment::SetRightClippingPlane( float rightClippingPlane )
244 mRightClippingPlane = rightClippingPlane;
245 mUpdateProjectionFlag = UPDATE_COUNT;
248 void CameraAttachment::SetTopClippingPlane( float topClippingPlane )
250 mTopClippingPlane = topClippingPlane;
251 mUpdateProjectionFlag = UPDATE_COUNT;
254 void CameraAttachment::SetBottomClippingPlane( float bottomClippingPlane )
256 mBottomClippingPlane = bottomClippingPlane;
257 mUpdateProjectionFlag = UPDATE_COUNT;
260 void CameraAttachment::SetNearClippingPlane( float nearClippingPlane )
262 mNearClippingPlane = nearClippingPlane;
263 mUpdateProjectionFlag = UPDATE_COUNT;
266 void CameraAttachment::SetFarClippingPlane( float farClippingPlane )
268 mFarClippingPlane = farClippingPlane;
269 mUpdateProjectionFlag = UPDATE_COUNT;
272 void CameraAttachment::SetTargetPosition( const Vector3& targetPosition )
274 mTargetPosition = targetPosition;
275 mUpdateViewFlag = UPDATE_COUNT;
278 const Matrix& CameraAttachment::GetProjectionMatrix( BufferIndex bufferIndex ) const
280 return mProjectionMatrix[ bufferIndex ];
283 const Matrix& CameraAttachment::GetViewMatrix( BufferIndex bufferIndex ) const
285 return mViewMatrix[ bufferIndex ];
288 const Matrix& CameraAttachment::GetInverseViewProjectionMatrix( BufferIndex bufferIndex ) const
290 return mInverseViewProjection[ bufferIndex ];
293 const PropertyInputImpl* CameraAttachment::GetProjectionMatrix() const
295 return &mProjectionMatrix;
298 const PropertyInputImpl* CameraAttachment::GetViewMatrix() const
303 void CameraAttachment::Update( BufferIndex updateBufferIndex, const Node& owningNode, int nodeDirtyFlags )
305 // if owning node has changes in world position we need to update camera for next 2 frames
306 if( nodeDirtyFlags & TransformFlag )
308 mUpdateViewFlag = UPDATE_COUNT;
310 if( nodeDirtyFlags & VisibleFlag )
312 // If the visibility changes, the projection matrix needs to be re-calculated.
313 // It may happen the first time an actor is rendered it's rendered only once and becomes invisible,
314 // in the following update the node will be skipped leaving the projection matrix (double buffered)
315 // with the Identity.
316 mUpdateProjectionFlag = UPDATE_COUNT;
318 if( 0u != mUpdateViewFlag )
323 // camera orientation taken from node - i.e. look in abitrary, unconstrained direction
324 case Dali::Camera::FREE_LOOK:
326 Matrix& viewMatrix = mViewMatrix.Get(updateBufferIndex);
327 viewMatrix.SetInverseTransformComponents(
329 owningNode.GetWorldRotation(updateBufferIndex),
330 owningNode.GetWorldPosition(updateBufferIndex) );
332 mViewMatrix.SetDirty(updateBufferIndex);
336 // camera orientation constrained to look at a target
337 case Dali::Camera::LOOK_AT_TARGET:
339 Matrix& viewMatrix = mViewMatrix.Get(updateBufferIndex);
341 owningNode.GetWorldPosition(updateBufferIndex),
343 owningNode.GetWorldRotation(updateBufferIndex).Rotate(Vector3::YAXIS) );
345 mViewMatrix.SetDirty(updateBufferIndex);
351 UpdateProjection( updateBufferIndex );
354 bool CameraAttachment::ViewMatrixUpdated()
356 return 0u != mUpdateViewFlag;
359 void CameraAttachment::UpdateProjection( BufferIndex updateBufferIndex )
361 // Early-exit if no update required
362 if ( 0u != mUpdateProjectionFlag )
364 if ( COPY_PREVIOUS_PROJECTION == mUpdateProjectionFlag )
366 // The projection matrix was updated in the previous frame; copy it
367 mProjectionMatrix.CopyPrevious( updateBufferIndex );
369 else // UPDATE_COUNT == mUpdateProjectionFlag
371 switch( mProjectionMode )
373 case Dali::Camera::PERSPECTIVE_PROJECTION:
375 Matrix &projectionMatrix = mProjectionMatrix.Get(updateBufferIndex);
376 Perspective( projectionMatrix,
385 case Dali::Camera::ORTHOGRAPHIC_PROJECTION:
387 Matrix &projectionMatrix = mProjectionMatrix.Get(updateBufferIndex);
388 Orthographic( projectionMatrix,
389 mLeftClippingPlane, mRightClippingPlane,
390 mBottomClippingPlane, mTopClippingPlane,
391 mNearClippingPlane, mFarClippingPlane,
397 mProjectionMatrix.SetDirty(updateBufferIndex);
399 --mUpdateProjectionFlag;
402 // if model or view matrix changed we need to recalculate the inverse VP
403 if( !mViewMatrix.IsClean() || !mProjectionMatrix.IsClean() )
405 UpdateInverseViewProjection( updateBufferIndex );
409 void CameraAttachment::UpdateInverseViewProjection( BufferIndex updateBufferIndex )
411 Matrix::Multiply( mInverseViewProjection[ updateBufferIndex ], mViewMatrix[ updateBufferIndex ], mProjectionMatrix[ updateBufferIndex ] );
412 // ignore the error, if the view projection is incorrect (non inversible) then you will have tough times anyways
413 static_cast< void >( mInverseViewProjection[ updateBufferIndex ].Invert() );
416 } // namespace SceneGraph
418 } // namespace Internal