gesture_camera.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014 Google Inc. All Rights Reserved.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
18 #include "tango-gl/util.h"
19 #include "glm/gtx/quaternion.hpp"
20 
21 namespace {
22 // Render camera observation distance in third person camera mode.
23 const float kThirdPersonCameraDist = 7.0f;
24 
25 // Render camera observation distance in third person camera mode.
26 const float kThirdPersonFollowCameraDist = 2.0f;
27 
28 // Render camera observation distance in top down camera mode.
29 const float kTopDownCameraDist = 5.0f;
30 
31 // Zoom in speed.
32 const float kZoomSpeed = 10.0f;
33 
34 // Move speed
35 const float kMoveSpeed = 10.0f;
36 
37 // Rotation speed
38 const float kRotationSpeed = 2.0f;
39 
40 // Min/max clamp value of camera observation distance.
41 const float kCamViewMinDist = .1f;
42 const float kCamViewMaxDist = 100.f;
43 
44 // FOV set up values.
45 // Third and top down camera's FOV is 65 degrees.
46 // First person camera's FOV is 85 degrees.
47 const float kHighestFov = 120.0f;
48 const float kHighFov = 85.0f;
49 const float kLowFov = 65.0f;
50 const float kLowestFov = 40.0f;
51 }
52 
53 namespace tango_gl {
54 
56  cam_cur_target_rot_(1,0,0,0),
57  start_touch_dist_(0.0f),
58  cur_touch_dist_(0.0f)
59 {
62 }
63 
65 
66 void GestureCamera::OnTouchEvent(int touch_count, TouchEvent event, float x0,
67  float y0, float x1, float y1) {
68 
69  if (camera_type_!=kFirstPerson && touch_count == 1) {
70  switch (event) {
71  case kTouch0Down: {
73 
76  break;
77  }
78  case kTouchMove: {
80 
81  float rotation_x = (touch0_start_position_.y - y0) * kRotationSpeed;
82  float rotation_y = (touch0_start_position_.x - x0) * kRotationSpeed;
83 
85  cam_cur_angle_.x = cam_start_angle_.x + rotation_x;
86  cam_cur_angle_.y = cam_start_angle_.y + rotation_y;
87 
89 
90  break;
91  }
92  default: { break; }
93  }
94  }
95  if (touch_count == 2) {
96  switch (event) {
97  case kTouch1Down: {
98  float abs_x = x0 - x1;
99  float abs_y = y0 - y1;
100  start_touch_dist_ = std::sqrt(abs_x * abs_x + abs_y * abs_y);
102  cam_start_fov_ = this->getFOV();
103 
104  // center touch
105  touch0_start_position_.x = (x0+x1)/2.0f;
106  touch0_start_position_.y = (y0+y1)/2.0f;
107  break;
108  }
109  case kTouchMove: {
110  float abs_x = x0 - x1;
111  float abs_y = y0 - y1;
112  float dist = start_touch_dist_ - std::sqrt(abs_x * abs_x + abs_y * abs_y);
113 
115  {
116  this->SetFieldOfView(tango_gl::util::Clamp(cam_start_fov_ + dist * kZoomSpeed*10.0f, kLowestFov, kHighestFov));
117  }
118  else
119  {
121  kCamViewMinDist, kCamViewMaxDist);
122 
124  if(camera_type_ == kTopOrtho)
125  {
127  }
128 
129  glm::vec2 touch_center_position((x0+x1)/2.0f, (y0+y1)/2.0f);
131  offset.x = (touch_center_position.x - touch0_start_position_.x) * kMoveSpeed;
132  offset.y = (touch_center_position.y - touch0_start_position_.y) * kMoveSpeed;
133  touch0_start_position_ = touch_center_position;
134 
136 
138  }
139  break;
140  }
141  default: { break; }
142  }
143  }
144 }
145 
147  float normalized_y,
148  float touch_range) {
149  float screen_height = touch_range * (2.0f * glm::tan(field_of_view_ * 0.5f));
150  float screen_width = screen_height * aspect_ratio_;
151  // normalized_x and normalized_x are from OnTouchEvent, top-left corner of the
152  // screen
153  // is [0, 0], transform it to opengl frame.
154  normalized_x = normalized_x - 0.5f;
155  normalized_y = 0.5f - normalized_y;
156  glm::vec3 start =
159  glm::vec3(normalized_x * screen_width, normalized_y * screen_height,
160  -touch_range));
161  Segment segment(start, end);
162  return segment;
163 }
164 
166  // Anchor position
168 
169  // Anchor rotation
171  {
177  }
178 }
179 
181  camera_type_ = camera_index;
182  switch (camera_index) {
183  case kFirstPerson:
184  SetOrthoMode(false);
185  SetFieldOfView(kLowestFov);
186  SetNearFarClipPlanes(0.25, 25);
187  SetPosition(glm::vec3(0.0f, 0.0f, 0.0f));
188  SetRotation(glm::quat(1.0f, 0.0f, 0.0f, 0.0f));
189  cam_cur_dist_ = 0.0f;
190  anchor_offset_ = glm::vec3(0.0f,0.0f,0.0f);
191  cam_cur_angle_.x = 0.0f;
192  cam_cur_angle_.y = 0.0f;
193  cam_cur_target_rot_ = glm::quat(1,0,0,0);
196  break;
197  case kThirdPerson:
198  case kThirdPersonFollow:
199  SetOrthoMode(false);
200  SetFieldOfView(kLowFov);
201  SetNearFarClipPlanes(1, 50);
202  SetPosition(glm::vec3(0.0f, 0.0f, 0.0f));
203  SetRotation(glm::quat(1.0f, 0.0f, 0.0f, 0.0f));
204  cam_cur_dist_ = camera_index==kThirdPersonFollow?kThirdPersonFollowCameraDist:kThirdPersonCameraDist;
205  anchor_offset_ = glm::vec3(0.0f,0.0f,0.0f);
206  cam_cur_angle_.x = -M_PI / 12.0f;
208  cam_cur_target_rot_ = glm::quat(1,0,0,0);
210  break;
211  case kTopDown:
212  SetPosition(glm::vec3(0.0f, 0.0f, 0.0f));
213  SetRotation(glm::quat(1.0f, 0.0f, 0.0f, 0.0f));
214  SetOrthoMode(false);
215  SetFieldOfView(kLowFov);
216  SetNearFarClipPlanes(1, 50);
217  cam_cur_dist_ = kTopDownCameraDist;
218  anchor_offset_ = glm::vec3(0.0f,0.0f,0.0f);
219  cam_cur_angle_.x = -M_PI / 2.0f;
220  cam_cur_angle_.y = 0.0f;
221  cam_cur_target_rot_ = glm::quat(1,0,0,0);
223  break;
224  case kTopOrtho:
225  SetPosition(glm::vec3(0.0f, 0.0f, 0.0f));
226  SetRotation(glm::quat(1.0f, 0.0f, 0.0f, 0.0f));
227  SetOrthoMode(true);
228  SetOrthoScale(kTopDownCameraDist);
229  SetOrthoCropFactor(-1.0f);
230  SetFieldOfView(kLowFov);
231  SetNearFarClipPlanes(1, 50);
232  cam_cur_dist_ = kTopDownCameraDist;
233  anchor_offset_ = glm::vec3(0.0f,0.0f,0.0f);
234  cam_cur_angle_.x = -M_PI / 2.0f;
235  cam_cur_angle_.y = 0.0f;
236  cam_cur_target_rot_ = glm::quat(1,0,0,0);
238  break;
239  default:
240  break;
241  }
242 }
243 
245 {
246  //Anchor rotation
247  glm::quat parent_cam_rot = glm::rotate(cam_cur_target_rot_, cam_cur_angle_.y, glm::vec3(0, 1, 0));
248  parent_cam_rot = glm::rotate(parent_cam_rot, cam_cur_angle_.x, glm::vec3(1, 0, 0));
249  cam_parent_transform_->SetRotation(parent_cam_rot);
250 
251  //Camera position
253 }
254 } // namespace tango_gl
tango_gl::Camera::aspect_ratio_
float aspect_ratio_
Definition: camera.h:60
tango_gl::Transform::GetPosition
glm::vec3 GetPosition() const
Definition: transform.cpp:39
tango_gl::GestureCamera::getFOV
float getFOV() const
Definition: gesture_camera.h:69
glm::detail::tvec2::x
T x
Definition: type_vec2.hpp:84
glm::tan
GLM_FUNC_DECL genType tan(genType const &angle)
tango_gl::Camera::SetOrthoMode
void SetOrthoMode(bool enabled)
Definition: camera.h:32
tango_gl::GestureCamera::kTopOrtho
@ kTopOrtho
Definition: gesture_camera.h:32
x1
x1
tango_gl::GestureCamera::kTouchMove
@ kTouchMove
Definition: gesture_camera.h:39
tango_gl::util::ApplyTransform
glm::vec3 ApplyTransform(const glm::mat4 &mat, const glm::vec3 &vec)
Definition: util.cpp:237
tango_gl::GestureCamera::SetCameraType
void SetCameraType(CameraType camera_index)
Definition: gesture_camera.cpp:180
tango_gl::GestureCamera::kThirdPerson
@ kThirdPerson
Definition: gesture_camera.h:33
glm::detail::tvec2::y
T y
Definition: type_vec2.hpp:85
tango_gl::GestureCamera::kFirstPerson
@ kFirstPerson
Definition: gesture_camera.h:29
glm::quat
highp_quat quat
Quaternion of default single-precision floating-point numbers.
Definition: fwd.hpp:68
glm::rotate
GLM_FUNC_DECL detail::tmat4x4< T, P > rotate(detail::tmat4x4< T, P > const &m, T const &angle, detail::tvec3< T, P > const &axis)
tango_gl::GestureCamera::start_touch_dist_
float start_touch_dist_
Definition: gesture_camera.h:88
end
end
tango_gl::Camera::SetOrthoScale
void SetOrthoScale(float scale)
Definition: camera.h:33
util.h
tango_gl::GestureCamera::cam_cur_target_rot_
glm::quat cam_cur_target_rot_
Definition: gesture_camera.h:81
glm::vec3
highp_vec3 vec3
Definition: type_vec.hpp:392
tango_gl::GestureCamera::GetSegmentFromTouch
Segment GetSegmentFromTouch(float normalized_x, float normalized_y, float touch_range)
Definition: gesture_camera.cpp:146
glm::normalize
GLM_FUNC_DECL genType normalize(genType const &x)
tango_gl::Transform::SetPosition
void SetPosition(const glm::vec3 &position)
Definition: transform.cpp:35
glm::detail::tquat
Definition: fwd.hpp:41
glm::detail::tquat::z
T z
Definition: gtc/quaternion.hpp:65
glm::detail::tvec3
Definition: type_mat.hpp:37
tango_gl::Transform::Transform
Transform()
Definition: transform.cpp:24
tango_gl::GestureCamera::CameraType
CameraType
Definition: gesture_camera.h:28
tango_gl::Camera::SetFieldOfView
void SetFieldOfView(const float fov)
Definition: camera.cpp:52
tango_gl::Camera::field_of_view_
float field_of_view_
Definition: camera.h:59
tango_gl::GestureCamera::kThirdPersonFollow
@ kThirdPersonFollow
Definition: gesture_camera.h:30
tango_gl::Segment
Definition: segment.h:23
glm::detail::tvec3::z
T z
Definition: type_vec3.hpp:86
tango_gl::GestureCamera::cam_cur_dist_
float cam_cur_dist_
Definition: gesture_camera.h:85
glm::sqrt
GLM_FUNC_DECL vecType< T, P > sqrt(vecType< T, P > const &x)
tango_gl::GestureCamera::cam_start_dist_
float cam_start_dist_
Definition: gesture_camera.h:83
tango_gl::GestureCamera::cam_cur_angle_
glm::vec2 cam_cur_angle_
Definition: gesture_camera.h:80
x0
x0
tango_gl::GestureCamera::GestureCamera
GestureCamera()
Definition: gesture_camera.cpp:55
tango_gl::GestureCamera::TouchEvent
TouchEvent
Definition: gesture_camera.h:36
tango_gl::GestureCamera::cam_start_fov_
float cam_start_fov_
Definition: gesture_camera.h:84
tango_gl::GestureCamera::cam_start_angle_
glm::vec2 cam_start_angle_
Definition: gesture_camera.h:79
tango_gl::GestureCamera::camera_type_
CameraType camera_type_
Definition: gesture_camera.h:77
offset
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set set set set surface set nocontour set clabel set mapping cartesian set nohidden3d set cntrparam order set cntrparam linear set cntrparam levels auto set cntrparam points set size set set xzeroaxis lt lw set x2zeroaxis lt lw set yzeroaxis lt lw set y2zeroaxis lt lw set tics in set ticslevel set tics set mxtics default set mytics default set mx2tics default set my2tics default set xtics border mirror norotate autofreq set ytics border mirror norotate autofreq set ztics border nomirror norotate autofreq set nox2tics set noy2tics set timestamp bottom norotate offset
quaternion.hpp
tango_gl::GestureCamera::kTopDown
@ kTopDown
Definition: gesture_camera.h:31
tango_gl::GestureCamera::touch0_start_position_
glm::vec2 touch0_start_position_
Definition: gesture_camera.h:91
f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
y1
CEPHES_EXTERN_EXPORT double y1(double x)
M_PI
#define M_PI
Definition: tango-gl/include/tango-gl/util.h:66
segment
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE FixedSegmentReturnType< N >::Type segment(Index start, Index n=N)
tango_gl::GestureCamera::kTouch1Down
@ kTouch1Down
Definition: gesture_camera.h:40
tango_gl::GestureCamera::StartCameraToCurrentTransform
void StartCameraToCurrentTransform()
Definition: gesture_camera.cpp:244
glm::rotation
GLM_FUNC_DECL detail::tquat< T, P > rotation(detail::tvec3< T, P > const &orig, detail::tvec3< T, P > const &dest)
y0
CEPHES_EXTERN_EXPORT double y0(double x)
tango_gl::GestureCamera::kTouch0Down
@ kTouch0Down
Definition: gesture_camera.h:37
tango_gl::Camera::SetOrthoCropFactor
void SetOrthoCropFactor(float value)
Definition: camera.h:34
tango_gl::GestureCamera::anchor_offset_
glm::vec3 anchor_offset_
Definition: gesture_camera.h:86
tango_gl::util::Clamp
float Clamp(float value, float min, float max)
Definition: util.cpp:165
tango_gl::Transform::SetRotation
void SetRotation(const glm::quat &rotation)
Definition: transform.cpp:43
tango_gl::GestureCamera::SetAnchorPosition
void SetAnchorPosition(const glm::vec3 &pos, const glm::quat &rotation)
Definition: gesture_camera.cpp:165
tango_gl
Definition: axis.cpp:20
gesture_camera.h
pos
dist
dist
tango_gl::Camera::SetNearFarClipPlanes
void SetNearFarClipPlanes(const float near, const float far)
Definition: camera.cpp:56
tango_gl::Transform::GetTransformationMatrix
glm::mat4 GetTransformationMatrix() const
Definition: transform.cpp:67
tango_gl::Transform::GetRotation
glm::quat GetRotation() const
Definition: transform.cpp:47
glm::detail::tvec2
Definition: type_mat.hpp:36
tango_gl::GestureCamera::~GestureCamera
~GestureCamera()
Definition: gesture_camera.cpp:64
glm::detail::tquat::x
T x
Definition: gtc/quaternion.hpp:65
tango_gl::Transform::SetParent
void SetParent(Transform *transform)
Definition: transform.cpp:80
tango_gl::GestureCamera::cam_parent_transform_
Transform * cam_parent_transform_
Definition: gesture_camera.h:75
tango_gl::GestureCamera::OnTouchEvent
void OnTouchEvent(int touch_count, TouchEvent event, float x0, float y0, float x1, float y1)
Definition: gesture_camera.cpp:66


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jul 25 2024 02:50:10