| 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" |
| 14 #include "chrome/browser/android/vr_shell/elbow_model.h" |
| 13 #include "device/vr/vr_math.h" | 15 #include "device/vr/vr_math.h" |
| 14 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/
gvr.h" | 16 #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" | 17 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/
gvr_controller.h" |
| 16 | 18 |
| 17 namespace vr_shell { | 19 namespace vr_shell { |
| 18 | 20 |
| 19 namespace { | 21 namespace { |
| 20 | 22 |
| 21 constexpr float kDisplacementScaleFactor = 300.0f; | 23 constexpr float kDisplacementScaleFactor = 300.0f; |
| 22 | 24 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 34 // not equal. Also, minimum time distance needed to call two timestamps | 36 // not equal. Also, minimum time distance needed to call two timestamps |
| 35 // not equal. | 37 // not equal. |
| 36 constexpr float kDelta = 1.0e-7f; | 38 constexpr float kDelta = 1.0e-7f; |
| 37 | 39 |
| 38 constexpr float kCutoffHz = 10.0f; | 40 constexpr float kCutoffHz = 10.0f; |
| 39 constexpr float kRC = static_cast<float>(1.0 / (2.0 * M_PI * kCutoffHz)); | 41 constexpr float kRC = static_cast<float>(1.0 / (2.0 * M_PI * kCutoffHz)); |
| 40 constexpr float kNanoSecondsPerSecond = 1.0e9f; | 42 constexpr float kNanoSecondsPerSecond = 1.0e9f; |
| 41 | 43 |
| 42 constexpr int kMaxNumOfExtrapolations = 2; | 44 constexpr int kMaxNumOfExtrapolations = 2; |
| 43 | 45 |
| 44 static constexpr gfx::Point3F kControllerPosition = {0.2f, -0.5f, -0.15f}; | 46 // Distance from the center of the controller to start rendering the laser. |
| 47 constexpr float kLaserStartDisplacement = 0.045; |
| 45 | 48 |
| 46 void ClampTouchpadPosition(gfx::Vector2dF* position) { | 49 void ClampTouchpadPosition(gfx::Vector2dF* position) { |
| 47 position->set_x(std::min(std::max(0.0f, position->x()), 1.0f)); | 50 position->set_x(vr::Clampf(position->x(), 0.0f, 1.0f)); |
| 48 position->set_y(std::min(std::max(0.0f, position->y()), 1.0f)); | 51 position->set_y(vr::Clampf(position->y(), 0.0f, 1.0f)); |
| 52 } |
| 53 |
| 54 float DeltaTimeSeconds(int64_t last_timestamp_nanos) { |
| 55 return (gvr::GvrApi::GetTimePointNow().monotonic_system_time_nanos - |
| 56 last_timestamp_nanos) / |
| 57 kNanoSecondsPerSecond; |
| 49 } | 58 } |
| 50 | 59 |
| 51 } // namespace | 60 } // namespace |
| 52 | 61 |
| 53 VrController::VrController(gvr_context* vr_context) { | 62 VrController::VrController(gvr_context* vr_context) { |
| 54 DVLOG(1) << __FUNCTION__ << "=" << this; | 63 DVLOG(1) << __FUNCTION__ << "=" << this; |
| 55 Initialize(vr_context); | 64 Initialize(vr_context); |
| 65 elbow_model_ = base::MakeUnique<ElbowModel>(handedness_); |
| 56 Reset(); | 66 Reset(); |
| 67 last_timestamp_nanos_ = |
| 68 gvr::GvrApi::GetTimePointNow().monotonic_system_time_nanos; |
| 57 } | 69 } |
| 58 | 70 |
| 59 VrController::~VrController() { | 71 VrController::~VrController() { |
| 60 DVLOG(1) << __FUNCTION__ << "=" << this; | 72 DVLOG(1) << __FUNCTION__ << "=" << this; |
| 61 } | 73 } |
| 62 | 74 |
| 63 void VrController::OnResume() { | 75 void VrController::OnResume() { |
| 64 if (controller_api_) | 76 if (controller_api_) |
| 65 controller_api_->Resume(); | 77 controller_api_->Resume(); |
| 66 } | 78 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 float VrController::TouchPosY() { | 119 float VrController::TouchPosY() { |
| 108 return controller_state_->GetTouchPos().y; | 120 return controller_state_->GetTouchPos().y; |
| 109 } | 121 } |
| 110 | 122 |
| 111 vr::Quatf VrController::Orientation() const { | 123 vr::Quatf VrController::Orientation() const { |
| 112 const gvr::Quatf& orientation = controller_state_->GetOrientation(); | 124 const gvr::Quatf& orientation = controller_state_->GetOrientation(); |
| 113 return *reinterpret_cast<vr::Quatf*>(const_cast<gvr::Quatf*>(&orientation)); | 125 return *reinterpret_cast<vr::Quatf*>(const_cast<gvr::Quatf*>(&orientation)); |
| 114 } | 126 } |
| 115 | 127 |
| 116 void VrController::GetTransform(vr::Mat4f* out) const { | 128 void VrController::GetTransform(vr::Mat4f* out) const { |
| 117 // TODO(acondor): Position and orientation needs to be obtained | 129 QuatToMatrix(elbow_model_->GetControllerRotation(), out); |
| 118 // from an elbow model. | 130 auto position = elbow_model_->GetControllerPosition(); |
| 119 // Placing the controller in a fixed position for now. | 131 vr::TranslateM(*out, vr::ToVector(position), out); |
| 120 vr::SetIdentityM(out); | 132 } |
| 121 // Changing rotation point. | 133 |
| 122 vr::TranslateM(*out, gfx::Vector3dF(0, 0, 0.05), out); | 134 float VrController::GetOpacity() const { |
| 123 vr::Mat4f quat_to_matrix; | 135 return elbow_model_->GetAlphaValue(); |
| 124 vr::QuatToMatrix(Orientation(), &quat_to_matrix); | 136 } |
| 125 vr::MatrixMul(quat_to_matrix, *out, out); | 137 |
| 126 gfx::Vector3dF translation(kControllerPosition.x(), kControllerPosition.y(), | 138 gfx::Point3F VrController::GetPointerStart() const { |
| 127 kControllerPosition.z() - 0.05); | 139 auto controller_position = elbow_model_->GetControllerPosition(); |
| 128 vr::TranslateM(*out, translation, out); | 140 gfx::Vector3dF pointer_direction{0.0f, -sin(kErgoAngleOffset), |
| 141 -cos(kErgoAngleOffset)}; |
| 142 vr::Mat4f rotation_mat; |
| 143 vr::QuatToMatrix(Orientation(), &rotation_mat); |
| 144 pointer_direction = vr::MatrixVectorRotate(rotation_mat, pointer_direction); |
| 145 return controller_position + |
| 146 gfx::ScaleVector3d(pointer_direction, kLaserStartDisplacement); |
| 129 } | 147 } |
| 130 | 148 |
| 131 VrControllerModel::State VrController::GetModelState() const { | 149 VrControllerModel::State VrController::GetModelState() const { |
| 132 if (ButtonState(gvr::ControllerButton::GVR_CONTROLLER_BUTTON_CLICK)) | 150 if (ButtonState(gvr::ControllerButton::GVR_CONTROLLER_BUTTON_CLICK)) |
| 133 return VrControllerModel::TOUCHPAD; | 151 return VrControllerModel::TOUCHPAD; |
| 134 if (ButtonState(gvr::ControllerButton::GVR_CONTROLLER_BUTTON_APP)) | 152 if (ButtonState(gvr::ControllerButton::GVR_CONTROLLER_BUTTON_APP)) |
| 135 return VrControllerModel::APP; | 153 return VrControllerModel::APP; |
| 136 if (ButtonState(gvr::ControllerButton::GVR_CONTROLLER_BUTTON_HOME)) | 154 if (ButtonState(gvr::ControllerButton::GVR_CONTROLLER_BUTTON_HOME)) |
| 137 return VrControllerModel::SYSTEM; | 155 return VrControllerModel::SYSTEM; |
| 138 return VrControllerModel::IDLE; | 156 return VrControllerModel::IDLE; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 155 } | 173 } |
| 156 | 174 |
| 157 bool VrController::ButtonState(gvr::ControllerButton button) const { | 175 bool VrController::ButtonState(gvr::ControllerButton button) const { |
| 158 return controller_state_->GetButtonState(button); | 176 return controller_state_->GetButtonState(button); |
| 159 } | 177 } |
| 160 | 178 |
| 161 bool VrController::IsConnected() { | 179 bool VrController::IsConnected() { |
| 162 return controller_state_->GetConnectionState() == gvr::kControllerConnected; | 180 return controller_state_->GetConnectionState() == gvr::kControllerConnected; |
| 163 } | 181 } |
| 164 | 182 |
| 165 void VrController::UpdateState() { | 183 void VrController::UpdateState(const gfx::Vector3dF& head_direction) { |
| 166 const int32_t old_status = controller_state_->GetApiStatus(); | 184 const int32_t old_status = controller_state_->GetApiStatus(); |
| 167 const int32_t old_connection_state = controller_state_->GetConnectionState(); | 185 const int32_t old_connection_state = controller_state_->GetConnectionState(); |
| 168 // Read current controller state. | 186 // Read current controller state. |
| 169 controller_state_->Update(*controller_api_); | 187 controller_state_->Update(*controller_api_); |
| 170 // Print new API status and connection state, if they changed. | 188 // Print new API status and connection state, if they changed. |
| 171 if (controller_state_->GetApiStatus() != old_status || | 189 if (controller_state_->GetApiStatus() != old_status || |
| 172 controller_state_->GetConnectionState() != old_connection_state) { | 190 controller_state_->GetConnectionState() != old_connection_state) { |
| 173 VLOG(1) << "Controller Connection status: " | 191 VLOG(1) << "Controller Connection status: " |
| 174 << gvr_controller_connection_state_to_string( | 192 << gvr_controller_connection_state_to_string( |
| 175 controller_state_->GetConnectionState()); | 193 controller_state_->GetConnectionState()); |
| 176 } | 194 } |
| 195 |
| 196 const gvr::Vec3f& gvr_gyro = controller_state_->GetGyro(); |
| 197 elbow_model_->Update({IsConnected(), Orientation(), |
| 198 gfx::Vector3dF(gvr_gyro.x, gvr_gyro.y, gvr_gyro.z), |
| 199 head_direction, |
| 200 DeltaTimeSeconds(last_timestamp_nanos_)}); |
| 177 } | 201 } |
| 178 | 202 |
| 179 void VrController::UpdateTouchInfo() { | 203 void VrController::UpdateTouchInfo() { |
| 180 CHECK(touch_info_ != nullptr) << "touch_info_ not initialized properly."; | 204 CHECK(touch_info_ != nullptr) << "touch_info_ not initialized properly."; |
| 181 if (IsTouching() && state_ == SCROLLING && | 205 if (IsTouching() && state_ == SCROLLING && |
| 182 (controller_state_->GetLastTouchTimestamp() == last_touch_timestamp_ || | 206 (controller_state_->GetLastTouchTimestamp() == last_touch_timestamp_ || |
| 183 (cur_touch_point_->position == prev_touch_point_->position && | 207 (cur_touch_point_->position == prev_touch_point_->position && |
| 184 extrapolated_touch_ < kMaxNumOfExtrapolations))) { | 208 extrapolated_touch_ < kMaxNumOfExtrapolations))) { |
| 185 extrapolated_touch_++; | 209 extrapolated_touch_++; |
| 186 touch_position_changed_ = true; | 210 touch_position_changed_ = true; |
| 187 // Fill the touch_info | 211 // Fill the touch_info |
| 188 float duration = | 212 float duration = DeltaTimeSeconds(last_timestamp_nanos_); |
| 189 (gvr::GvrApi::GetTimePointNow().monotonic_system_time_nanos - | |
| 190 last_timestamp_nanos_) / | |
| 191 kNanoSecondsPerSecond; | |
| 192 touch_info_->touch_point.position.set_x(cur_touch_point_->position.x() + | 213 touch_info_->touch_point.position.set_x(cur_touch_point_->position.x() + |
| 193 overall_velocity_.x() * duration); | 214 overall_velocity_.x() * duration); |
| 194 touch_info_->touch_point.position.set_y(cur_touch_point_->position.y() + | 215 touch_info_->touch_point.position.set_y(cur_touch_point_->position.y() + |
| 195 overall_velocity_.y() * duration); | 216 overall_velocity_.y() * duration); |
| 196 } else { | 217 } else { |
| 197 if (extrapolated_touch_ == kMaxNumOfExtrapolations) { | 218 if (extrapolated_touch_ == kMaxNumOfExtrapolations) { |
| 198 overall_velocity_ = {0, 0}; | 219 overall_velocity_ = {0, 0}; |
| 199 } | 220 } |
| 200 extrapolated_touch_ = 0; | 221 extrapolated_touch_ = 0; |
| 201 } | 222 } |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 420 | 441 |
| 421 const gfx::Vector2dF& velocity = ScaleVector2d(displacement, (1 / duration)); | 442 const gfx::Vector2dF& velocity = ScaleVector2d(displacement, (1 / duration)); |
| 422 | 443 |
| 423 float weight = duration / (kRC + duration); | 444 float weight = duration / (kRC + duration); |
| 424 | 445 |
| 425 overall_velocity_ = ScaleVector2d(overall_velocity_, (1 - weight)) + | 446 overall_velocity_ = ScaleVector2d(overall_velocity_, (1 - weight)) + |
| 426 ScaleVector2d(velocity, weight); | 447 ScaleVector2d(velocity, weight); |
| 427 } | 448 } |
| 428 | 449 |
| 429 } // namespace vr_shell | 450 } // namespace vr_shell |
| OLD | NEW |