2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
18 #include <dali/internal/event/actors/camera-actor-impl.h>
21 #include <dali/public-api/common/stage.h>
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/internal/event/actor-attachments/camera-attachment-impl.h>
24 #include <dali/internal/event/common/property-index-ranges.h>
25 #include <dali/internal/event/common/stage-impl.h>
26 #include <dali/internal/event/render-tasks/render-task-impl.h>
27 #include <dali/internal/event/render-tasks/render-task-list-impl.h>
28 #include <dali/internal/event/common/projection.h>
29 #include <dali/integration-api/debug.h>
34 const Property::Index CameraActor::TYPE = DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
35 const Property::Index CameraActor::PROJECTION_MODE = DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 1;
36 const Property::Index CameraActor::FIELD_OF_VIEW = DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 2;
37 const Property::Index CameraActor::ASPECT_RATIO = DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 3;
38 const Property::Index CameraActor::NEAR_PLANE_DISTANCE = DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 4;
39 const Property::Index CameraActor::FAR_PLANE_DISTANCE = DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 5;
40 const Property::Index CameraActor::LEFT_PLANE_DISTANCE = DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 6;
41 const Property::Index CameraActor::RIGHT_PLANE_DISTANCE = DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 7;
42 const Property::Index CameraActor::TOP_PLANE_DISTANCE = DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 8;
43 const Property::Index CameraActor::BOTTOM_PLANE_DISTANCE = DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 9;
44 const Property::Index CameraActor::TARGET_POSITION = DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 10;
45 const Property::Index CameraActor::PROJECTION_MATRIX = DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 11;
46 const Property::Index CameraActor::VIEW_MATRIX = DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 12;
47 const Property::Index CameraActor::INVERT_Y_AXIS = DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 13;
51 bool CameraActor::mFirstInstance = true;
52 Actor::DefaultPropertyLookup* CameraActor::mDefaultCameraActorPropertyLookup = NULL;
57 // calculate the far plane distance for a 16bit depth buffer with 4 bits per unit precision
58 void CalculateClippingAndZ( float width, float height, float& nearClippingPlane, float& farClippingPlane, float& cameraZ )
60 nearClippingPlane = std::max( width, height );
61 farClippingPlane = nearClippingPlane + static_cast<float>( 0xFFFF >> 4 );
62 cameraZ = 2.0f * nearClippingPlane;
67 return Dali::CameraActor::New();
70 TypeRegistration mType( typeid(Dali::CameraActor), typeid(Dali::Actor), Create );
72 const std::string DEFAULT_CAMERA_ACTOR_PROPERTY_NAMES[] =
78 "near-plane-distance",
80 "left-plane-distance",
81 "right-plane-distance",
83 "bottom-plane-distance",
89 const int DEFAULT_CAMERA_ACTOR_PROPERTY_COUNT = sizeof( DEFAULT_CAMERA_ACTOR_PROPERTY_NAMES ) / sizeof( std::string );
91 const Property::Type DEFAULT_CAMERA_ACTOR_PROPERTY_TYPES[DEFAULT_CAMERA_ACTOR_PROPERTY_COUNT] =
93 Property::STRING, // "type",
94 Property::STRING, // "projection-mode",
95 Property::FLOAT, // "field-of-view",
96 Property::FLOAT, // "aspect-ratio",
97 Property::FLOAT, // "near-plane-distance",
98 Property::FLOAT, // "far-plane-distance",
99 Property::FLOAT, // "left-plane-distance",
100 Property::FLOAT, // "right-plane-distance",
101 Property::FLOAT, // "top-plane-distance",
102 Property::FLOAT, // "bottom-plane-distance",
103 Property::VECTOR3, // "target-position",
104 Property::MATRIX, // "projection-matrix",
105 Property::MATRIX, // "view-matrix",
106 Property::BOOLEAN, // "invert-y-axis",
110 * Builds the picking ray in the world reference system from an orthographic camera
111 * The ray origin is the screen coordinate in the near plane translated to a parallel
112 * plane at the camera origin. The ray direction is the direction the camera is facing
113 * (i.e. Z=-1 in view space).
115 void BuildOrthoPickingRay( const Matrix& viewMatrix,
116 const Matrix& projectionMatrix,
117 const Viewport& viewport,
122 float nearPlaneDistance )
124 // inv( modelMatrix ) inv( viewMatrix ) inv( projectionMatrix ) normalize
125 // <----------------- <----------------- <-------------- <-------------
126 // Local World Camera Normalized Screen
127 // reference reference reference clip coordinates
128 // system system system coordinates
129 // -----------------> -----------------> --------------> ------------->
130 // modelMatrix viewMatrix projectionMatrix viewport
132 // Transforms the touch point from the screen reference system to the world reference system.
133 Matrix invViewProjection( false ); // Don't initialize.
134 Matrix::Multiply( invViewProjection, viewMatrix, projectionMatrix );
135 if( !invViewProjection.Invert() )
137 DALI_ASSERT_DEBUG( false );
140 Vector4 near( screenX - viewport.x, viewport.height - (screenY - viewport.y), 0.f, 1.f );
141 if( !Unproject( near, invViewProjection, viewport.width, viewport.height, rayOrigin ) )
143 DALI_ASSERT_DEBUG( false );
146 Matrix invView = viewMatrix;
147 if( !invView.Invert() )
149 DALI_ASSERT_DEBUG( false );
152 Vector4 cameraOrigin = invView * Vector4( 0.f, 0.f, 0.f, 1.f );
153 Vector4 nearPlaneOrigin = invView * Vector4( 0.0f, 0.0f, -nearPlaneDistance, 1.0f);
155 // Vector pointing from the camera to the near plane
156 rayDir = cameraOrigin - nearPlaneOrigin;
164 CameraActorPtr CameraActor::New( const Size& size )
166 CameraActorPtr actor(new CameraActor());
168 // Second-phase construction
172 actor->SetName("DefaultCamera");
174 // Create the attachment
175 actor->mCameraAttachment = CameraAttachment::New( *actor->mNode );
177 actor->Attach(*actor->mCameraAttachment);
179 actor->SetPerspectiveProjection( size );
184 void CameraActor::OnInitialize()
186 if(CameraActor::mFirstInstance)
188 mDefaultCameraActorPropertyLookup = new DefaultPropertyLookup();
189 const int start = DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
190 for ( int i = 0; i < DEFAULT_CAMERA_ACTOR_PROPERTY_COUNT; ++i )
192 (*mDefaultCameraActorPropertyLookup)[DEFAULT_CAMERA_ACTOR_PROPERTY_NAMES[i]] = i + start;
194 CameraActor::mFirstInstance = false;
198 CameraActor::CameraActor()
199 : Actor( Actor::BASIC )
203 CameraActor::~CameraActor()
207 void CameraActor::SetType( Dali::Camera::Type type )
209 mCameraAttachment->SetType(type);
212 Dali::Camera::Type CameraActor::GetType() const
214 return mCameraAttachment->GetType();
217 void CameraActor::SetProjectionMode( Dali::Camera::ProjectionMode mode )
219 mCameraAttachment->SetProjectionMode(mode);
222 Dali::Camera::ProjectionMode CameraActor::GetProjectionMode() const
224 return mCameraAttachment->GetProjectionMode();
227 void CameraActor::SetFieldOfView( float fieldOfView )
229 mCameraAttachment->SetFieldOfView(fieldOfView);
232 float CameraActor::GetFieldOfView( ) const
234 return mCameraAttachment->GetFieldOfView();
237 void CameraActor::SetAspectRatio( float aspectRatio )
239 mCameraAttachment->SetAspectRatio(aspectRatio);
242 float CameraActor::GetAspectRatio( ) const
244 return mCameraAttachment->GetAspectRatio();
247 void CameraActor::SetNearClippingPlane( float nearClippingPlane )
249 mCameraAttachment->SetNearClippingPlane(nearClippingPlane);
252 float CameraActor::GetNearClippingPlane( ) const
254 return mCameraAttachment->GetNearClippingPlane();
257 void CameraActor::SetFarClippingPlane( float farClippingPlane )
259 mCameraAttachment->SetFarClippingPlane(farClippingPlane);
262 float CameraActor::GetFarClippingPlane( ) const
264 return mCameraAttachment->GetFarClippingPlane();
267 void CameraActor::SetTargetPosition(const Vector3& target)
269 mCameraAttachment->SetTargetPosition(target);
272 Vector3 CameraActor::GetTargetPosition() const
274 return mCameraAttachment->GetTargetPosition();
277 void CameraActor::SetInvertYAxis(bool invertYAxis)
279 mCameraAttachment->SetInvertYAxis(invertYAxis);
282 bool CameraActor::GetInvertYAxis() const
284 return mCameraAttachment->GetInvertYAxis();
287 void CameraActor::SetPerspectiveProjection( const Size& size )
289 float width = size.width;
290 float height = size.height;
292 if( Size::ZERO == size )
294 if( Stage::IsInstalled() )
296 const Size& stageSize = mStage->GetSize();
298 width = stageSize.width;
299 height = stageSize.height;
303 if( ( width < Math::MACHINE_EPSILON_1000 ) || ( height < Math::MACHINE_EPSILON_1000 ) )
305 // On the stage initialization this method is called but the size has not been set.
306 // There is no point to set any value if width or height is zero.
310 float nearClippingPlane;
311 float farClippingPlane;
313 CalculateClippingAndZ( width, height, nearClippingPlane, farClippingPlane, cameraZ );
315 // calculate the position of the camera to have the desired aspect ratio
316 const float fieldOfView = 2.0f * std::atan( height * 0.5f / cameraZ );
318 // unless it is too small, we want at least as much space to the back as we have torwards the front
319 const float minClippingFarPlane = 2.f * nearClippingPlane;
320 if ( farClippingPlane < minClippingFarPlane )
322 farClippingPlane = minClippingFarPlane;
325 const float aspectRatio = width / height;
327 SetProjectionMode(Dali::Camera::PERSPECTIVE_PROJECTION);
328 SetFieldOfView( fieldOfView );
329 SetNearClippingPlane( nearClippingPlane );
330 SetFarClippingPlane( farClippingPlane );
331 SetAspectRatio( aspectRatio );
336 void CameraActor::SetOrthographicProjection( const Vector2& size )
338 // Choose near, far and Z parameters to match the SetPerspectiveProjection above.
339 float nearClippingPlane;
340 float farClippingPlane;
342 CalculateClippingAndZ( size.width, size.height, nearClippingPlane, farClippingPlane, cameraZ );
343 SetOrthographicProjection( -size.x*0.5f, size.x*0.5f, size.y*0.5f, -size.y*0.5f,
344 nearClippingPlane, farClippingPlane );
348 void CameraActor::SetOrthographicProjection( float left, float right, float top, float bottom, float near, float far )
350 mCameraAttachment->SetLeftClippingPlane(left);
351 mCameraAttachment->SetRightClippingPlane(right);
352 mCameraAttachment->SetTopClippingPlane(top);
353 mCameraAttachment->SetBottomClippingPlane(bottom);
354 SetNearClippingPlane( near );
355 SetFarClippingPlane( far );
356 SetProjectionMode(Dali::Camera::ORTHOGRAPHIC_PROJECTION);
359 bool CameraActor::BuildPickingRay( const Vector2& screenCoordinates,
360 const Viewport& viewport,
362 Vector4& rayDirection )
365 if( GetProjectionMode() == Dali::Camera::PERSPECTIVE_PROJECTION )
367 // Build a picking ray in the world reference system.
368 // ray starts from the camera world position
369 rayOrigin = mNode->GetWorldPosition( mStage->GetEventBufferIndex() );
372 // Transform the touch point from the screen coordinate system to the world coordinates system.
373 Vector4 near( screenCoordinates.x - viewport.x, viewport.height - (screenCoordinates.y - viewport.y), 0.f, 1.f );
374 if( !Unproject( near, mCameraAttachment->GetInverseViewProjectionMatrix(), viewport.width, viewport.height, near ) )
376 // unproject failed so no picking ray possible
380 // Compute the ray's director vector.
381 rayDirection.x = near.x - rayOrigin.x;
382 rayDirection.y = near.y - rayOrigin.y;
383 rayDirection.z = near.z - rayOrigin.z;
384 rayDirection.Normalize();
385 rayDirection.w = 1.f;
389 float nearPlaneDistance = GetNearClippingPlane();
390 BuildOrthoPickingRay( GetViewMatrix(),
391 GetProjectionMatrix(),
392 viewport, screenCoordinates.x,
402 const Matrix& CameraActor::GetViewMatrix() const
406 return mCameraAttachment->GetViewMatrix();
410 return Matrix::IDENTITY;
414 const Matrix& CameraActor::GetProjectionMatrix() const
418 return mCameraAttachment->GetProjectionMatrix();
422 return Matrix::IDENTITY;
426 unsigned int CameraActor::GetDefaultPropertyCount() const
428 return Actor::GetDefaultPropertyCount() + DEFAULT_CAMERA_ACTOR_PROPERTY_COUNT;
431 void CameraActor::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
433 Actor::GetDefaultPropertyIndices( indices ); // Actor class properties
435 indices.reserve( indices.size() + DEFAULT_CAMERA_ACTOR_PROPERTY_COUNT );
437 int index = DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
438 for ( int i = 0; i < DEFAULT_CAMERA_ACTOR_PROPERTY_COUNT; ++i, ++index )
440 indices.push_back( index );
444 bool CameraActor::IsDefaultPropertyWritable( Property::Index index ) const
446 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
448 return Actor::IsDefaultPropertyWritable(index);
452 if( Dali::CameraActor::PROJECTION_MATRIX == index || Dali::CameraActor::VIEW_MATRIX == index )
463 bool CameraActor::IsDefaultPropertyAnimatable( Property::Index index ) const
465 bool animatable = false; // Our properties are not animatable.
467 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
469 animatable = Actor::IsDefaultPropertyAnimatable(index);
474 Property::Type CameraActor::GetDefaultPropertyType( Property::Index index ) const
476 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
478 return Actor::GetDefaultPropertyType(index);
482 index -= DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
484 if ( ( index >= 0 ) && ( index < DEFAULT_CAMERA_ACTOR_PROPERTY_COUNT ) )
486 return DEFAULT_CAMERA_ACTOR_PROPERTY_TYPES[index];
490 // index out-of-bounds
491 return Property::NONE;
496 const std::string& CameraActor::GetDefaultPropertyName( Property::Index index ) const
498 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
500 return Actor::GetDefaultPropertyName(index);
504 index -= DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
506 if ( ( index >= 0 ) && ( index < DEFAULT_CAMERA_ACTOR_PROPERTY_COUNT ) )
508 return DEFAULT_CAMERA_ACTOR_PROPERTY_NAMES[index];
512 // index out-of-bounds
513 static const std::string INVALID_PROPERTY_NAME;
514 return INVALID_PROPERTY_NAME;
519 Property::Index CameraActor::GetDefaultPropertyIndex(const std::string& name) const
521 Property::Index index = Property::INVALID_INDEX;
523 DALI_ASSERT_DEBUG( NULL != mDefaultCameraActorPropertyLookup );
525 // Look for name in current class' default properties
526 DefaultPropertyLookup::const_iterator result = mDefaultCameraActorPropertyLookup->find( name );
527 if ( mDefaultCameraActorPropertyLookup->end() != result )
529 index = result->second;
533 // If not found, check in base class
534 index = Actor::GetDefaultPropertyIndex( name );
540 void CameraActor::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
542 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
544 Actor::SetDefaultProperty(index, propertyValue);
548 DALI_ASSERT_DEBUG(mCameraAttachment && "where is the camera?");
551 case Dali::CameraActor::TYPE:
553 std::string s(propertyValue.Get<std::string>());
554 if(s == "LOOK_AT_TARGET")
556 mCameraAttachment->SetType(Dali::Camera::LOOK_AT_TARGET);
558 else if(s == "FREE_LOOK")
560 mCameraAttachment->SetType(Dali::Camera::FREE_LOOK);
564 DALI_LOG_WARNING("Unknown camera type\n");
568 case Dali::CameraActor::PROJECTION_MODE:
570 std::string s(propertyValue.Get<std::string>());
571 if(s == "PERSPECTIVE_PROJECTION")
573 mCameraAttachment->SetProjectionMode(Dali::Camera::PERSPECTIVE_PROJECTION);
575 else if(s == "ORTHOGRAPHIC_PROJECTION")
577 mCameraAttachment->SetProjectionMode(Dali::Camera::ORTHOGRAPHIC_PROJECTION);
581 DALI_LOG_WARNING("Unknown projection mode\n");
585 case Dali::CameraActor::FIELD_OF_VIEW:
587 mCameraAttachment->SetFieldOfView(propertyValue.Get<float>());
590 case Dali::CameraActor::ASPECT_RATIO:
592 mCameraAttachment->SetAspectRatio(propertyValue.Get<float>());
595 case Dali::CameraActor::LEFT_PLANE_DISTANCE:
597 mCameraAttachment->SetLeftClippingPlane(propertyValue.Get<float>());
600 case Dali::CameraActor::RIGHT_PLANE_DISTANCE:
602 mCameraAttachment->SetRightClippingPlane(propertyValue.Get<float>());
605 case Dali::CameraActor::TOP_PLANE_DISTANCE:
607 mCameraAttachment->SetTopClippingPlane(propertyValue.Get<float>());
610 case Dali::CameraActor::BOTTOM_PLANE_DISTANCE:
612 mCameraAttachment->SetBottomClippingPlane(propertyValue.Get<float>());
615 case Dali::CameraActor::NEAR_PLANE_DISTANCE:
617 mCameraAttachment->SetNearClippingPlane(propertyValue.Get<float>());
620 case Dali::CameraActor::FAR_PLANE_DISTANCE:
622 mCameraAttachment->SetFarClippingPlane(propertyValue.Get<float>());
625 case Dali::CameraActor::TARGET_POSITION:
627 mCameraAttachment->SetTargetPosition(propertyValue.Get<Vector3>());
630 case Dali::CameraActor::PROJECTION_MATRIX:
632 DALI_LOG_WARNING("projection-matrix property is not animatable \n");
635 case Dali::CameraActor::VIEW_MATRIX:
637 DALI_LOG_WARNING("view-matrix property is not animatable \n");
640 case Dali::CameraActor::INVERT_Y_AXIS:
642 mCameraAttachment->SetInvertYAxis(propertyValue.Get<bool>());
647 DALI_LOG_WARNING("Unknown property (%d)\n", index);
655 Property::Value CameraActor::GetDefaultProperty( Property::Index index ) const
658 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
660 ret = Actor::GetDefaultProperty(index);
664 DALI_ASSERT_DEBUG(mCameraAttachment && "where is the camera?");
668 case Dali::CameraActor::TYPE:
670 if(mCameraAttachment->GetType() == Dali::Camera::LOOK_AT_TARGET)
672 ret = "LOOK_AT_TARGET";
674 else if(mCameraAttachment->GetType() == Dali::Camera::FREE_LOOK)
681 DALI_ASSERT_DEBUG("Unknown camera type\n");
687 case Dali::CameraActor::PROJECTION_MODE:
689 if(mCameraAttachment->GetProjectionMode() == Dali::Camera::PERSPECTIVE_PROJECTION)
691 ret = "PERSPECTIVE_PROJECTION";
693 else if(mCameraAttachment->GetProjectionMode() == Dali::Camera::ORTHOGRAPHIC_PROJECTION)
695 ret = "ORTHOGRAPHIC_PROJECTION";
700 DALI_ASSERT_DEBUG("Unknown projection mode\n");
704 case Dali::CameraActor::FIELD_OF_VIEW:
706 ret = mCameraAttachment->GetFieldOfView();
709 case Dali::CameraActor::ASPECT_RATIO:
711 ret = mCameraAttachment->GetAspectRatio();
714 case Dali::CameraActor::LEFT_PLANE_DISTANCE:
716 ret = mCameraAttachment->GetLeftClippingPlane();
719 case Dali::CameraActor::RIGHT_PLANE_DISTANCE:
721 ret = mCameraAttachment->GetRightClippingPlane();
724 case Dali::CameraActor::TOP_PLANE_DISTANCE:
726 ret = mCameraAttachment->GetTopClippingPlane();
729 case Dali::CameraActor::BOTTOM_PLANE_DISTANCE:
731 ret = mCameraAttachment->GetBottomClippingPlane();
734 case Dali::CameraActor::NEAR_PLANE_DISTANCE:
736 ret = mCameraAttachment->GetNearClippingPlane();
739 case Dali::CameraActor::FAR_PLANE_DISTANCE:
741 ret = mCameraAttachment->GetFarClippingPlane();
744 case Dali::CameraActor::TARGET_POSITION:
746 ret = mCameraAttachment->GetTargetPosition();
749 case Dali::CameraActor::PROJECTION_MATRIX:
751 ret = mCameraAttachment->GetProjectionMatrix();
754 case Dali::CameraActor::VIEW_MATRIX:
756 ret = mCameraAttachment->GetViewMatrix();
759 case Dali::CameraActor::INVERT_Y_AXIS:
761 ret = mCameraAttachment->GetInvertYAxis();
766 DALI_LOG_WARNING("Unknown property (%d)\n", index);
775 const SceneGraph::PropertyBase* CameraActor::GetSceneObjectAnimatableProperty( Property::Index index ) const
777 DALI_ASSERT_ALWAYS( IsPropertyAnimatable(index) && "Property is not animatable" );
779 const SceneGraph::PropertyBase* property( NULL );
781 // This method should only return a property of an object connected to the scene-graph
787 // let actor handle animatable properties, we have no animatable properties
788 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
790 property = Actor::GetSceneObjectAnimatableProperty(index);
796 const PropertyInputImpl* CameraActor::GetSceneObjectInputProperty( Property::Index index ) const
798 const PropertyInputImpl* property( NULL );
800 // This method should only return a property of an object connected to the scene-graph
806 // if its an actor default property or a custom property (actor already handles custom properties)
807 if( ( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT ) || ( index >= DEFAULT_PROPERTY_MAX_COUNT ) )
809 property = Actor::GetSceneObjectInputProperty(index);
815 case Dali::CameraActor::PROJECTION_MATRIX:
817 property = mCameraAttachment->GetProjectionMatrixProperty();
820 case Dali::CameraActor::VIEW_MATRIX:
822 property = mCameraAttachment->GetViewMatrixProperty();
826 DALI_LOG_WARNING("Not an input property (%d)\n", index);
835 } // namespace Internal