2 * Copyright (c) 2019 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/event/actors/camera-actor-impl.h>
23 #include <cstring> // for strcmp
26 #include <dali/public-api/common/stage.h>
27 #include <dali/public-api/object/type-registry.h>
28 #include <dali/integration-api/debug.h>
29 #include <dali/internal/event/common/property-helper.h>
30 #include <dali/internal/event/common/stage-impl.h>
31 #include <dali/internal/event/common/scene-impl.h>
32 #include <dali/internal/event/render-tasks/render-task-impl.h>
33 #include <dali/internal/event/render-tasks/render-task-list-impl.h>
34 #include <dali/internal/event/common/projection.h>
35 #include <dali/internal/event/common/thread-local-storage.h>
36 #include <dali/internal/update/render-tasks/scene-graph-camera.h>
50 * We want to discourage the use of property strings (minimize string comparisons),
51 * particularly for the default properties.
52 * Name Type writable animatable constraint-input enum for index-checking
54 DALI_PROPERTY_TABLE_BEGIN
55 DALI_PROPERTY( "type", STRING, true, false, true, Dali::CameraActor::Property::TYPE )
56 DALI_PROPERTY( "projectionMode", STRING, true, false, true, Dali::CameraActor::Property::PROJECTION_MODE )
57 DALI_PROPERTY( "fieldOfView", FLOAT, true, false, true, Dali::CameraActor::Property::FIELD_OF_VIEW )
58 DALI_PROPERTY( "aspectRatio", FLOAT, true, false, true, Dali::CameraActor::Property::ASPECT_RATIO )
59 DALI_PROPERTY( "nearPlaneDistance", FLOAT, true, false, true, Dali::CameraActor::Property::NEAR_PLANE_DISTANCE )
60 DALI_PROPERTY( "farPlaneDistance", FLOAT, true, false, true, Dali::CameraActor::Property::FAR_PLANE_DISTANCE )
61 DALI_PROPERTY( "leftPlaneDistance", FLOAT, true, false, true, Dali::CameraActor::Property::LEFT_PLANE_DISTANCE )
62 DALI_PROPERTY( "rightPlaneDistance", FLOAT, true, false, true, Dali::CameraActor::Property::RIGHT_PLANE_DISTANCE )
63 DALI_PROPERTY( "topPlaneDistance", FLOAT, true, false, true, Dali::CameraActor::Property::TOP_PLANE_DISTANCE )
64 DALI_PROPERTY( "bottomPlaneDistance", FLOAT, true, false, true, Dali::CameraActor::Property::BOTTOM_PLANE_DISTANCE )
65 DALI_PROPERTY( "targetPosition", VECTOR3, true, false, true, Dali::CameraActor::Property::TARGET_POSITION )
66 DALI_PROPERTY( "projectionMatrix", MATRIX, false, false, true, Dali::CameraActor::Property::PROJECTION_MATRIX )
67 DALI_PROPERTY( "viewMatrix", MATRIX, false, false, true, Dali::CameraActor::Property::VIEW_MATRIX )
68 DALI_PROPERTY( "invertYAxis", BOOLEAN, true, false, true, Dali::CameraActor::Property::INVERT_Y_AXIS )
69 DALI_PROPERTY_TABLE_END( DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX, CameraDefaultProperties )
71 // calculate the far plane distance for a 16bit depth buffer with 4 bits per unit precision
72 void CalculateClippingAndZ( float width, float height, float& nearClippingPlane, float& farClippingPlane, float& cameraZ )
74 nearClippingPlane = std::max( width, height );
75 farClippingPlane = nearClippingPlane + static_cast<float>( 0xFFFF >> 4 );
76 cameraZ = 2.0f * nearClippingPlane;
81 return Dali::CameraActor::New();
84 TypeRegistration mType( typeid( Dali::CameraActor ), typeid( Dali::Actor ), Create, CameraDefaultProperties );
87 * Builds the picking ray in the world reference system from an orthographic camera
88 * The ray origin is the screen coordinate in the near plane translated to a parallel
89 * plane at the camera origin. The ray direction is the direction the camera is facing
90 * (i.e. Z=-1 in view space).
92 void BuildOrthoPickingRay( const Matrix& viewMatrix,
93 const Matrix& projectionMatrix,
94 const Viewport& viewport,
99 float nearPlaneDistance )
101 // inv( modelMatrix ) inv( viewMatrix ) inv( projectionMatrix ) normalize
102 // <----------------- <----------------- <-------------- <-------------
103 // Local World Camera Normalized Screen
104 // reference reference reference clip coordinates
105 // system system system coordinates
106 // -----------------> -----------------> --------------> ------------->
107 // modelMatrix viewMatrix projectionMatrix viewport
109 // Transforms the touch point from the screen reference system to the world reference system.
110 Matrix invViewProjection( false ); // Don't initialize.
111 Matrix::Multiply( invViewProjection, viewMatrix, projectionMatrix );
112 if( !invViewProjection.Invert() )
114 DALI_ASSERT_DEBUG( false );
117 Vector4 near( screenX - static_cast<float>( viewport.x ),
118 static_cast<float>( viewport.height ) - (screenY - static_cast<float>( viewport.y ) ),
120 if( !Unproject( near, invViewProjection, static_cast<float>( viewport.width ), static_cast<float>( viewport.height ), rayOrigin ) )
122 DALI_ASSERT_DEBUG( false );
125 Matrix invView = viewMatrix;
126 if( !invView.Invert() )
128 DALI_ASSERT_DEBUG( false );
131 Vector4 cameraOrigin = invView * Vector4( 0.f, 0.f, 0.f, 1.f );
132 Vector4 nearPlaneOrigin = invView * Vector4( 0.0f, 0.0f, -nearPlaneDistance, 1.0f);
134 // Vector pointing from the camera to the near plane
135 rayDir = cameraOrigin - nearPlaneOrigin;
143 CameraActorPtr CameraActor::New( const Size& size )
145 CameraActorPtr actor( new CameraActor( *CreateNode() ) );
147 // Second-phase construction
150 actor->SetName( "DefaultCamera" );
151 actor->SetPerspectiveProjection( size );
153 // By default Actors face in the positive Z direction in world space
154 // CameraActors should face in the negative Z direction, towards the other actors
155 actor->SetOrientation( Quaternion( Dali::ANGLE_180, Vector3::YAXIS ) );
160 CameraActor::CameraActor( const SceneGraph::Node& node )
161 : Actor( Actor::BASIC, node ),
162 mSceneObject( NULL ),
163 mTarget( SceneGraph::Camera::DEFAULT_TARGET_POSITION ),
164 mType( SceneGraph::Camera::DEFAULT_TYPE ),
165 mProjectionMode( SceneGraph::Camera::DEFAULT_MODE ),
166 mFieldOfView( SceneGraph::Camera::DEFAULT_FIELD_OF_VIEW ),
167 mAspectRatio( SceneGraph::Camera::DEFAULT_ASPECT_RATIO ),
168 mNearClippingPlane( SceneGraph::Camera::DEFAULT_NEAR_CLIPPING_PLANE ),
169 mFarClippingPlane( SceneGraph::Camera::DEFAULT_FAR_CLIPPING_PLANE ),
170 mLeftClippingPlane( SceneGraph::Camera::DEFAULT_LEFT_CLIPPING_PLANE ),
171 mRightClippingPlane( SceneGraph::Camera::DEFAULT_RIGHT_CLIPPING_PLANE ),
172 mTopClippingPlane( SceneGraph::Camera::DEFAULT_TOP_CLIPPING_PLANE ),
173 mBottomClippingPlane( SceneGraph::Camera::DEFAULT_BOTTOM_CLIPPING_PLANE ),
174 mInvertYAxis( SceneGraph::Camera::DEFAULT_INVERT_Y_AXIS )
178 CameraActor::~CameraActor()
180 if( EventThreadServices::IsCoreRunning() )
182 // Create scene-object and transfer ownership through message
183 RemoveCameraMessage( GetEventThreadServices().GetUpdateManager(), mSceneObject );
187 void CameraActor::OnInitialize()
189 // Create scene-object and keep raw pointer for message passing.
190 SceneGraph::Camera* sceneGraphCamera = SceneGraph::Camera::New();
192 // Store a pointer to this camera node inside the scene-graph camera.
193 sceneGraphCamera->SetNode( &GetNode() );
195 mSceneObject = sceneGraphCamera;
196 OwnerPointer< SceneGraph::Camera > sceneGraphCameraOwner( sceneGraphCamera );
198 // Send message to inform update of this camera (and move ownership).
199 AddCameraMessage( GetEventThreadServices().GetUpdateManager(), sceneGraphCameraOwner );
202 void CameraActor::OnStageConnectionInternal()
204 // If the canvas size has not been set, then use the size of the scene we've been added to to set up the perspective projection
205 if( ( mCanvasSize.width < Math::MACHINE_EPSILON_1000 ) || ( mCanvasSize.height < Math::MACHINE_EPSILON_1000 ) )
207 SetPerspectiveProjection( GetScene().GetSize() );
211 void CameraActor::SetTarget( const Vector3& target )
213 if( target != mTarget ) // using range epsilon
217 SetTargetPositionMessage( GetEventThreadServices(), *mSceneObject, mTarget );
221 Vector3 CameraActor::GetTarget() const
226 void CameraActor::SetType( Dali::Camera::Type type )
232 // sceneObject is being used in a separate thread; queue a message to set
233 SetTypeMessage( GetEventThreadServices(), *mSceneObject, mType );
237 Dali::Camera::Type CameraActor::GetType() const
242 void CameraActor::SetProjectionMode( Dali::Camera::ProjectionMode mode )
244 if( mode != mProjectionMode )
246 mProjectionMode = mode;
248 // sceneObject is being used in a separate thread; queue a message to set
249 SetProjectionModeMessage( GetEventThreadServices(), *mSceneObject, mProjectionMode );
253 Dali::Camera::ProjectionMode CameraActor::GetProjectionMode() const
255 return mProjectionMode;
258 void CameraActor::SetFieldOfView( float fieldOfView )
260 if( ! Equals( fieldOfView, mFieldOfView ) )
262 mFieldOfView = fieldOfView;
264 // sceneObject is being used in a separate thread; queue a message to set
265 SetFieldOfViewMessage( GetEventThreadServices(), *mSceneObject, mFieldOfView );
269 float CameraActor::GetFieldOfView() const
274 void CameraActor::SetAspectRatio( float aspectRatio )
276 if( ! Equals( aspectRatio, mAspectRatio ) )
278 mAspectRatio = aspectRatio;
280 // sceneObject is being used in a separate thread; queue a message to set
281 SetAspectRatioMessage( GetEventThreadServices(), *mSceneObject, mAspectRatio );
285 float CameraActor::GetAspectRatio() const
290 void CameraActor::SetNearClippingPlane( float nearClippingPlane )
292 if( ! Equals( nearClippingPlane, mNearClippingPlane ) )
294 mNearClippingPlane = nearClippingPlane;
296 // sceneObject is being used in a separate thread; queue a message to set
297 SetNearClippingPlaneMessage( GetEventThreadServices(), *mSceneObject, mNearClippingPlane );
301 float CameraActor::GetNearClippingPlane() const
303 return mNearClippingPlane;
306 void CameraActor::SetFarClippingPlane( float farClippingPlane )
308 if( ! Equals( farClippingPlane, mFarClippingPlane ) )
310 mFarClippingPlane = farClippingPlane;
312 // sceneObject is being used in a separate thread; queue a message to set
313 SetFarClippingPlaneMessage( GetEventThreadServices(), *mSceneObject, mFarClippingPlane );
317 float CameraActor::GetFarClippingPlane() const
319 return mFarClippingPlane;
322 void CameraActor::SetLeftClippingPlane( float leftClippingPlane )
324 if( ! Equals( leftClippingPlane, mLeftClippingPlane ) )
326 mLeftClippingPlane = leftClippingPlane;
328 // sceneObject is being used in a separate thread; queue a message to set
329 SetLeftClippingPlaneMessage( GetEventThreadServices(), *mSceneObject, mLeftClippingPlane );
333 void CameraActor::SetRightClippingPlane( float rightClippingPlane )
335 if( ! Equals( rightClippingPlane, mRightClippingPlane ) )
337 mRightClippingPlane = rightClippingPlane;
339 // sceneObject is being used in a separate thread; queue a message to set
340 SetRightClippingPlaneMessage( GetEventThreadServices(), *mSceneObject, mRightClippingPlane );
344 void CameraActor::SetTopClippingPlane( float topClippingPlane )
346 if( ! Equals( topClippingPlane, mTopClippingPlane ) )
348 mTopClippingPlane = topClippingPlane;
350 // sceneObject is being used in a separate thread; queue a message to set
351 SetTopClippingPlaneMessage( GetEventThreadServices(), *mSceneObject, mTopClippingPlane );
355 void CameraActor::SetBottomClippingPlane( float bottomClippingPlane )
357 if( ! Equals( bottomClippingPlane, mBottomClippingPlane ) )
359 mBottomClippingPlane = bottomClippingPlane;
361 // sceneObject is being used in a separate thread; queue a message to set
362 SetBottomClippingPlaneMessage( GetEventThreadServices(), *mSceneObject, mBottomClippingPlane );
366 void CameraActor::SetInvertYAxis(bool invertYAxis)
368 if( invertYAxis != mInvertYAxis )
370 mInvertYAxis = invertYAxis;
372 // sceneObject is being used in a separate thread; queue a message to set
373 SetInvertYAxisMessage( GetEventThreadServices(), *mSceneObject, mInvertYAxis );
377 bool CameraActor::GetInvertYAxis() const
382 void CameraActor::SetPerspectiveProjection( const Size& size )
386 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
388 // If the size given is invalid, i.e. ZERO, then check if we've been added to a scene
391 // We've been added to a scene already, set the canvas size to the scene's size
392 mCanvasSize = GetScene().GetSize();
396 // We've not been added to a scene yet, so just return.
397 // We'll set the canvas size when we get added to a scene later
402 float width = mCanvasSize.width;
403 float height = mCanvasSize.height;
405 float nearClippingPlane;
406 float farClippingPlane;
408 CalculateClippingAndZ( width, height, nearClippingPlane, farClippingPlane, cameraZ );
410 // calculate the position of the camera to have the desired aspect ratio
411 const float fieldOfView = 2.0f * std::atan( height * 0.5f / cameraZ );
413 // unless it is too small, we want at least as much space to the back as we have torwards the front
414 const float minClippingFarPlane = 2.f * nearClippingPlane;
415 if ( farClippingPlane < minClippingFarPlane )
417 farClippingPlane = minClippingFarPlane;
420 const float aspectRatio = width / height;
422 // sceneObject is being used in a separate thread; queue a message to set
423 SetProjectionMode(Dali::Camera::PERSPECTIVE_PROJECTION);
424 SetFieldOfView( fieldOfView );
425 SetNearClippingPlane( nearClippingPlane );
426 SetFarClippingPlane( farClippingPlane );
427 SetAspectRatio( aspectRatio );
432 void CameraActor::SetOrthographicProjection( const Vector2& size )
434 // Choose near, far and Z parameters to match the SetPerspectiveProjection above.
435 float nearClippingPlane;
436 float farClippingPlane;
438 CalculateClippingAndZ( size.width, size.height, nearClippingPlane, farClippingPlane, cameraZ );
439 SetOrthographicProjection( -size.x*0.5f, size.x*0.5f, size.y*0.5f, -size.y*0.5f,
440 nearClippingPlane, farClippingPlane );
444 void CameraActor::SetOrthographicProjection( float left, float right, float top, float bottom, float near, float far )
446 SetLeftClippingPlane( left );
447 SetRightClippingPlane( right );
448 SetTopClippingPlane( top );
449 SetBottomClippingPlane( bottom );
450 SetNearClippingPlane( near );
451 SetFarClippingPlane( far );
452 SetProjectionMode( Dali::Camera::ORTHOGRAPHIC_PROJECTION );
455 bool CameraActor::BuildPickingRay( const Vector2& screenCoordinates,
456 const Viewport& viewport,
458 Vector4& rayDirection )
461 if( mProjectionMode == Dali::Camera::PERSPECTIVE_PROJECTION )
463 // Build a picking ray in the world reference system.
464 // ray starts from the camera world position
465 rayOrigin = GetNode().GetWorldMatrix(0).GetTranslation();
468 // Transform the touch point from the screen coordinate system to the world coordinates system.
469 Vector4 near( screenCoordinates.x - static_cast<float>(viewport.x),
470 static_cast<float>( viewport.height ) - (screenCoordinates.y - static_cast<float>( viewport.y ) ),
472 const Matrix& inverseViewProjection = mSceneObject->GetInverseViewProjectionMatrix( GetEventThreadServices().GetEventBufferIndex() );
473 success = Unproject( near, inverseViewProjection, static_cast<float>( viewport.width ), static_cast<float>( viewport.height ), near );
475 // Compute the ray's director vector.
476 rayDirection.x = near.x - rayOrigin.x;
477 rayDirection.y = near.y - rayOrigin.y;
478 rayDirection.z = near.z - rayOrigin.z;
479 rayDirection.Normalize();
480 rayDirection.w = 1.f;
484 float nearPlaneDistance = GetNearClippingPlane();
485 BuildOrthoPickingRay( GetViewMatrix(),
486 GetProjectionMatrix(),
487 viewport, screenCoordinates.x,
497 const Matrix& CameraActor::GetViewMatrix() const
501 return mSceneObject->GetViewMatrix( GetEventThreadServices().GetEventBufferIndex() );
505 return Matrix::IDENTITY;
509 const Matrix& CameraActor::GetProjectionMatrix() const
513 return mSceneObject->GetProjectionMatrix( GetEventThreadServices().GetEventBufferIndex() );
517 return Matrix::IDENTITY;
520 const SceneGraph::Camera* CameraActor::GetCamera() const
525 void CameraActor::RotateProjection( int rotationAngle )
527 // sceneObject is being used in a separate thread; queue a message to set
528 RotateProjectionMessage( GetEventThreadServices(), *mSceneObject, rotationAngle );
531 void CameraActor::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
533 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
535 Actor::SetDefaultProperty(index, propertyValue);
541 case Dali::CameraActor::Property::TYPE:
543 std::string s( propertyValue.Get<std::string>() );
544 if(s == "LOOK_AT_TARGET")
546 SetType( Dali::Camera::LOOK_AT_TARGET );
548 else if(s == "FREE_LOOK")
550 SetType( Dali::Camera::FREE_LOOK );
554 case Dali::CameraActor::Property::PROJECTION_MODE:
556 std::string s( propertyValue.Get<std::string>() );
557 if( s == "PERSPECTIVE_PROJECTION" )
559 SetProjectionMode( Dali::Camera::PERSPECTIVE_PROJECTION );
561 else if( s == "ORTHOGRAPHIC_PROJECTION" )
563 SetProjectionMode( Dali::Camera::ORTHOGRAPHIC_PROJECTION );
567 case Dali::CameraActor::Property::FIELD_OF_VIEW:
569 SetFieldOfView( propertyValue.Get<float>() ); // set to 0 in case property is not float
572 case Dali::CameraActor::Property::ASPECT_RATIO:
574 SetAspectRatio( propertyValue.Get<float>() ); // set to 0 in case property is not float
577 case Dali::CameraActor::Property::NEAR_PLANE_DISTANCE:
579 SetNearClippingPlane( propertyValue.Get<float>() ); // set to 0 in case property is not float
582 case Dali::CameraActor::Property::FAR_PLANE_DISTANCE:
584 SetFarClippingPlane( propertyValue.Get<float>() ); // set to 0 in case property is not float
587 case Dali::CameraActor::Property::LEFT_PLANE_DISTANCE:
589 SetLeftClippingPlane( propertyValue.Get<float>() ); // set to 0 in case property is not float
592 case Dali::CameraActor::Property::RIGHT_PLANE_DISTANCE:
594 SetRightClippingPlane( propertyValue.Get<float>() ); // set to 0 in case property is not float
597 case Dali::CameraActor::Property::TOP_PLANE_DISTANCE:
599 SetTopClippingPlane( propertyValue.Get<float>() ); // set to 0 in case property is not float
602 case Dali::CameraActor::Property::BOTTOM_PLANE_DISTANCE:
604 SetBottomClippingPlane( propertyValue.Get<float>() ); // set to 0 in case property is not float
607 case Dali::CameraActor::Property::TARGET_POSITION:
609 SetTarget( propertyValue.Get<Vector3>() ); // set to 0 in case property is not Vector3
612 case Dali::CameraActor::Property::PROJECTION_MATRIX:
614 DALI_LOG_WARNING( "projection-matrix is read-only\n" );
617 case Dali::CameraActor::Property::VIEW_MATRIX:
619 DALI_LOG_WARNING( "view-matrix is read-only\n" );
622 case Dali::CameraActor::Property::INVERT_Y_AXIS:
624 SetInvertYAxis( propertyValue.Get<bool>() ); // set to false in case property is not bool
629 DALI_LOG_WARNING( "Unknown property (%d)\n", index );
637 Property::Value CameraActor::GetDefaultProperty( Property::Index index ) const
640 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
642 ret = Actor::GetDefaultProperty(index);
648 case Dali::CameraActor::Property::TYPE:
650 if( Dali::Camera::LOOK_AT_TARGET == mType )
652 ret = "LOOK_AT_TARGET";
654 else if( Dali::Camera::FREE_LOOK == mType )
660 case Dali::CameraActor::Property::PROJECTION_MODE:
662 if( Dali::Camera::PERSPECTIVE_PROJECTION == mProjectionMode )
664 ret = "PERSPECTIVE_PROJECTION";
666 else if( Dali::Camera::ORTHOGRAPHIC_PROJECTION == mProjectionMode )
668 ret = "ORTHOGRAPHIC_PROJECTION";
672 case Dali::CameraActor::Property::FIELD_OF_VIEW:
677 case Dali::CameraActor::Property::ASPECT_RATIO:
682 case Dali::CameraActor::Property::NEAR_PLANE_DISTANCE:
684 ret = mNearClippingPlane;
687 case Dali::CameraActor::Property::FAR_PLANE_DISTANCE:
689 ret = mFarClippingPlane;
692 case Dali::CameraActor::Property::LEFT_PLANE_DISTANCE:
694 ret = mLeftClippingPlane;
697 case Dali::CameraActor::Property::RIGHT_PLANE_DISTANCE:
699 ret = mRightClippingPlane;
702 case Dali::CameraActor::Property::TOP_PLANE_DISTANCE:
704 ret = mTopClippingPlane;
707 case Dali::CameraActor::Property::BOTTOM_PLANE_DISTANCE:
709 ret = mBottomClippingPlane;
712 case Dali::CameraActor::Property::TARGET_POSITION:
717 case Dali::CameraActor::Property::PROJECTION_MATRIX:
719 ret = GetProjectionMatrix(); // Only on scene-graph
722 case Dali::CameraActor::Property::VIEW_MATRIX:
724 ret = GetViewMatrix(); // Only on scene-graph
727 case Dali::CameraActor::Property::INVERT_Y_AXIS:
738 Property::Value CameraActor::GetDefaultPropertyCurrentValue( Property::Index index ) const
741 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
743 ret = Actor::GetDefaultPropertyCurrentValue(index);
747 ret = GetDefaultProperty( index ); // Most are event-side properties, the scene-graph properties are only on the scene-graph
753 const PropertyInputImpl* CameraActor::GetSceneObjectInputProperty( Property::Index index ) const
755 const PropertyInputImpl* property( NULL );
759 case Dali::CameraActor::Property::PROJECTION_MATRIX:
761 property = mSceneObject->GetProjectionMatrix();
764 case Dali::CameraActor::Property::VIEW_MATRIX:
766 property = mSceneObject->GetViewMatrix();
769 // no default on purpose as we chain method up to actor
773 property = Actor::GetSceneObjectInputProperty( index );
779 } // namespace Internal