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/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/internal/event/actor-attachments/camera-attachment-impl.h>
29 #include <dali/internal/event/common/property-helper.h>
30 #include <dali/internal/event/common/stage-impl.h>
31 #include <dali/internal/event/render-tasks/render-task-impl.h>
32 #include <dali/internal/event/render-tasks/render-task-list-impl.h>
33 #include <dali/internal/event/common/projection.h>
34 #include <dali/integration-api/debug.h>
48 * We want to discourage the use of property strings (minimize string comparisons),
49 * particularly for the default properties.
50 * Name Type writable animatable constraint-input enum for index-checking
52 DALI_PROPERTY_TABLE_BEGIN
53 DALI_PROPERTY( "type", STRING, true, false, true, Dali::CameraActor::Property::TYPE )
54 DALI_PROPERTY( "projection-mode", STRING, true, false, true, Dali::CameraActor::Property::PROJECTION_MODE )
55 DALI_PROPERTY( "field-of-view", FLOAT, true, false, true, Dali::CameraActor::Property::FIELD_OF_VIEW )
56 DALI_PROPERTY( "aspect-ratio", FLOAT, true, false, true, Dali::CameraActor::Property::ASPECT_RATIO )
57 DALI_PROPERTY( "near-plane-distance", FLOAT, true, false, true, Dali::CameraActor::Property::NEAR_PLANE_DISTANCE )
58 DALI_PROPERTY( "far-plane-distance", FLOAT, true, false, true, Dali::CameraActor::Property::FAR_PLANE_DISTANCE )
59 DALI_PROPERTY( "left-plane-distance", FLOAT, true, false, true, Dali::CameraActor::Property::LEFT_PLANE_DISTANCE )
60 DALI_PROPERTY( "right-plane-distance", FLOAT, true, false, true, Dali::CameraActor::Property::RIGHT_PLANE_DISTANCE )
61 DALI_PROPERTY( "top-plane-distance", FLOAT, true, false, true, Dali::CameraActor::Property::TOP_PLANE_DISTANCE )
62 DALI_PROPERTY( "bottom-plane-distance", FLOAT, true, false, true, Dali::CameraActor::Property::BOTTOM_PLANE_DISTANCE )
63 DALI_PROPERTY( "target-position", VECTOR3, true, false, true, Dali::CameraActor::Property::TARGET_POSITION )
64 DALI_PROPERTY( "projection-matrix", MATRIX, false, false, true, Dali::CameraActor::Property::PROJECTION_MATRIX )
65 DALI_PROPERTY( "view-matrix", MATRIX, false, false, true, Dali::CameraActor::Property::VIEW_MATRIX )
66 DALI_PROPERTY( "invert-y-axis", BOOLEAN, true, false, true, Dali::CameraActor::Property::INVERT_Y_AXIS )
67 DALI_PROPERTY_TABLE_END( DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX )
69 // calculate the far plane distance for a 16bit depth buffer with 4 bits per unit precision
70 void CalculateClippingAndZ( float width, float height, float& nearClippingPlane, float& farClippingPlane, float& cameraZ )
72 nearClippingPlane = std::max( width, height );
73 farClippingPlane = nearClippingPlane + static_cast<float>( 0xFFFF >> 4 );
74 cameraZ = 2.0f * nearClippingPlane;
79 return Dali::CameraActor::New();
82 TypeRegistration mType( typeid( Dali::CameraActor ), typeid( Dali::Actor ), Create );
85 * Builds the picking ray in the world reference system from an orthographic camera
86 * The ray origin is the screen coordinate in the near plane translated to a parallel
87 * plane at the camera origin. The ray direction is the direction the camera is facing
88 * (i.e. Z=-1 in view space).
90 void BuildOrthoPickingRay( const Matrix& viewMatrix,
91 const Matrix& projectionMatrix,
92 const Viewport& viewport,
97 float nearPlaneDistance )
99 // inv( modelMatrix ) inv( viewMatrix ) inv( projectionMatrix ) normalize
100 // <----------------- <----------------- <-------------- <-------------
101 // Local World Camera Normalized Screen
102 // reference reference reference clip coordinates
103 // system system system coordinates
104 // -----------------> -----------------> --------------> ------------->
105 // modelMatrix viewMatrix projectionMatrix viewport
107 // Transforms the touch point from the screen reference system to the world reference system.
108 Matrix invViewProjection( false ); // Don't initialize.
109 Matrix::Multiply( invViewProjection, viewMatrix, projectionMatrix );
110 if( !invViewProjection.Invert() )
112 DALI_ASSERT_DEBUG( false );
115 Vector4 near( screenX - viewport.x, viewport.height - (screenY - viewport.y), 0.f, 1.f );
116 if( !Unproject( near, invViewProjection, viewport.width, viewport.height, rayOrigin ) )
118 DALI_ASSERT_DEBUG( false );
121 Matrix invView = viewMatrix;
122 if( !invView.Invert() )
124 DALI_ASSERT_DEBUG( false );
127 Vector4 cameraOrigin = invView * Vector4( 0.f, 0.f, 0.f, 1.f );
128 Vector4 nearPlaneOrigin = invView * Vector4( 0.0f, 0.0f, -nearPlaneDistance, 1.0f);
130 // Vector pointing from the camera to the near plane
131 rayDir = cameraOrigin - nearPlaneOrigin;
139 CameraActorPtr CameraActor::New( const Size& size )
141 CameraActorPtr actor(new CameraActor());
143 // Second-phase construction
147 actor->SetName("DefaultCamera");
149 // Create the attachment
150 actor->mCameraAttachment = CameraAttachment::New( actor->GetEventThreadServices(), *actor->mNode );
152 actor->Attach(*actor->mCameraAttachment);
154 actor->SetPerspectiveProjection( size );
156 // By default Actors face in the positive Z direction in world space
157 // CameraActors should face in the negative Z direction, towards the other actors
158 actor->SetOrientation( Quaternion( Math::PI, Vector3::YAXIS ) );
163 void CameraActor::OnInitialize()
167 CameraActor::CameraActor()
168 : Actor( Actor::BASIC )
172 CameraActor::~CameraActor()
176 void CameraActor::SetType( Dali::Camera::Type type )
178 mCameraAttachment->SetType(type);
181 Dali::Camera::Type CameraActor::GetType() const
183 return mCameraAttachment->GetType();
186 void CameraActor::SetProjectionMode( Dali::Camera::ProjectionMode mode )
188 mCameraAttachment->SetProjectionMode(mode);
191 Dali::Camera::ProjectionMode CameraActor::GetProjectionMode() const
193 return mCameraAttachment->GetProjectionMode();
196 void CameraActor::SetFieldOfView( float fieldOfView )
198 mCameraAttachment->SetFieldOfView(fieldOfView);
201 float CameraActor::GetFieldOfView( ) const
203 return mCameraAttachment->GetFieldOfView();
206 void CameraActor::SetAspectRatio( float aspectRatio )
208 mCameraAttachment->SetAspectRatio(aspectRatio);
211 float CameraActor::GetAspectRatio( ) const
213 return mCameraAttachment->GetAspectRatio();
216 void CameraActor::SetNearClippingPlane( float nearClippingPlane )
218 mCameraAttachment->SetNearClippingPlane(nearClippingPlane);
221 float CameraActor::GetNearClippingPlane( ) const
223 return mCameraAttachment->GetNearClippingPlane();
226 void CameraActor::SetFarClippingPlane( float farClippingPlane )
228 mCameraAttachment->SetFarClippingPlane(farClippingPlane);
231 float CameraActor::GetFarClippingPlane( ) const
233 return mCameraAttachment->GetFarClippingPlane();
236 void CameraActor::SetTargetPosition(const Vector3& target)
238 mCameraAttachment->SetTargetPosition(target);
241 Vector3 CameraActor::GetTargetPosition() const
243 return mCameraAttachment->GetTargetPosition();
246 void CameraActor::SetInvertYAxis(bool invertYAxis)
248 mCameraAttachment->SetInvertYAxis(invertYAxis);
251 bool CameraActor::GetInvertYAxis() const
253 return mCameraAttachment->GetInvertYAxis();
256 void CameraActor::SetPerspectiveProjection( const Size& size, const Vector2& stereoBias /* = Vector2::ZERO */ )
258 float width = size.width;
259 float height = size.height;
261 if( Size::ZERO == size )
263 if( Stage::IsInstalled() )
265 const Size& stageSize = Stage::GetCurrent()->GetSize();
267 width = stageSize.width;
268 height = stageSize.height;
272 if( ( width < Math::MACHINE_EPSILON_1000 ) || ( height < Math::MACHINE_EPSILON_1000 ) )
274 // On the stage initialization this method is called but the size has not been set.
275 // There is no point to set any value if width or height is zero.
279 float nearClippingPlane;
280 float farClippingPlane;
282 CalculateClippingAndZ( width, height, nearClippingPlane, farClippingPlane, cameraZ );
284 // calculate the position of the camera to have the desired aspect ratio
285 const float fieldOfView = 2.0f * std::atan( height * 0.5f / cameraZ );
287 // unless it is too small, we want at least as much space to the back as we have torwards the front
288 const float minClippingFarPlane = 2.f * nearClippingPlane;
289 if ( farClippingPlane < minClippingFarPlane )
291 farClippingPlane = minClippingFarPlane;
294 const float aspectRatio = width / height;
296 SetProjectionMode(Dali::Camera::PERSPECTIVE_PROJECTION);
297 SetFieldOfView( fieldOfView );
298 SetNearClippingPlane( nearClippingPlane );
299 SetFarClippingPlane( farClippingPlane );
300 SetAspectRatio( aspectRatio );
301 mCameraAttachment->SetStereoBias( stereoBias );
306 void CameraActor::SetOrthographicProjection( const Vector2& size )
308 // Choose near, far and Z parameters to match the SetPerspectiveProjection above.
309 float nearClippingPlane;
310 float farClippingPlane;
312 CalculateClippingAndZ( size.width, size.height, nearClippingPlane, farClippingPlane, cameraZ );
313 SetOrthographicProjection( -size.x*0.5f, size.x*0.5f, size.y*0.5f, -size.y*0.5f,
314 nearClippingPlane, farClippingPlane );
318 void CameraActor::SetOrthographicProjection( float left, float right, float top, float bottom, float near, float far )
320 mCameraAttachment->SetLeftClippingPlane(left);
321 mCameraAttachment->SetRightClippingPlane(right);
322 mCameraAttachment->SetTopClippingPlane(top);
323 mCameraAttachment->SetBottomClippingPlane(bottom);
324 SetNearClippingPlane( near );
325 SetFarClippingPlane( far );
326 SetProjectionMode(Dali::Camera::ORTHOGRAPHIC_PROJECTION);
329 bool CameraActor::BuildPickingRay( const Vector2& screenCoordinates,
330 const Viewport& viewport,
332 Vector4& rayDirection )
335 if( GetProjectionMode() == Dali::Camera::PERSPECTIVE_PROJECTION )
337 // Build a picking ray in the world reference system.
338 // ray starts from the camera world position
339 rayOrigin = mNode->GetWorldPosition( GetEventThreadServices().GetEventBufferIndex() );
342 // Transform the touch point from the screen coordinate system to the world coordinates system.
343 Vector4 near( screenCoordinates.x - viewport.x, viewport.height - (screenCoordinates.y - viewport.y), 0.f, 1.f );
344 if( !Unproject( near, mCameraAttachment->GetInverseViewProjectionMatrix(), viewport.width, viewport.height, near ) )
346 // unproject failed so no picking ray possible
350 // Compute the ray's director vector.
351 rayDirection.x = near.x - rayOrigin.x;
352 rayDirection.y = near.y - rayOrigin.y;
353 rayDirection.z = near.z - rayOrigin.z;
354 rayDirection.Normalize();
355 rayDirection.w = 1.f;
359 float nearPlaneDistance = GetNearClippingPlane();
360 BuildOrthoPickingRay( GetViewMatrix(),
361 GetProjectionMatrix(),
362 viewport, screenCoordinates.x,
372 const Matrix& CameraActor::GetViewMatrix() const
376 return mCameraAttachment->GetViewMatrix();
380 return Matrix::IDENTITY;
384 const Matrix& CameraActor::GetProjectionMatrix() const
388 return mCameraAttachment->GetProjectionMatrix();
392 return Matrix::IDENTITY;
396 unsigned int CameraActor::GetDefaultPropertyCount() const
398 return Actor::GetDefaultPropertyCount() + DEFAULT_PROPERTY_COUNT;
401 void CameraActor::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
403 Actor::GetDefaultPropertyIndices( indices ); // Actor class properties
405 indices.reserve( indices.size() + DEFAULT_PROPERTY_COUNT );
407 int index = DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
408 for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i, ++index )
410 indices.push_back( index );
414 bool CameraActor::IsDefaultPropertyWritable( Property::Index index ) const
416 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
418 return Actor::IsDefaultPropertyWritable( index );
421 return DEFAULT_PROPERTY_DETAILS[index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX].writable;
424 bool CameraActor::IsDefaultPropertyAnimatable( Property::Index index ) const
426 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
428 return Actor::IsDefaultPropertyAnimatable( index );
431 return DEFAULT_PROPERTY_DETAILS[index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX].animatable;
434 bool CameraActor::IsDefaultPropertyAConstraintInput( Property::Index index ) const
436 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
438 return Actor::IsDefaultPropertyAConstraintInput( index );
441 return DEFAULT_PROPERTY_DETAILS[index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX].constraintInput;
444 Property::Type CameraActor::GetDefaultPropertyType( Property::Index index ) const
446 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
448 return Actor::GetDefaultPropertyType( index );
452 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
454 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
456 return DEFAULT_PROPERTY_DETAILS[index].type;
460 // index out-of-bounds
461 return Property::NONE;
466 const char* CameraActor::GetDefaultPropertyName( Property::Index index ) const
468 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
470 return Actor::GetDefaultPropertyName(index);
474 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
476 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
478 return DEFAULT_PROPERTY_DETAILS[index].name;
484 Property::Index CameraActor::GetDefaultPropertyIndex(const std::string& name) const
486 Property::Index index = Property::INVALID_INDEX;
488 // Look for name in current class' default properties
489 for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
491 if( 0 == strcmp( name.c_str(), DEFAULT_PROPERTY_DETAILS[i].name ) ) // dont want to convert rhs to string
493 index = i + DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
498 // If not found, check in base class
499 if( Property::INVALID_INDEX == index )
501 index = Actor::GetDefaultPropertyIndex( name );
507 void CameraActor::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
509 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
511 Actor::SetDefaultProperty(index, propertyValue);
515 DALI_ASSERT_DEBUG(mCameraAttachment && "where is the camera?");
518 case Dali::CameraActor::Property::TYPE:
520 std::string s( propertyValue.Get<std::string>() );
521 if(s == "LOOK_AT_TARGET")
523 mCameraAttachment->SetType(Dali::Camera::LOOK_AT_TARGET);
525 else if(s == "FREE_LOOK")
527 mCameraAttachment->SetType(Dali::Camera::FREE_LOOK);
531 DALI_LOG_WARNING("Unknown camera type\n");
535 case Dali::CameraActor::Property::PROJECTION_MODE:
537 std::string s(propertyValue.Get<std::string>());
538 if(s == "PERSPECTIVE_PROJECTION")
540 mCameraAttachment->SetProjectionMode(Dali::Camera::PERSPECTIVE_PROJECTION);
542 else if(s == "ORTHOGRAPHIC_PROJECTION")
544 mCameraAttachment->SetProjectionMode(Dali::Camera::ORTHOGRAPHIC_PROJECTION);
548 DALI_LOG_WARNING("Unknown projection mode\n");
552 case Dali::CameraActor::Property::FIELD_OF_VIEW:
554 mCameraAttachment->SetFieldOfView(propertyValue.Get<float>());
557 case Dali::CameraActor::Property::ASPECT_RATIO:
559 mCameraAttachment->SetAspectRatio(propertyValue.Get<float>());
562 case Dali::CameraActor::Property::LEFT_PLANE_DISTANCE:
564 mCameraAttachment->SetLeftClippingPlane(propertyValue.Get<float>());
567 case Dali::CameraActor::Property::RIGHT_PLANE_DISTANCE:
569 mCameraAttachment->SetRightClippingPlane(propertyValue.Get<float>());
572 case Dali::CameraActor::Property::TOP_PLANE_DISTANCE:
574 mCameraAttachment->SetTopClippingPlane(propertyValue.Get<float>());
577 case Dali::CameraActor::Property::BOTTOM_PLANE_DISTANCE:
579 mCameraAttachment->SetBottomClippingPlane(propertyValue.Get<float>());
582 case Dali::CameraActor::Property::NEAR_PLANE_DISTANCE:
584 mCameraAttachment->SetNearClippingPlane(propertyValue.Get<float>());
587 case Dali::CameraActor::Property::FAR_PLANE_DISTANCE:
589 mCameraAttachment->SetFarClippingPlane(propertyValue.Get<float>());
592 case Dali::CameraActor::Property::TARGET_POSITION:
594 mCameraAttachment->SetTargetPosition(propertyValue.Get<Vector3>());
597 case Dali::CameraActor::Property::PROJECTION_MATRIX:
599 DALI_LOG_WARNING("projection-matrix property is not animatable \n");
602 case Dali::CameraActor::Property::VIEW_MATRIX:
604 DALI_LOG_WARNING("view-matrix property is not animatable \n");
607 case Dali::CameraActor::Property::INVERT_Y_AXIS:
609 mCameraAttachment->SetInvertYAxis(propertyValue.Get<bool>());
614 DALI_LOG_WARNING("Unknown property (%d)\n", index);
622 Property::Value CameraActor::GetDefaultProperty( Property::Index index ) const
625 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
627 ret = Actor::GetDefaultProperty(index);
631 DALI_ASSERT_DEBUG(mCameraAttachment && "where is the camera?");
634 case Dali::CameraActor::Property::TYPE:
636 if(mCameraAttachment->GetType() == Dali::Camera::LOOK_AT_TARGET)
638 ret = "LOOK_AT_TARGET";
640 else if(mCameraAttachment->GetType() == Dali::Camera::FREE_LOOK)
647 DALI_ASSERT_DEBUG("Unknown camera type\n");
651 case Dali::CameraActor::Property::PROJECTION_MODE:
653 if(mCameraAttachment->GetProjectionMode() == Dali::Camera::PERSPECTIVE_PROJECTION)
655 ret = "PERSPECTIVE_PROJECTION";
657 else if(mCameraAttachment->GetProjectionMode() == Dali::Camera::ORTHOGRAPHIC_PROJECTION)
659 ret = "ORTHOGRAPHIC_PROJECTION";
664 DALI_ASSERT_DEBUG("Unknown projection mode\n");
668 case Dali::CameraActor::Property::FIELD_OF_VIEW:
670 ret = mCameraAttachment->GetFieldOfView();
673 case Dali::CameraActor::Property::ASPECT_RATIO:
675 ret = mCameraAttachment->GetAspectRatio();
678 case Dali::CameraActor::Property::LEFT_PLANE_DISTANCE:
680 ret = mCameraAttachment->GetLeftClippingPlane();
683 case Dali::CameraActor::Property::RIGHT_PLANE_DISTANCE:
685 ret = mCameraAttachment->GetRightClippingPlane();
688 case Dali::CameraActor::Property::TOP_PLANE_DISTANCE:
690 ret = mCameraAttachment->GetTopClippingPlane();
693 case Dali::CameraActor::Property::BOTTOM_PLANE_DISTANCE:
695 ret = mCameraAttachment->GetBottomClippingPlane();
698 case Dali::CameraActor::Property::NEAR_PLANE_DISTANCE:
700 ret = mCameraAttachment->GetNearClippingPlane();
703 case Dali::CameraActor::Property::FAR_PLANE_DISTANCE:
705 ret = mCameraAttachment->GetFarClippingPlane();
708 case Dali::CameraActor::Property::TARGET_POSITION:
710 ret = mCameraAttachment->GetTargetPosition();
713 case Dali::CameraActor::Property::PROJECTION_MATRIX:
715 ret = mCameraAttachment->GetProjectionMatrix();
718 case Dali::CameraActor::Property::VIEW_MATRIX:
720 ret = mCameraAttachment->GetViewMatrix();
723 case Dali::CameraActor::Property::INVERT_Y_AXIS:
725 ret = mCameraAttachment->GetInvertYAxis();
730 DALI_LOG_WARNING("Unknown property (%d)\n", index);
739 const SceneGraph::PropertyBase* CameraActor::GetSceneObjectAnimatableProperty( Property::Index index ) const
741 DALI_ASSERT_ALWAYS( IsPropertyAnimatable(index) && "Property is not animatable" );
743 const SceneGraph::PropertyBase* property( NULL );
745 // This method should only return a property of an object connected to the scene-graph
751 // let actor handle animatable properties, we have no animatable properties
752 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
754 property = Actor::GetSceneObjectAnimatableProperty(index);
760 const PropertyInputImpl* CameraActor::GetSceneObjectInputProperty( Property::Index index ) const
762 const PropertyInputImpl* property( NULL );
764 // This method should only return a property of an object connected to the scene-graph
770 // if its an actor default property or a custom property (actor already handles custom properties)
771 if( ( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT ) || ( index >= DEFAULT_PROPERTY_MAX_COUNT ) )
773 property = Actor::GetSceneObjectInputProperty(index);
779 case Dali::CameraActor::Property::PROJECTION_MATRIX:
781 property = mCameraAttachment->GetProjectionMatrixProperty();
784 case Dali::CameraActor::Property::VIEW_MATRIX:
786 property = mCameraAttachment->GetViewMatrixProperty();
790 DALI_LOG_WARNING("Not an input property (%d)\n", index);
799 } // namespace Internal