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>
25 #include <dali/public-api/common/stage.h>
26 #include <dali/public-api/object/type-registry.h>
27 #include <dali/internal/event/actor-attachments/camera-attachment-impl.h>
28 #include <dali/internal/event/common/property-helper.h>
29 #include <dali/internal/event/common/stage-impl.h>
30 #include <dali/internal/event/render-tasks/render-task-impl.h>
31 #include <dali/internal/event/render-tasks/render-task-list-impl.h>
32 #include <dali/internal/event/common/projection.h>
33 #include <dali/integration-api/debug.h>
47 * We want to discourage the use of property strings (minimize string comparisons),
48 * particularly for the default properties.
49 * Name Type writable animatable constraint-input enum for index-checking
51 DALI_PROPERTY_TABLE_BEGIN
52 DALI_PROPERTY( "type", STRING, true, false, true, Dali::CameraActor::Property::TYPE )
53 DALI_PROPERTY( "projection-mode", STRING, true, false, true, Dali::CameraActor::Property::PROJECTION_MODE )
54 DALI_PROPERTY( "field-of-view", FLOAT, true, false, true, Dali::CameraActor::Property::FIELD_OF_VIEW )
55 DALI_PROPERTY( "aspect-ratio", FLOAT, true, false, true, Dali::CameraActor::Property::ASPECT_RATIO )
56 DALI_PROPERTY( "near-plane-distance", FLOAT, true, false, true, Dali::CameraActor::Property::NEAR_PLANE_DISTANCE )
57 DALI_PROPERTY( "far-plane-distance", FLOAT, true, false, true, Dali::CameraActor::Property::FAR_PLANE_DISTANCE )
58 DALI_PROPERTY( "left-plane-distance", FLOAT, true, false, true, Dali::CameraActor::Property::LEFT_PLANE_DISTANCE )
59 DALI_PROPERTY( "right-plane-distance", FLOAT, true, false, true, Dali::CameraActor::Property::RIGHT_PLANE_DISTANCE )
60 DALI_PROPERTY( "top-plane-distance", FLOAT, true, false, true, Dali::CameraActor::Property::TOP_PLANE_DISTANCE )
61 DALI_PROPERTY( "bottom-plane-distance", FLOAT, true, false, true, Dali::CameraActor::Property::BOTTOM_PLANE_DISTANCE )
62 DALI_PROPERTY( "target-position", VECTOR3, true, false, true, Dali::CameraActor::Property::TARGET_POSITION )
63 DALI_PROPERTY( "projection-matrix", MATRIX, false, false, true, Dali::CameraActor::Property::PROJECTION_MATRIX )
64 DALI_PROPERTY( "view-matrix", MATRIX, false, false, true, Dali::CameraActor::Property::VIEW_MATRIX )
65 DALI_PROPERTY( "invert-y-axis", BOOLEAN, true, false, true, Dali::CameraActor::Property::INVERT_Y_AXIS )
66 DALI_PROPERTY_TABLE_END( DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX )
68 // calculate the far plane distance for a 16bit depth buffer with 4 bits per unit precision
69 void CalculateClippingAndZ( float width, float height, float& nearClippingPlane, float& farClippingPlane, float& cameraZ )
71 nearClippingPlane = std::max( width, height );
72 farClippingPlane = nearClippingPlane + static_cast<float>( 0xFFFF >> 4 );
73 cameraZ = 2.0f * nearClippingPlane;
78 return Dali::CameraActor::New();
81 TypeRegistration mType( typeid( Dali::CameraActor ), typeid( Dali::Actor ), Create );
84 * Builds the picking ray in the world reference system from an orthographic camera
85 * The ray origin is the screen coordinate in the near plane translated to a parallel
86 * plane at the camera origin. The ray direction is the direction the camera is facing
87 * (i.e. Z=-1 in view space).
89 void BuildOrthoPickingRay( const Matrix& viewMatrix,
90 const Matrix& projectionMatrix,
91 const Viewport& viewport,
96 float nearPlaneDistance )
98 // inv( modelMatrix ) inv( viewMatrix ) inv( projectionMatrix ) normalize
99 // <----------------- <----------------- <-------------- <-------------
100 // Local World Camera Normalized Screen
101 // reference reference reference clip coordinates
102 // system system system coordinates
103 // -----------------> -----------------> --------------> ------------->
104 // modelMatrix viewMatrix projectionMatrix viewport
106 // Transforms the touch point from the screen reference system to the world reference system.
107 Matrix invViewProjection( false ); // Don't initialize.
108 Matrix::Multiply( invViewProjection, viewMatrix, projectionMatrix );
109 if( !invViewProjection.Invert() )
111 DALI_ASSERT_DEBUG( false );
114 Vector4 near( screenX - viewport.x, viewport.height - (screenY - viewport.y), 0.f, 1.f );
115 if( !Unproject( near, invViewProjection, viewport.width, viewport.height, rayOrigin ) )
117 DALI_ASSERT_DEBUG( false );
120 Matrix invView = viewMatrix;
121 if( !invView.Invert() )
123 DALI_ASSERT_DEBUG( false );
126 Vector4 cameraOrigin = invView * Vector4( 0.f, 0.f, 0.f, 1.f );
127 Vector4 nearPlaneOrigin = invView * Vector4( 0.0f, 0.0f, -nearPlaneDistance, 1.0f);
129 // Vector pointing from the camera to the near plane
130 rayDir = cameraOrigin - nearPlaneOrigin;
138 CameraActorPtr CameraActor::New( const Size& size )
140 CameraActorPtr actor(new CameraActor());
142 // Second-phase construction
146 actor->SetName("DefaultCamera");
148 // Create the attachment
149 actor->mCameraAttachment = CameraAttachment::New( *actor->mNode );
151 actor->Attach(*actor->mCameraAttachment);
153 actor->SetPerspectiveProjection( size );
155 // By default Actors face in the positive Z direction in world space
156 // CameraActors should face in the negative Z direction, towards the other actors
157 actor->SetOrientation( Quaternion( Math::PI, Vector3::YAXIS ) );
162 void CameraActor::OnInitialize()
166 CameraActor::CameraActor()
167 : Actor( Actor::BASIC )
171 CameraActor::~CameraActor()
175 void CameraActor::SetType( Dali::Camera::Type type )
177 mCameraAttachment->SetType(type);
180 Dali::Camera::Type CameraActor::GetType() const
182 return mCameraAttachment->GetType();
185 void CameraActor::SetProjectionMode( Dali::Camera::ProjectionMode mode )
187 mCameraAttachment->SetProjectionMode(mode);
190 Dali::Camera::ProjectionMode CameraActor::GetProjectionMode() const
192 return mCameraAttachment->GetProjectionMode();
195 void CameraActor::SetFieldOfView( float fieldOfView )
197 mCameraAttachment->SetFieldOfView(fieldOfView);
200 float CameraActor::GetFieldOfView( ) const
202 return mCameraAttachment->GetFieldOfView();
205 void CameraActor::SetAspectRatio( float aspectRatio )
207 mCameraAttachment->SetAspectRatio(aspectRatio);
210 float CameraActor::GetAspectRatio( ) const
212 return mCameraAttachment->GetAspectRatio();
215 void CameraActor::SetNearClippingPlane( float nearClippingPlane )
217 mCameraAttachment->SetNearClippingPlane(nearClippingPlane);
220 float CameraActor::GetNearClippingPlane( ) const
222 return mCameraAttachment->GetNearClippingPlane();
225 void CameraActor::SetFarClippingPlane( float farClippingPlane )
227 mCameraAttachment->SetFarClippingPlane(farClippingPlane);
230 float CameraActor::GetFarClippingPlane( ) const
232 return mCameraAttachment->GetFarClippingPlane();
235 void CameraActor::SetTargetPosition(const Vector3& target)
237 mCameraAttachment->SetTargetPosition(target);
240 Vector3 CameraActor::GetTargetPosition() const
242 return mCameraAttachment->GetTargetPosition();
245 void CameraActor::SetInvertYAxis(bool invertYAxis)
247 mCameraAttachment->SetInvertYAxis(invertYAxis);
250 bool CameraActor::GetInvertYAxis() const
252 return mCameraAttachment->GetInvertYAxis();
255 void CameraActor::SetPerspectiveProjection( const Size& size, const Vector2& stereoBias /* = Vector2::ZERO */ )
257 float width = size.width;
258 float height = size.height;
260 if( Size::ZERO == size )
262 if( Stage::IsInstalled() )
264 const Size& stageSize = mStage->GetSize();
266 width = stageSize.width;
267 height = stageSize.height;
271 if( ( width < Math::MACHINE_EPSILON_1000 ) || ( height < Math::MACHINE_EPSILON_1000 ) )
273 // On the stage initialization this method is called but the size has not been set.
274 // There is no point to set any value if width or height is zero.
278 float nearClippingPlane;
279 float farClippingPlane;
281 CalculateClippingAndZ( width, height, nearClippingPlane, farClippingPlane, cameraZ );
283 // calculate the position of the camera to have the desired aspect ratio
284 const float fieldOfView = 2.0f * std::atan( height * 0.5f / cameraZ );
286 // unless it is too small, we want at least as much space to the back as we have torwards the front
287 const float minClippingFarPlane = 2.f * nearClippingPlane;
288 if ( farClippingPlane < minClippingFarPlane )
290 farClippingPlane = minClippingFarPlane;
293 const float aspectRatio = width / height;
295 SetProjectionMode(Dali::Camera::PERSPECTIVE_PROJECTION);
296 SetFieldOfView( fieldOfView );
297 SetNearClippingPlane( nearClippingPlane );
298 SetFarClippingPlane( farClippingPlane );
299 SetAspectRatio( aspectRatio );
300 mCameraAttachment->SetStereoBias( stereoBias );
305 void CameraActor::SetOrthographicProjection( const Vector2& size )
307 // Choose near, far and Z parameters to match the SetPerspectiveProjection above.
308 float nearClippingPlane;
309 float farClippingPlane;
311 CalculateClippingAndZ( size.width, size.height, nearClippingPlane, farClippingPlane, cameraZ );
312 SetOrthographicProjection( -size.x*0.5f, size.x*0.5f, size.y*0.5f, -size.y*0.5f,
313 nearClippingPlane, farClippingPlane );
317 void CameraActor::SetOrthographicProjection( float left, float right, float top, float bottom, float near, float far )
319 mCameraAttachment->SetLeftClippingPlane(left);
320 mCameraAttachment->SetRightClippingPlane(right);
321 mCameraAttachment->SetTopClippingPlane(top);
322 mCameraAttachment->SetBottomClippingPlane(bottom);
323 SetNearClippingPlane( near );
324 SetFarClippingPlane( far );
325 SetProjectionMode(Dali::Camera::ORTHOGRAPHIC_PROJECTION);
328 bool CameraActor::BuildPickingRay( const Vector2& screenCoordinates,
329 const Viewport& viewport,
331 Vector4& rayDirection )
334 if( GetProjectionMode() == Dali::Camera::PERSPECTIVE_PROJECTION )
336 // Build a picking ray in the world reference system.
337 // ray starts from the camera world position
338 rayOrigin = mNode->GetWorldPosition( mStage->GetEventBufferIndex() );
341 // Transform the touch point from the screen coordinate system to the world coordinates system.
342 Vector4 near( screenCoordinates.x - viewport.x, viewport.height - (screenCoordinates.y - viewport.y), 0.f, 1.f );
343 if( !Unproject( near, mCameraAttachment->GetInverseViewProjectionMatrix(), viewport.width, viewport.height, near ) )
345 // unproject failed so no picking ray possible
349 // Compute the ray's director vector.
350 rayDirection.x = near.x - rayOrigin.x;
351 rayDirection.y = near.y - rayOrigin.y;
352 rayDirection.z = near.z - rayOrigin.z;
353 rayDirection.Normalize();
354 rayDirection.w = 1.f;
358 float nearPlaneDistance = GetNearClippingPlane();
359 BuildOrthoPickingRay( GetViewMatrix(),
360 GetProjectionMatrix(),
361 viewport, screenCoordinates.x,
371 const Matrix& CameraActor::GetViewMatrix() const
375 return mCameraAttachment->GetViewMatrix();
379 return Matrix::IDENTITY;
383 const Matrix& CameraActor::GetProjectionMatrix() const
387 return mCameraAttachment->GetProjectionMatrix();
391 return Matrix::IDENTITY;
395 unsigned int CameraActor::GetDefaultPropertyCount() const
397 return Actor::GetDefaultPropertyCount() + DEFAULT_PROPERTY_COUNT;
400 void CameraActor::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
402 Actor::GetDefaultPropertyIndices( indices ); // Actor class properties
404 indices.reserve( indices.size() + DEFAULT_PROPERTY_COUNT );
406 int index = DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
407 for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i, ++index )
409 indices.push_back( index );
413 bool CameraActor::IsDefaultPropertyWritable( Property::Index index ) const
415 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
417 return Actor::IsDefaultPropertyWritable( index );
420 return DEFAULT_PROPERTY_DETAILS[index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX].writable;
423 bool CameraActor::IsDefaultPropertyAnimatable( Property::Index index ) const
425 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
427 return Actor::IsDefaultPropertyAnimatable( index );
430 return DEFAULT_PROPERTY_DETAILS[index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX].animatable;
433 bool CameraActor::IsDefaultPropertyAConstraintInput( Property::Index index ) const
435 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
437 return Actor::IsDefaultPropertyAConstraintInput( index );
440 return DEFAULT_PROPERTY_DETAILS[index - DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX].constraintInput;
443 Property::Type CameraActor::GetDefaultPropertyType( Property::Index index ) const
445 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
447 return Actor::GetDefaultPropertyType( index );
451 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
453 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
455 return DEFAULT_PROPERTY_DETAILS[index].type;
459 // index out-of-bounds
460 return Property::NONE;
465 const char* CameraActor::GetDefaultPropertyName( Property::Index index ) const
467 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
469 return Actor::GetDefaultPropertyName(index);
473 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
475 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
477 return DEFAULT_PROPERTY_DETAILS[index].name;
483 Property::Index CameraActor::GetDefaultPropertyIndex(const std::string& name) const
485 Property::Index index = Property::INVALID_INDEX;
487 // Look for name in current class' default properties
488 for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
490 if( 0 == strcmp( name.c_str(), DEFAULT_PROPERTY_DETAILS[i].name ) ) // dont want to convert rhs to string
492 index = i + DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
497 // If not found, check in base class
498 if( Property::INVALID_INDEX == index )
500 index = Actor::GetDefaultPropertyIndex( name );
506 void CameraActor::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
508 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
510 Actor::SetDefaultProperty(index, propertyValue);
514 DALI_ASSERT_DEBUG(mCameraAttachment && "where is the camera?");
517 case Dali::CameraActor::Property::TYPE:
519 std::string s( propertyValue.Get<std::string>() );
520 if(s == "LOOK_AT_TARGET")
522 mCameraAttachment->SetType(Dali::Camera::LOOK_AT_TARGET);
524 else if(s == "FREE_LOOK")
526 mCameraAttachment->SetType(Dali::Camera::FREE_LOOK);
530 DALI_LOG_WARNING("Unknown camera type\n");
534 case Dali::CameraActor::Property::PROJECTION_MODE:
536 std::string s(propertyValue.Get<std::string>());
537 if(s == "PERSPECTIVE_PROJECTION")
539 mCameraAttachment->SetProjectionMode(Dali::Camera::PERSPECTIVE_PROJECTION);
541 else if(s == "ORTHOGRAPHIC_PROJECTION")
543 mCameraAttachment->SetProjectionMode(Dali::Camera::ORTHOGRAPHIC_PROJECTION);
547 DALI_LOG_WARNING("Unknown projection mode\n");
551 case Dali::CameraActor::Property::FIELD_OF_VIEW:
553 mCameraAttachment->SetFieldOfView(propertyValue.Get<float>());
556 case Dali::CameraActor::Property::ASPECT_RATIO:
558 mCameraAttachment->SetAspectRatio(propertyValue.Get<float>());
561 case Dali::CameraActor::Property::LEFT_PLANE_DISTANCE:
563 mCameraAttachment->SetLeftClippingPlane(propertyValue.Get<float>());
566 case Dali::CameraActor::Property::RIGHT_PLANE_DISTANCE:
568 mCameraAttachment->SetRightClippingPlane(propertyValue.Get<float>());
571 case Dali::CameraActor::Property::TOP_PLANE_DISTANCE:
573 mCameraAttachment->SetTopClippingPlane(propertyValue.Get<float>());
576 case Dali::CameraActor::Property::BOTTOM_PLANE_DISTANCE:
578 mCameraAttachment->SetBottomClippingPlane(propertyValue.Get<float>());
581 case Dali::CameraActor::Property::NEAR_PLANE_DISTANCE:
583 mCameraAttachment->SetNearClippingPlane(propertyValue.Get<float>());
586 case Dali::CameraActor::Property::FAR_PLANE_DISTANCE:
588 mCameraAttachment->SetFarClippingPlane(propertyValue.Get<float>());
591 case Dali::CameraActor::Property::TARGET_POSITION:
593 mCameraAttachment->SetTargetPosition(propertyValue.Get<Vector3>());
596 case Dali::CameraActor::Property::PROJECTION_MATRIX:
598 DALI_LOG_WARNING("projection-matrix property is not animatable \n");
601 case Dali::CameraActor::Property::VIEW_MATRIX:
603 DALI_LOG_WARNING("view-matrix property is not animatable \n");
606 case Dali::CameraActor::Property::INVERT_Y_AXIS:
608 mCameraAttachment->SetInvertYAxis(propertyValue.Get<bool>());
613 DALI_LOG_WARNING("Unknown property (%d)\n", index);
621 Property::Value CameraActor::GetDefaultProperty( Property::Index index ) const
624 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
626 ret = Actor::GetDefaultProperty(index);
630 DALI_ASSERT_DEBUG(mCameraAttachment && "where is the camera?");
633 case Dali::CameraActor::Property::TYPE:
635 if(mCameraAttachment->GetType() == Dali::Camera::LOOK_AT_TARGET)
637 ret = "LOOK_AT_TARGET";
639 else if(mCameraAttachment->GetType() == Dali::Camera::FREE_LOOK)
646 DALI_ASSERT_DEBUG("Unknown camera type\n");
650 case Dali::CameraActor::Property::PROJECTION_MODE:
652 if(mCameraAttachment->GetProjectionMode() == Dali::Camera::PERSPECTIVE_PROJECTION)
654 ret = "PERSPECTIVE_PROJECTION";
656 else if(mCameraAttachment->GetProjectionMode() == Dali::Camera::ORTHOGRAPHIC_PROJECTION)
658 ret = "ORTHOGRAPHIC_PROJECTION";
663 DALI_ASSERT_DEBUG("Unknown projection mode\n");
667 case Dali::CameraActor::Property::FIELD_OF_VIEW:
669 ret = mCameraAttachment->GetFieldOfView();
672 case Dali::CameraActor::Property::ASPECT_RATIO:
674 ret = mCameraAttachment->GetAspectRatio();
677 case Dali::CameraActor::Property::LEFT_PLANE_DISTANCE:
679 ret = mCameraAttachment->GetLeftClippingPlane();
682 case Dali::CameraActor::Property::RIGHT_PLANE_DISTANCE:
684 ret = mCameraAttachment->GetRightClippingPlane();
687 case Dali::CameraActor::Property::TOP_PLANE_DISTANCE:
689 ret = mCameraAttachment->GetTopClippingPlane();
692 case Dali::CameraActor::Property::BOTTOM_PLANE_DISTANCE:
694 ret = mCameraAttachment->GetBottomClippingPlane();
697 case Dali::CameraActor::Property::NEAR_PLANE_DISTANCE:
699 ret = mCameraAttachment->GetNearClippingPlane();
702 case Dali::CameraActor::Property::FAR_PLANE_DISTANCE:
704 ret = mCameraAttachment->GetFarClippingPlane();
707 case Dali::CameraActor::Property::TARGET_POSITION:
709 ret = mCameraAttachment->GetTargetPosition();
712 case Dali::CameraActor::Property::PROJECTION_MATRIX:
714 ret = mCameraAttachment->GetProjectionMatrix();
717 case Dali::CameraActor::Property::VIEW_MATRIX:
719 ret = mCameraAttachment->GetViewMatrix();
722 case Dali::CameraActor::Property::INVERT_Y_AXIS:
724 ret = mCameraAttachment->GetInvertYAxis();
729 DALI_LOG_WARNING("Unknown property (%d)\n", index);
738 const SceneGraph::PropertyBase* CameraActor::GetSceneObjectAnimatableProperty( Property::Index index ) const
740 DALI_ASSERT_ALWAYS( IsPropertyAnimatable(index) && "Property is not animatable" );
742 const SceneGraph::PropertyBase* property( NULL );
744 // This method should only return a property of an object connected to the scene-graph
750 // let actor handle animatable properties, we have no animatable properties
751 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
753 property = Actor::GetSceneObjectAnimatableProperty(index);
759 const PropertyInputImpl* CameraActor::GetSceneObjectInputProperty( Property::Index index ) const
761 const PropertyInputImpl* property( NULL );
763 // This method should only return a property of an object connected to the scene-graph
769 // if its an actor default property or a custom property (actor already handles custom properties)
770 if( ( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT ) || ( index >= DEFAULT_PROPERTY_MAX_COUNT ) )
772 property = Actor::GetSceneObjectInputProperty(index);
778 case Dali::CameraActor::Property::PROJECTION_MATRIX:
780 property = mCameraAttachment->GetProjectionMatrixProperty();
783 case Dali::CameraActor::Property::VIEW_MATRIX:
785 property = mCameraAttachment->GetViewMatrixProperty();
789 DALI_LOG_WARNING("Not an input property (%d)\n", index);
798 } // namespace Internal