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

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: Updated Cardboard lib to v0.5.3 Created 5 years, 8 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 void MatrixToOrientationQuat(const float m[16], blink::WebVRVector4& out) {
26 float fTrace = m[0] + m[5] + m[10];
27 float fRoot;
28 if ( fTrace > 0.0f ) {
29 fRoot = sqrtf(1.0f + fTrace) * 2.0f;
30 out.x = (m[9] - m[6]) / fRoot;
31 out.y = (m[2] - m[8]) / fRoot;
32 out.z = (m[4] - m[1]) / fRoot;
33 out.w = 0.25f * fRoot;
34 } else if ((m[0] > m[5]) && (m[0] > m[10])) {
35 fRoot = sqrtf(1.0f + m[0] - m[5] - m[10]) * 2.0f;
36 out.x = 0.25f * fRoot;
37 out.y = (m[1] + m[4]) / fRoot;
38 out.z = (m[2] + m[8]) / fRoot;
39 out.w = (m[9] - m[6]) / fRoot;
40 } else if (m[5] > m[10]) {
41 fRoot = sqrtf(1.0f + m[5] - m[0] - m[10]) * 2.0f;
42 out.x = (m[1] + m[4]) / fRoot;
43 out.y = 0.25f * fRoot;
44 out.z = (m[6] + m[9]) / fRoot;
45 out.w = (m[2] - m[8]) / fRoot;
46 } else {
47 fRoot = sqrtf(1.0f + m[10] - m[0] - m[5]) * 2.0f;
48 out.x = (m[2] + m[8]) / fRoot;
49 out.y = (m[6] + m[9]) / fRoot;
50 out.z = 0.25f * fRoot;
51 out.w = (m[4] - m[1]) / fRoot;
52 }
53 }
54
55 }
56
57 bool CardboardVRDevice::RegisterCardboardVRDevice(JNIEnv* env) {
58 return RegisterNativesImpl(env);
59 }
60
61 CardboardVRDevice::CardboardVRDevice(VRDeviceProvider* provider)
62 : VRDevice(provider)
63 , frame_index_(0)
jdduke (slow) 2015/04/16 22:24:50 Nit: This looks like Blink constructor formatting,
64 {
65 j_cardboard_device_.Reset(
66 Java_CardboardVRDevice_create(
67 AttachCurrentThread(),
68 base::android::GetApplicationContext(),
69 reinterpret_cast<intptr_t>(this)));
70 }
71
72 CardboardVRDevice::~CardboardVRDevice() {
73 Java_CardboardVRDevice_stopTracking(AttachCurrentThread(),
74 j_cardboard_device_.obj());
75 }
76
77 void CardboardVRDevice::GetVRDevice(blink::WebVRDevice* device) {
78 memset(device, 0, sizeof(blink::WebVRDevice));
79
80 device->flags = blink::WebVRDeviceTypePosition | blink::WebVRDeviceTypeHMD;
81
82 JNIEnv* env = AttachCurrentThread();
83
84 {
85 ScopedJavaLocalRef<jstring> j_device_name;
86 j_device_name = Java_CardboardVRDevice_getDeviceName(
87 env,
88 j_cardboard_device_.obj());
89
90 base::string16 tmp16;
91 base::android::ConvertJavaStringToUTF16(env, j_device_name.obj(), &tmp16);
92 tmp16.copy(device->deviceName, blink::WebVRDevice::deviceNameLengthCap - 1);
93 }
94
95 ScopedJavaLocalRef<jfloatArray> j_fov(env, env->NewFloatArray(4));
96 Java_CardboardVRDevice_getFieldOfView(env,
97 j_cardboard_device_.obj(),
98 j_fov.obj());
99
100 std::vector<float> fov;
101 base::android::JavaFloatArrayToFloatVector(env,
102 j_fov.obj(),
103 &fov);
104
105 blink::WebVRHMDInfo& hmdInfo = device->hmdInfo;
106
107 hmdInfo.leftEye.recommendedFieldOfView.upDegrees = fov[0];
108 hmdInfo.leftEye.recommendedFieldOfView.downDegrees = fov[1];
109 hmdInfo.leftEye.recommendedFieldOfView.leftDegrees = fov[2];
110 hmdInfo.leftEye.recommendedFieldOfView.rightDegrees = fov[3];
111
112 // Cardboard devices always assume a mirrored FOV, so this is just the left
113 // eye FOV with the left and right degrees swapped.
114 hmdInfo.rightEye.recommendedFieldOfView.upDegrees = fov[0];
115 hmdInfo.rightEye.recommendedFieldOfView.downDegrees = fov[1];
116 hmdInfo.rightEye.recommendedFieldOfView.leftDegrees = fov[3];
117 hmdInfo.rightEye.recommendedFieldOfView.rightDegrees = fov[2];
118
119 // Cardboard does not support configurable FOV.
120 hmdInfo.leftEye.maximumFieldOfView = hmdInfo.leftEye.recommendedFieldOfView;
121 hmdInfo.rightEye.maximumFieldOfView = hmdInfo.rightEye.recommendedFieldOfView;
122 hmdInfo.leftEye.minimumFieldOfView = hmdInfo.leftEye.recommendedFieldOfView;
123 hmdInfo.rightEye.minimumFieldOfView = hmdInfo.rightEye.recommendedFieldOfView;
124
125 float ipd = Java_CardboardVRDevice_getIpd(env, j_cardboard_device_.obj());
126
127 hmdInfo.leftEye.eyeTranslation.x = ipd * -0.5f;
128 hmdInfo.leftEye.eyeTranslation.y = 0.0f;
129 hmdInfo.leftEye.eyeTranslation.z = 0.0f;
130
131 hmdInfo.rightEye.eyeTranslation.x = ipd * 0.5f;
132 hmdInfo.rightEye.eyeTranslation.y = 0.0f;
133 hmdInfo.rightEye.eyeTranslation.z = 0.0f;
134
135 ScopedJavaLocalRef<jintArray> j_screen_size(env, env->NewIntArray(2));
136 Java_CardboardVRDevice_getScreenSize(env,
137 j_cardboard_device_.obj(),
138 j_screen_size.obj());
139
140 std::vector<int> screen_size;
141 base::android::JavaIntArrayToIntVector(env,
142 j_screen_size.obj(),
143 &screen_size);
144
145 hmdInfo.leftEye.renderRect.x = 0;
146 hmdInfo.leftEye.renderRect.y = 0;
147 hmdInfo.leftEye.renderRect.width = screen_size[0] / 2.0;
148 hmdInfo.leftEye.renderRect.height = screen_size[1];
149
150 hmdInfo.rightEye.renderRect.x = screen_size[0] / 2.0;
151 hmdInfo.rightEye.renderRect.y = 0;
152 hmdInfo.rightEye.renderRect.width = screen_size[0] / 2.0;
153 hmdInfo.rightEye.renderRect.height = screen_size[1];
154 }
155
156 void CardboardVRDevice::GetOrientationPosition(
157 blink::WebVRVector4& orientation,
158 blink::WebVRVector3& position) {
159 JNIEnv* env = AttachCurrentThread();
jdduke (slow) 2015/04/16 22:24:50 How requently is this called? It may not be worth
bajones 2015/04/17 00:38:42 It'll get called once per frame for most VR apps,
160 ScopedJavaLocalRef<jfloatArray> j_head_matrix(env, env->NewFloatArray(16));
161 Java_CardboardVRDevice_getSensorState(env,
162 j_cardboard_device_.obj(),
163 j_head_matrix.obj());
164
165 std::vector<float> head_matrix;
166 base::android::JavaFloatArrayToFloatVector(env,
167 j_head_matrix.obj(),
168 &head_matrix);
169
170 MatrixToOrientationQuat(&head_matrix[0], orientation);
171
172 position.x = -head_matrix[12];
173 position.y = head_matrix[13];
174 position.z = head_matrix[14];
175 }
176
177 void CardboardVRDevice::GetSensorState(blink::WebHMDSensorState* state) {
178 memset(state, 0, sizeof(blink::WebHMDSensorState));
179
180 state->timestamp = frame_index_;
181 state->frameIndex = frame_index_;
182 state->flags = blink::WebVRSensorStateOrientation |
183 blink::WebVRSensorStatePosition;
184
185 GetOrientationPosition(state->orientation, state->position);
186
187 frame_index_++;
188 }
189
190 void CardboardVRDevice::ResetSensor() {
191 Java_CardboardVRDevice_resetSensor(AttachCurrentThread(),
192 j_cardboard_device_.obj());
193 }
194
195 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698