2 * Copyright (c) 2020 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.
18 #include "look-camera.h"
20 #include <dali/public-api/render-tasks/render-task-list.h>
21 #include <dali/public-api/render-tasks/render-task.h>
22 #include <dali/public-api/events/touch-event.h>
28 // Input sensitivity, the larger value, the more sensitive input
29 // Default value has been chosen empirically
30 const float CAMERA_SENSITIVITY ( 90.0f );
32 // Vertical angle limit of the camera
33 const float CAMERA_VERTICAL_LIMIT ( 80.0f );
35 // Position where camera is instantiated by default
36 const Vector3 CAMERA_DEFAULT_POSITION ( 0.0f, 0.0f, 3.0f );
38 // Field-of-View in degrees
39 const float CAMERA_DEFAULT_FOV ( 60.0f );
42 const float CAMERA_DEFAULT_NEAR ( 0.1f );
45 const float CAMERA_DEFAULT_FAR ( 100.0f );
47 // Default forward vector
48 const Vector3 CAMERA_FORWARD ( 0.0f, 0.0f, 1.0f );
51 const Vector3 CAMERA_UP ( Vector3::YAXIS );
54 LookCamera::LookCamera()
55 : mCameraYawPitch( 0.0f, 180.0f ),
56 mFovY( CAMERA_DEFAULT_FOV ),
57 mNear( CAMERA_DEFAULT_NEAR ),
58 mFar( CAMERA_DEFAULT_FAR ),
59 mCameraPosition( CAMERA_DEFAULT_POSITION )
63 LookCamera::~LookCamera()
66 mCameraActor.Remove( mInterceptorActor );
69 void LookCamera::Initialise( Window window, const Vector3& position, float fovY, float near, float far )
77 // Camera position is shadowed in order to avoid using.GetCurrentProperty< Vector3 >( Actor::Property::POSITION )
78 mCameraPosition = position;
80 // Initialise default camera
81 InitialiseDefaultCamera();
83 // Create input interceptor actor
84 CreateInterceptorActor();
87 mTimer = Timer::New( 16 );
88 mTimer.TickSignal().Connect( this, &LookCamera::OnTick );
92 bool LookCamera::OnTick()
94 Vector2 windowSize = mWindow.GetSize();
96 // ---------------------------------------------------------------------
98 Vector2 tmp( mScreenLookDelta );
99 mScreenLookDelta = Vector2::ZERO;
101 float yaw = ( (tmp.y / windowSize.x ) * CAMERA_SENSITIVITY );
102 float pitch = ( (tmp.x / windowSize.y ) * CAMERA_SENSITIVITY );
103 mCameraYawPitch.x -= yaw;
104 mCameraYawPitch.y -= pitch;
105 if( abs( mCameraYawPitch.x ) > CAMERA_VERTICAL_LIMIT )
107 mCameraYawPitch.x = CAMERA_VERTICAL_LIMIT * ((mCameraYawPitch.x < 0) ? -1.0f : 1.0f );
111 Quaternion rotX( Degree( mCameraYawPitch.x), Vector3( 1.0f, 0.0f, 0.0f ) );
112 Quaternion rotY( Degree( mCameraYawPitch.y), Vector3( 0.0f, 1.0f, 0.0f ) );
113 rotation = ( rotY * rotX );
115 mCameraActor.SetProperty( Actor::Property::ORIENTATION, rotation );
120 void LookCamera::InitialiseDefaultCamera()
122 mCameraActor = mWindow.GetRenderTaskList().GetTask(0).GetCameraActor();
123 mCameraActor.SetProperty( Dali::Actor::Property::NAME, "LookCamera" );
124 mCameraActor.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
125 mCameraActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
126 mCameraActor.SetFieldOfView( Radian( Degree( mFovY ) ) );
128 // should be read from file
129 mCameraActor.SetNearClippingPlane( mNear );
130 mCameraActor.SetFarClippingPlane( mFar );
131 mCameraActor.SetProperty( Actor::Property::POSITION, mCameraPosition );
134 void LookCamera::CreateInterceptorActor()
136 Vector2 windowSize = mWindow.GetSize();
138 mInterceptorActor = Actor::New();
139 mInterceptorActor.SetProperty( Dali::Actor::Property::NAME, "InputInterceptor" );
140 mInterceptorActor.SetProperty( Actor::Property::SIZE, Vector3( windowSize.width, windowSize.height, 1 ) );
141 mInterceptorActor.SetProperty( Actor::Property::POSITION, Vector3( 0.0, 0.0, 1.0 ) );
142 mInterceptorActor.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
143 mInterceptorActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
144 mCameraActor.Add( mInterceptorActor );
146 // Connect TouchSignal to interceptor actor
147 mInterceptorActor.TouchSignal().Connect( this, &LookCamera::OnTouch );
150 bool LookCamera::OnTouch( Actor actor, const TouchEvent& touch )
152 for( int i = 0; i < (int)touch.GetPointCount() && i < 3; ++i )
154 Vector2 position( touch.GetScreenPosition( i ) );
157 if( touch.GetState( i ) == PointState::STARTED )
159 mOldTouchLookPosition = position;
161 else if( touch.GetState( i ) == PointState::FINISHED ||
162 touch.GetState( i ) == PointState::LEAVE ||
163 touch.GetState( i ) == PointState::INTERRUPTED
166 mScreenLookDelta = Vector2::ZERO;
167 mOldTouchLookPosition = Vector2::ZERO;
171 mScreenLookDelta.x += ( position.x - mOldTouchLookPosition.x );
172 mScreenLookDelta.y += ( position.y - mOldTouchLookPosition.y );
173 mOldTouchLookPosition = position;