2 * Copyright (c) 2017 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/common/stage.h>
21 #include <dali/public-api/render-tasks/render-task-list.h>
22 #include <dali/public-api/render-tasks/render-task.h>
23 #include <dali/public-api/events/touch-data.h>
24 #include <dali/public-api/events/touch-event.h>
30 // Input sensitivity, the larger value, the more sensitive input
31 // Default value has been chosen empirically
32 const float CAMERA_SENSITIVITY ( 90.0f );
34 // Vertical angle limit of the camera
35 const float CAMERA_VERTICAL_LIMIT ( 80.0f );
37 // Position where camera is instantiated by default
38 const Vector3 CAMERA_DEFAULT_POSITION ( 0.0f, 0.0f, 3.0f );
40 // Field-of-View in degrees
41 const float CAMERA_DEFAULT_FOV ( 60.0f );
44 const float CAMERA_DEFAULT_NEAR ( 0.1f );
47 const float CAMERA_DEFAULT_FAR ( 100.0f );
49 // Default forward vector
50 const Vector3 CAMERA_FORWARD ( 0.0f, 0.0f, 1.0f );
53 const Vector3 CAMERA_UP ( Vector3::YAXIS );
56 LookCamera::LookCamera()
57 : mCameraYawPitch( 0.0f, 180.0f ),
58 mFovY( CAMERA_DEFAULT_FOV ),
59 mNear( CAMERA_DEFAULT_NEAR ),
60 mFar( CAMERA_DEFAULT_FAR ),
61 mCameraPosition( CAMERA_DEFAULT_POSITION )
65 LookCamera::~LookCamera()
68 mCameraActor.Remove( mInterceptorActor );
71 void LookCamera::Initialise( const Vector3& position, float fovY, float near, float far )
77 // Camera position is shadowed in order to avoid using GetCurrentPosition()
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 stageSize = Stage::GetCurrent().GetSize();
96 // ---------------------------------------------------------------------
98 Vector2 tmp( mScreenLookDelta );
99 mScreenLookDelta = Vector2::ZERO;
101 float yaw = ( (tmp.y / stageSize.x ) * CAMERA_SENSITIVITY );
102 float pitch = ( (tmp.x / stageSize.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.SetOrientation( rotation );
120 void LookCamera::InitialiseDefaultCamera()
122 Stage stage = Stage::GetCurrent();
123 mCameraActor = stage.GetRenderTaskList().GetTask(0).GetCameraActor();
124 mCameraActor.SetName( "LookCamera" );
125 mCameraActor.SetAnchorPoint( AnchorPoint::CENTER );
126 mCameraActor.SetParentOrigin( ParentOrigin::CENTER );
127 mCameraActor.SetFieldOfView( Radian( Degree( mFovY ) ) );
129 // should be read from file
130 mCameraActor.SetNearClippingPlane( mNear );
131 mCameraActor.SetFarClippingPlane( mFar );
132 mCameraActor.SetPosition( mCameraPosition );
135 void LookCamera::CreateInterceptorActor()
137 Stage stage = Stage::GetCurrent();
139 mInterceptorActor = Actor::New();
140 mInterceptorActor.SetName( "InputInterceptor" );
141 mInterceptorActor.SetSize( Vector3( stage.GetSize().x, stage.GetSize().y, 1 ) );
142 mInterceptorActor.SetPosition( Vector3( 0.0, 0.0, 1.0 ) );
143 mInterceptorActor.SetAnchorPoint( AnchorPoint::CENTER );
144 mInterceptorActor.SetParentOrigin( ParentOrigin::CENTER );
145 mCameraActor.Add( mInterceptorActor );
147 // Connect TouchSignal to interceptor actor
148 mInterceptorActor.TouchSignal().Connect( this, &LookCamera::OnTouch );
151 bool LookCamera::OnTouch( Actor actor, const TouchData& touch )
153 Stage stage = Stage::GetCurrent();
155 for( int i = 0; i < (int)touch.GetPointCount() && i < 3; ++i )
157 Vector2 position( touch.GetScreenPosition( i ) );
160 if( touch.GetState( i ) == PointState::STARTED )
162 mOldTouchLookPosition = position;
164 else if( touch.GetState( i ) == PointState::FINISHED ||
165 touch.GetState( i ) == PointState::LEAVE ||
166 touch.GetState( i ) == PointState::INTERRUPTED
169 mScreenLookDelta = Vector2::ZERO;
170 mOldTouchLookPosition = Vector2::ZERO;
174 mScreenLookDelta.x += ( position.x - mOldTouchLookPosition.x );
175 mScreenLookDelta.y += ( position.y - mOldTouchLookPosition.y );
176 mOldTouchLookPosition = position;