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 45 degrees.
47 const float kHighFov = 65.0f;
48 const float kLowFov = 65.0f;
49 }
50 
51 namespace tango_gl {
52 
54  cam_cur_target_rot_(1,0,0,0),
55  start_touch_dist_(0.0f),
56  cur_touch_dist_(0.0f)
57 {
60 }
61 
63 
64 void GestureCamera::OnTouchEvent(int touch_count, TouchEvent event, float x0,
65  float y0, float x1, float y1) {
66 
67  if (camera_type_!=kFirstPerson && touch_count == 1) {
68  switch (event) {
69  case kTouch0Down: {
71 
74  break;
75  }
76  case kTouchMove: {
77  glm::vec2 offset;
78 
79  float rotation_x = (touch0_start_position_.y - y0) * kRotationSpeed;
80  float rotation_y = (touch0_start_position_.x - x0) * kRotationSpeed;
81 
83  cam_cur_angle_.x = cam_start_angle_.x + rotation_x;
84  cam_cur_angle_.y = cam_start_angle_.y + rotation_y;
85 
87 
88  break;
89  }
90  default: { break; }
91  }
92  }
93  if (touch_count == 2) {
94  switch (event) {
95  case kTouch1Down: {
96  float abs_x = x0 - x1;
97  float abs_y = y0 - y1;
98  start_touch_dist_ = std::sqrt(abs_x * abs_x + abs_y * abs_y);
100  cam_start_fov_ = this->getFOV();
101 
102  // center touch
103  touch0_start_position_.x = (x0+x1)/2.0f;
104  touch0_start_position_.y = (y0+y1)/2.0f;
105  break;
106  }
107  case kTouchMove: {
108  float abs_x = x0 - x1;
109  float abs_y = y0 - y1;
110  float dist = start_touch_dist_ - std::sqrt(abs_x * abs_x + abs_y * abs_y);
111 
113  {
114  this->SetFieldOfView(tango_gl::util::Clamp(cam_start_fov_ + dist * kZoomSpeed*10.0f, 45, 90));
115  }
116  else
117  {
119  kCamViewMinDist, kCamViewMaxDist);
120 
122  if(camera_type_ == kTopOrtho)
123  {
125  }
126 
127  glm::vec2 touch_center_position((x0+x1)/2.0f, (y0+y1)/2.0f);
128  glm::vec2 offset;
129  offset.x = (touch_center_position.x - touch0_start_position_.x) * kMoveSpeed;
130  offset.y = (touch_center_position.y - touch0_start_position_.y) * kMoveSpeed;
131  touch0_start_position_ = touch_center_position;
132 
134 
136  }
137  break;
138  }
139  default: { break; }
140  }
141  }
142 }
143 
145  float normalized_y,
146  float touch_range) {
147  float screen_height = touch_range * (2.0f * glm::tan(field_of_view_ * 0.5f));
148  float screen_width = screen_height * aspect_ratio_;
149  // normalized_x and normalized_x are from OnTouchEvent, top-left corner of the
150  // screen
151  // is [0, 0], transform it to opengl frame.
152  normalized_x = normalized_x - 0.5f;
153  normalized_y = 0.5f - normalized_y;
154  glm::vec3 start =
157  glm::vec3(normalized_x * screen_width, normalized_y * screen_height,
158  -touch_range));
159  Segment segment(start, end);
160  return segment;
161 }
162 
164  // Anchor position
166 
167  // Anchor rotation
169  {
175  }
176 }
177 
179  camera_type_ = camera_index;
180  switch (camera_index) {
181  case kFirstPerson:
182  SetOrthoMode(false);
183  SetFieldOfView(kLowFov);
184  SetPosition(glm::vec3(0.0f, 0.0f, 0.0f));
185  SetRotation(glm::quat(1.0f, 0.0f, 0.0f, 0.0f));
186  cam_cur_dist_ = 0.0f;
187  anchor_offset_ = glm::vec3(0.0f,0.0f,0.0f);
188  cam_cur_angle_.x = 0.0f;
189  cam_cur_angle_.y = 0.0f;
190  cam_cur_target_rot_ = glm::quat(1,0,0,0);
193  break;
194  case kThirdPerson:
195  case kThirdPersonFollow:
196  SetOrthoMode(false);
197  SetFieldOfView(kHighFov);
198  SetPosition(glm::vec3(0.0f, 0.0f, 0.0f));
199  SetRotation(glm::quat(1.0f, 0.0f, 0.0f, 0.0f));
200  cam_cur_dist_ = kThirdPersonFollow?kThirdPersonFollowCameraDist:kThirdPersonCameraDist;
201  anchor_offset_ = glm::vec3(0.0f,0.0f,0.0f);
202  cam_cur_angle_.x = -M_PI / 12.0f;
204  cam_cur_target_rot_ = glm::quat(1,0,0,0);
206  break;
207  case kTopDown:
208  SetPosition(glm::vec3(0.0f, 0.0f, 0.0f));
209  SetRotation(glm::quat(1.0f, 0.0f, 0.0f, 0.0f));
210  SetOrthoMode(false);
211  SetFieldOfView(kHighFov);
212  cam_cur_dist_ = kTopDownCameraDist;
213  anchor_offset_ = glm::vec3(0.0f,0.0f,0.0f);
214  cam_cur_angle_.x = -M_PI / 2.0f;
215  cam_cur_angle_.y = 0.0f;
216  cam_cur_target_rot_ = glm::quat(1,0,0,0);
218  break;
219  case kTopOrtho:
220  SetPosition(glm::vec3(0.0f, 0.0f, 0.0f));
221  SetRotation(glm::quat(1.0f, 0.0f, 0.0f, 0.0f));
222  SetOrthoMode(true);
223  SetOrthoScale(kTopDownCameraDist);
224  SetOrthoCropFactor(-1.0f);
225  cam_cur_dist_ = kTopDownCameraDist;
226  anchor_offset_ = glm::vec3(0.0f,0.0f,0.0f);
227  cam_cur_angle_.x = -M_PI / 2.0f;
228  cam_cur_angle_.y = 0.0f;
229  cam_cur_target_rot_ = glm::quat(1,0,0,0);
231  break;
232  default:
233  break;
234  }
235 }
236 
238 {
239  //Anchor rotation
240  glm::quat parent_cam_rot = glm::rotate(cam_cur_target_rot_, cam_cur_angle_.y, glm::vec3(0, 1, 0));
241  parent_cam_rot = glm::rotate(parent_cam_rot, cam_cur_angle_.x, glm::vec3(1, 0, 0));
242  cam_parent_transform_->SetRotation(parent_cam_rot);
243 
244  //Camera position
246 }
247 } // namespace tango_gl
glm::vec3 GetPosition() const
Definition: transform.cpp:39
highp_vec3 vec3
Definition: type_vec.hpp:392
void SetRotation(const glm::quat &rotation)
Definition: transform.cpp:43
f
void SetCameraType(CameraType camera_index)
float field_of_view_
Definition: camera.h:59
GLM_FUNC_DECL vecType< T, P > sqrt(vecType< T, P > const &x)
glm::mat4 GetTransformationMatrix() const
Definition: transform.cpp:67
highp_quat quat
Quaternion of default single-precision floating-point numbers.
Definition: fwd.hpp:68
Transform * cam_parent_transform_
Segment GetSegmentFromTouch(float normalized_x, float normalized_y, float touch_range)
GLM_FUNC_DECL genType normalize(genType const &x)
void SetFieldOfView(const float fov)
Definition: camera.cpp:52
glm::quat GetRotation() const
Definition: transform.cpp:47
void SetOrthoMode(bool enabled)
Definition: camera.h:32
glm::vec3 ApplyTransform(const glm::mat4 &mat, const glm::vec3 &vec)
Definition: util.cpp:237
float Clamp(float value, float min, float max)
Definition: util.cpp:165
void SetParent(Transform *transform)
Definition: transform.cpp:80
void OnTouchEvent(int touch_count, TouchEvent event, float x0, float y0, float x1, float y1)
GLM_FUNC_DECL detail::tquat< T, P > rotation(detail::tvec3< T, P > const &orig, detail::tvec3< T, P > const &dest)
GLM_FUNC_DECL genType tan(genType const &angle)
void SetAnchorPosition(const glm::vec3 &pos, const glm::quat &rotation)
void SetOrthoCropFactor(float value)
Definition: camera.h:34
glm::vec2 touch0_start_position_
float aspect_ratio_
Definition: camera.h:60
void SetPosition(const glm::vec3 &position)
Definition: transform.cpp:35
void SetOrthoScale(float scale)
Definition: camera.h:33
GLM_FUNC_DECL detail::tmat4x4< T, P > rotate(detail::tmat4x4< T, P > const &m, T const &angle, detail::tvec3< T, P > const &axis)


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Dec 14 2020 03:34:58