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: {
79  glm::vec2 offset;
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);
130  glm::vec2 offset;
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
void SetNearFarClipPlanes(const float near, const float far)
Definition: camera.cpp:56
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::quat GetRotation() const
Definition: transform.cpp:47
highp_quat quat
Quaternion of default single-precision floating-point numbers.
Definition: fwd.hpp:68
Transform * cam_parent_transform_
glm::mat4 GetTransformationMatrix() const
Definition: transform.cpp:67
Segment GetSegmentFromTouch(float normalized_x, float normalized_y, float touch_range)
glm::vec3 GetPosition() const
Definition: transform.cpp:39
GLM_FUNC_DECL genType normalize(genType const &x)
void SetFieldOfView(const float fov)
Definition: camera.cpp:52
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_
dist
float aspect_ratio_
Definition: camera.h:60
void SetPosition(const glm::vec3 &position)
Definition: transform.cpp:35
end
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 Jan 23 2023 03:37:28