Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(310)

Side by Side Diff: content/browser/vr/cardboard/cardboard_vr_device.cc

Issue 829803003: Adding Chrome-side WebVR interface (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressing feedback from mdempsky Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/browser/vr/cardboard/cardboard_vr_device.h"
6
7 #include <math.h>
8 #include <algorithm>
9
10 #include "base/android/jni_android.h"
11 #include "base/android/jni_array.h"
12 #include "base/android/jni_string.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "jni/CardboardVRDevice_jni.h"
17 #include "ui/base/base_window.h"
18
19 using base::android::AttachCurrentThread;
20
21 namespace content {
22
23 namespace {
24
25 // Source: http://www.euclideanspace.com/maths/geometry/rotations/conversions/ma trixToQuaternion/
26 void MatrixToOrientationQuat(const float m[16], blink::WebVRVector4* out) {
27 float trace = m[0] + m[5] + m[10];
28 float root;
29 if (trace > 0.0f) {
30 root = sqrtf(1.0f + trace) * 2.0f;
31 out->x = (m[9] - m[6]) / root;
32 out->y = (m[2] - m[8]) / root;
33 out->z = (m[4] - m[1]) / root;
34 out->w = 0.25f * root;
35 } else if ((m[0] > m[5]) && (m[0] > m[10])) {
36 root = sqrtf(1.0f + m[0] - m[5] - m[10]) * 2.0f;
37 out->x = 0.25f * root;
38 out->y = (m[1] + m[4]) / root;
39 out->z = (m[2] + m[8]) / root;
40 out->w = (m[9] - m[6]) / root;
41 } else if (m[5] > m[10]) {
42 root = sqrtf(1.0f + m[5] - m[0] - m[10]) * 2.0f;
43 out->x = (m[1] + m[4]) / root;
44 out->y = 0.25f * root;
45 out->z = (m[6] + m[9]) / root;
46 out->w = (m[2] - m[8]) / root;
47 } else {
48 root = sqrtf(1.0f + m[10] - m[0] - m[5]) * 2.0f;
49 out->x = (m[2] + m[8]) / root;
50 out->y = (m[6] + m[9]) / root;
51 out->z = 0.25f * root;
52 out->w = (m[4] - m[1]) / root;
53 }
54 }
55
56 } // namespace
57
58 bool CardboardVRDevice::RegisterCardboardVRDevice(JNIEnv* env) {
59 return RegisterNativesImpl(env);
60 }
61
62 CardboardVRDevice::CardboardVRDevice(VRDeviceProvider* provider)
63 : VRDevice(provider), frame_index_(0) {
64 j_cardboard_device_.Reset(Java_CardboardVRDevice_create(
65 AttachCurrentThread(), base::android::GetApplicationContext()));
66 }
67
68 CardboardVRDevice::~CardboardVRDevice() {
69 Java_CardboardVRDevice_stopTracking(AttachCurrentThread(),
70 j_cardboard_device_.obj());
71 }
72
73 void CardboardVRDevice::GetVRDevice(blink::WebVRDevice* device) {
74 memset(device, 0, sizeof(blink::WebVRDevice));
75
76 device->flags = blink::WebVRDeviceTypePosition | blink::WebVRDeviceTypeHMD;
77
78 JNIEnv* env = AttachCurrentThread();
79
80 {
81 ScopedJavaLocalRef<jstring> j_device_name;
82 j_device_name =
83 Java_CardboardVRDevice_getDeviceName(env, j_cardboard_device_.obj());
84
85 base::string16 tmp16;
86 base::android::ConvertJavaStringToUTF16(env, j_device_name.obj(), &tmp16);
87 tmp16.copy(device->deviceName, blink::WebVRDevice::deviceNameLengthCap - 1);
88 }
89
90 ScopedJavaLocalRef<jfloatArray> j_fov(env, env->NewFloatArray(4));
91 Java_CardboardVRDevice_getFieldOfView(env, j_cardboard_device_.obj(),
92 j_fov.obj());
93
94 std::vector<float> fov;
95 base::android::JavaFloatArrayToFloatVector(env, j_fov.obj(), &fov);
96
97 blink::WebVRHMDInfo& hmdInfo = device->hmdInfo;
98
99 hmdInfo.leftEye.recommendedFieldOfView.upDegrees = fov[0];
100 hmdInfo.leftEye.recommendedFieldOfView.downDegrees = fov[1];
101 hmdInfo.leftEye.recommendedFieldOfView.leftDegrees = fov[2];
102 hmdInfo.leftEye.recommendedFieldOfView.rightDegrees = fov[3];
103
104 // Cardboard devices always assume a mirrored FOV, so this is just the left
105 // eye FOV with the left and right degrees swapped.
106 hmdInfo.rightEye.recommendedFieldOfView.upDegrees = fov[0];
107 hmdInfo.rightEye.recommendedFieldOfView.downDegrees = fov[1];
108 hmdInfo.rightEye.recommendedFieldOfView.leftDegrees = fov[3];
109 hmdInfo.rightEye.recommendedFieldOfView.rightDegrees = fov[2];
110
111 // Cardboard does not support configurable FOV.
112 hmdInfo.leftEye.maximumFieldOfView = hmdInfo.leftEye.recommendedFieldOfView;
113 hmdInfo.rightEye.maximumFieldOfView = hmdInfo.rightEye.recommendedFieldOfView;
114 hmdInfo.leftEye.minimumFieldOfView = hmdInfo.leftEye.recommendedFieldOfView;
115 hmdInfo.rightEye.minimumFieldOfView = hmdInfo.rightEye.recommendedFieldOfView;
116
117 float ipd = Java_CardboardVRDevice_getIpd(env, j_cardboard_device_.obj());
118
119 hmdInfo.leftEye.eyeTranslation.x = ipd * -0.5f;
120 hmdInfo.leftEye.eyeTranslation.y = 0.0f;
121 hmdInfo.leftEye.eyeTranslation.z = 0.0f;
122
123 hmdInfo.rightEye.eyeTranslation.x = ipd * 0.5f;
124 hmdInfo.rightEye.eyeTranslation.y = 0.0f;
125 hmdInfo.rightEye.eyeTranslation.z = 0.0f;
126
127 ScopedJavaLocalRef<jintArray> j_screen_size(env, env->NewIntArray(2));
128 Java_CardboardVRDevice_getScreenSize(env, j_cardboard_device_.obj(),
129 j_screen_size.obj());
130
131 std::vector<int> screen_size;
132 base::android::JavaIntArrayToIntVector(env, j_screen_size.obj(),
133 &screen_size);
134
135 hmdInfo.leftEye.renderRect.x = 0;
136 hmdInfo.leftEye.renderRect.y = 0;
137 hmdInfo.leftEye.renderRect.width = screen_size[0] / 2.0;
138 hmdInfo.leftEye.renderRect.height = screen_size[1];
139
140 hmdInfo.rightEye.renderRect.x = screen_size[0] / 2.0;
141 hmdInfo.rightEye.renderRect.y = 0;
142 hmdInfo.rightEye.renderRect.width = screen_size[0] / 2.0;
143 hmdInfo.rightEye.renderRect.height = screen_size[1];
144 }
145
146 void CardboardVRDevice::GetOrientationPosition(blink::WebVRVector4* orientation,
147 blink::WebVRVector3* position) {
148 JNIEnv* env = AttachCurrentThread();
149 ScopedJavaLocalRef<jfloatArray> j_head_matrix(env, env->NewFloatArray(16));
150 Java_CardboardVRDevice_getSensorState(env, j_cardboard_device_.obj(),
151 j_head_matrix.obj());
152
153 std::vector<float> head_matrix;
154 base::android::JavaFloatArrayToFloatVector(env, j_head_matrix.obj(),
155 &head_matrix);
156
157 MatrixToOrientationQuat(&head_matrix[0], orientation);
158
159 position->x = -head_matrix[12];
160 position->y = head_matrix[13];
161 position->z = head_matrix[14];
162 }
163
164 void CardboardVRDevice::GetSensorState(blink::WebHMDSensorState* state) {
165 memset(state, 0, sizeof(blink::WebHMDSensorState));
166
167 state->timestamp = frame_index_;
168 state->frameIndex = frame_index_;
169 state->flags =
170 blink::WebVRSensorStateOrientation | blink::WebVRSensorStatePosition;
171
172 GetOrientationPosition(&state->orientation, &state->position);
173
174 frame_index_++;
175 }
176
177 void CardboardVRDevice::ResetSensor() {
178 Java_CardboardVRDevice_resetSensor(AttachCurrentThread(),
179 j_cardboard_device_.obj());
180 }
181
182 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698