OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/android/vr_shell/vr_controller.h" | 5 #include "chrome/browser/android/vr_shell/vr_controller.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/ptr_util.h" | |
12 #include "base/time/time.h" | 13 #include "base/time/time.h" |
13 #include "chrome/browser/android/vr_shell/vr_math.h" | 14 #include "chrome/browser/android/vr_shell/vr_math.h" |
14 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/ gvr.h" | 15 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/ gvr.h" |
15 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/ gvr_controller.h" | 16 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/ gvr_controller.h" |
16 | 17 |
17 namespace vr_shell { | 18 namespace vr_shell { |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 constexpr float kDisplacementScaleFactor = 300.0f; | 22 constexpr float kDisplacementScaleFactor = 300.0f; |
(...skipping 12 matching lines...) Expand all Loading... | |
34 // not equal. Also, minimum time distance needed to call two timestamps | 35 // not equal. Also, minimum time distance needed to call two timestamps |
35 // not equal. | 36 // not equal. |
36 constexpr float kDelta = 1.0e-7f; | 37 constexpr float kDelta = 1.0e-7f; |
37 | 38 |
38 constexpr float kCutoffHz = 10.0f; | 39 constexpr float kCutoffHz = 10.0f; |
39 constexpr float kRC = static_cast<float>(1.0 / (2.0 * M_PI * kCutoffHz)); | 40 constexpr float kRC = static_cast<float>(1.0 / (2.0 * M_PI * kCutoffHz)); |
40 constexpr float kNanoSecondsPerSecond = 1.0e9f; | 41 constexpr float kNanoSecondsPerSecond = 1.0e9f; |
41 | 42 |
42 constexpr int kMaxNumOfExtrapolations = 2; | 43 constexpr int kMaxNumOfExtrapolations = 2; |
43 | 44 |
44 static constexpr gvr::Vec3f kControllerPosition = {0.2f, -0.5f, -0.15f}; | |
45 | |
46 class Vector { | 45 class Vector { |
47 public: | 46 public: |
48 static inline void ClampTouchpadPosition(gvr::Vec2f* position) { | 47 static inline void ClampTouchpadPosition(gvr::Vec2f* position) { |
49 position->x = std::min(std::max(0.0f, position->x), 1.0f); | 48 position->x = std::min(std::max(0.0f, position->x), 1.0f); |
50 position->y = std::min(std::max(0.0f, position->y), 1.0f); | 49 position->y = std::min(std::max(0.0f, position->y), 1.0f); |
51 } | 50 } |
52 | 51 |
53 static inline void SetZero(gvr::Vec2f* v) { | 52 static inline void SetZero(gvr::Vec2f* v) { |
54 v->x = 0; | 53 v->x = 0; |
55 v->y = 0; | 54 v->y = 0; |
(...skipping 24 matching lines...) Expand all Loading... | |
80 return vect_prod; | 79 return vect_prod; |
81 } | 80 } |
82 | 81 |
83 }; // Vector | 82 }; // Vector |
84 | 83 |
85 } // namespace | 84 } // namespace |
86 | 85 |
87 VrController::VrController(gvr_context* vr_context) { | 86 VrController::VrController(gvr_context* vr_context) { |
88 DVLOG(1) << __FUNCTION__ << "=" << this; | 87 DVLOG(1) << __FUNCTION__ << "=" << this; |
89 Initialize(vr_context); | 88 Initialize(vr_context); |
89 elbow_model_ = base::MakeUnique<ElbowModel>(handedness_); | |
90 Reset(); | 90 Reset(); |
91 last_timestamp_nanos_ = | |
92 gvr::GvrApi::GetTimePointNow().monotonic_system_time_nanos; | |
91 } | 93 } |
92 | 94 |
93 VrController::~VrController() { | 95 VrController::~VrController() { |
94 DVLOG(1) << __FUNCTION__ << "=" << this; | 96 DVLOG(1) << __FUNCTION__ << "=" << this; |
95 } | 97 } |
96 | 98 |
97 void VrController::OnResume() { | 99 void VrController::OnResume() { |
98 if (controller_api_) | 100 if (controller_api_) |
99 controller_api_->Resume(); | 101 controller_api_->Resume(); |
100 } | 102 } |
(...skipping 29 matching lines...) Expand all Loading... | |
130 | 132 |
131 float VrController::TouchPosY() { | 133 float VrController::TouchPosY() { |
132 return controller_state_->GetTouchPos().y; | 134 return controller_state_->GetTouchPos().y; |
133 } | 135 } |
134 | 136 |
135 gvr::Quatf VrController::Orientation() const { | 137 gvr::Quatf VrController::Orientation() const { |
136 return controller_state_->GetOrientation(); | 138 return controller_state_->GetOrientation(); |
137 } | 139 } |
138 | 140 |
139 gvr::Mat4f VrController::GetTransform() const { | 141 gvr::Mat4f VrController::GetTransform() const { |
140 // TODO(acondor): Position and orientation needs to be obtained | 142 auto mat = QuatToMatrix(elbow_model_->GetControllerRotation()); |
141 // from an elbow model. | 143 auto position = elbow_model_->GetControllerPosition(); |
142 // Placing the controller in a fixed position for now. | 144 TranslateM(mat, mat, position.x, position.y, position.z); |
143 gvr::Mat4f mat; | |
144 SetIdentityM(mat); | |
145 // Changing rotation point. | |
146 TranslateM(mat, mat, 0, 0, 0.05); | |
147 mat = MatrixMul(QuatToMatrix(Orientation()), mat); | |
148 TranslateM(mat, mat, kControllerPosition.x, kControllerPosition.y, | |
149 kControllerPosition.z - 0.05); | |
150 return mat; | 145 return mat; |
151 } | 146 } |
152 | 147 |
148 float VrController::GetOpacity() const { | |
149 return elbow_model_->GetAlphaValue(); | |
150 } | |
151 | |
152 gvr::Vec3f VrController::GetPointerStart() const { | |
153 return elbow_model_->GetControllerPosition(); | |
154 } | |
155 | |
153 VrControllerModel::State VrController::GetModelState() const { | 156 VrControllerModel::State VrController::GetModelState() const { |
154 if (ButtonState(gvr::ControllerButton::GVR_CONTROLLER_BUTTON_CLICK)) | 157 if (ButtonState(gvr::ControllerButton::GVR_CONTROLLER_BUTTON_CLICK)) |
155 return VrControllerModel::TOUCHPAD; | 158 return VrControllerModel::TOUCHPAD; |
156 if (ButtonState(gvr::ControllerButton::GVR_CONTROLLER_BUTTON_APP)) | 159 if (ButtonState(gvr::ControllerButton::GVR_CONTROLLER_BUTTON_APP)) |
157 return VrControllerModel::APP; | 160 return VrControllerModel::APP; |
158 if (ButtonState(gvr::ControllerButton::GVR_CONTROLLER_BUTTON_HOME)) | 161 if (ButtonState(gvr::ControllerButton::GVR_CONTROLLER_BUTTON_HOME)) |
159 return VrControllerModel::SYSTEM; | 162 return VrControllerModel::SYSTEM; |
160 return VrControllerModel::IDLE; | 163 return VrControllerModel::IDLE; |
161 } | 164 } |
162 | 165 |
(...skipping 14 matching lines...) Expand all Loading... | |
177 } | 180 } |
178 | 181 |
179 bool VrController::ButtonState(gvr::ControllerButton button) const { | 182 bool VrController::ButtonState(gvr::ControllerButton button) const { |
180 return controller_state_->GetButtonState(button); | 183 return controller_state_->GetButtonState(button); |
181 } | 184 } |
182 | 185 |
183 bool VrController::IsConnected() { | 186 bool VrController::IsConnected() { |
184 return controller_state_->GetConnectionState() == gvr::kControllerConnected; | 187 return controller_state_->GetConnectionState() == gvr::kControllerConnected; |
185 } | 188 } |
186 | 189 |
187 void VrController::UpdateState() { | 190 void VrController::UpdateState(const gvr::Vec3f& head_direction) { |
188 const int32_t old_status = controller_state_->GetApiStatus(); | 191 const int32_t old_status = controller_state_->GetApiStatus(); |
189 const int32_t old_connection_state = controller_state_->GetConnectionState(); | 192 const int32_t old_connection_state = controller_state_->GetConnectionState(); |
190 // Read current controller state. | 193 // Read current controller state. |
191 controller_state_->Update(*controller_api_); | 194 controller_state_->Update(*controller_api_); |
192 // Print new API status and connection state, if they changed. | 195 // Print new API status and connection state, if they changed. |
193 if (controller_state_->GetApiStatus() != old_status || | 196 if (controller_state_->GetApiStatus() != old_status || |
194 controller_state_->GetConnectionState() != old_connection_state) { | 197 controller_state_->GetConnectionState() != old_connection_state) { |
195 VLOG(1) << "Controller Connection status: " | 198 VLOG(1) << "Controller Connection status: " |
196 << gvr_controller_connection_state_to_string( | 199 << gvr_controller_connection_state_to_string( |
197 controller_state_->GetConnectionState()); | 200 controller_state_->GetConnectionState()); |
198 } | 201 } |
202 | |
203 float delta_time_seconds = | |
asimjour1
2017/04/19 18:58:12
this conversion is repeated a few times, could you
acondor_
2017/04/21 21:04:32
Done.
| |
204 (gvr::GvrApi::GetTimePointNow().monotonic_system_time_nanos - | |
205 last_timestamp_nanos_) / | |
206 kNanoSecondsPerSecond; | |
207 elbow_model_->Update({IsConnected(), Orientation(), | |
208 controller_state_->GetGyro(), head_direction, | |
209 delta_time_seconds}); | |
199 } | 210 } |
200 | 211 |
201 void VrController::UpdateTouchInfo() { | 212 void VrController::UpdateTouchInfo() { |
202 CHECK(touch_info_ != nullptr) << "touch_info_ not initialized properly."; | 213 CHECK(touch_info_ != nullptr) << "touch_info_ not initialized properly."; |
203 if (IsTouching() && state_ == SCROLLING && | 214 if (IsTouching() && state_ == SCROLLING && |
204 (controller_state_->GetLastTouchTimestamp() == last_touch_timestamp_ || | 215 (controller_state_->GetLastTouchTimestamp() == last_touch_timestamp_ || |
205 (Vector::Equal(cur_touch_point_->position, | 216 (Vector::Equal(cur_touch_point_->position, |
206 prev_touch_point_->position) && | 217 prev_touch_point_->position) && |
207 extrapolated_touch_ < kMaxNumOfExtrapolations))) { | 218 extrapolated_touch_ < kMaxNumOfExtrapolations))) { |
208 extrapolated_touch_++; | 219 extrapolated_touch_++; |
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
455 gvr::Vec2f velocity = Vector::ScalarMult(displacement, 1 / duration); | 466 gvr::Vec2f velocity = Vector::ScalarMult(displacement, 1 / duration); |
456 | 467 |
457 float weight = duration / (kRC + duration); | 468 float weight = duration / (kRC + duration); |
458 | 469 |
459 overall_velocity_ = | 470 overall_velocity_ = |
460 Vector::Add(Vector::ScalarMult(overall_velocity_, 1 - weight), | 471 Vector::Add(Vector::ScalarMult(overall_velocity_, 1 - weight), |
461 Vector::ScalarMult(velocity, weight)); | 472 Vector::ScalarMult(velocity, weight)); |
462 } | 473 } |
463 | 474 |
464 } // namespace vr_shell | 475 } // namespace vr_shell |
OLD | NEW |