OLD | NEW |
---|---|
(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) { | |
mdempsky
2015/05/11 19:25:01
Nit: Google C++ style guide disallows non-const re
bajones
2015/05/12 00:13:39
Ooh... that's a really good question. This was por
| |
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 } // namespace | |
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/24 21:46:07
Would it be worth having a ThreadChecker for this
bajones
2015/05/12 00:13:39
This class is only called from the VRService, whic
| |
64 j_cardboard_device_.Reset( | |
65 Java_CardboardVRDevice_create( | |
66 AttachCurrentThread(), | |
67 base::android::GetApplicationContext())); | |
68 } | |
69 | |
70 CardboardVRDevice::~CardboardVRDevice() { | |
71 Java_CardboardVRDevice_stopTracking(AttachCurrentThread(), | |
72 j_cardboard_device_.obj()); | |
73 } | |
74 | |
75 void CardboardVRDevice::GetVRDevice(blink::WebVRDevice* device) { | |
76 memset(device, 0, sizeof(blink::WebVRDevice)); | |
77 | |
78 device->flags = blink::WebVRDeviceTypePosition | blink::WebVRDeviceTypeHMD; | |
79 | |
80 JNIEnv* env = AttachCurrentThread(); | |
81 | |
82 { | |
83 ScopedJavaLocalRef<jstring> j_device_name; | |
84 j_device_name = Java_CardboardVRDevice_getDeviceName( | |
85 env, | |
86 j_cardboard_device_.obj()); | |
87 | |
88 base::string16 tmp16; | |
89 base::android::ConvertJavaStringToUTF16(env, j_device_name.obj(), &tmp16); | |
90 tmp16.copy(device->deviceName, blink::WebVRDevice::deviceNameLengthCap - 1); | |
91 } | |
92 | |
93 ScopedJavaLocalRef<jfloatArray> j_fov(env, env->NewFloatArray(4)); | |
94 Java_CardboardVRDevice_getFieldOfView(env, | |
95 j_cardboard_device_.obj(), | |
96 j_fov.obj()); | |
97 | |
98 std::vector<float> fov; | |
99 base::android::JavaFloatArrayToFloatVector(env, | |
100 j_fov.obj(), | |
101 &fov); | |
102 | |
103 blink::WebVRHMDInfo& hmdInfo = device->hmdInfo; | |
104 | |
105 hmdInfo.leftEye.recommendedFieldOfView.upDegrees = fov[0]; | |
106 hmdInfo.leftEye.recommendedFieldOfView.downDegrees = fov[1]; | |
107 hmdInfo.leftEye.recommendedFieldOfView.leftDegrees = fov[2]; | |
108 hmdInfo.leftEye.recommendedFieldOfView.rightDegrees = fov[3]; | |
109 | |
110 // Cardboard devices always assume a mirrored FOV, so this is just the left | |
111 // eye FOV with the left and right degrees swapped. | |
112 hmdInfo.rightEye.recommendedFieldOfView.upDegrees = fov[0]; | |
113 hmdInfo.rightEye.recommendedFieldOfView.downDegrees = fov[1]; | |
114 hmdInfo.rightEye.recommendedFieldOfView.leftDegrees = fov[3]; | |
115 hmdInfo.rightEye.recommendedFieldOfView.rightDegrees = fov[2]; | |
116 | |
117 // Cardboard does not support configurable FOV. | |
118 hmdInfo.leftEye.maximumFieldOfView = hmdInfo.leftEye.recommendedFieldOfView; | |
119 hmdInfo.rightEye.maximumFieldOfView = hmdInfo.rightEye.recommendedFieldOfView; | |
120 hmdInfo.leftEye.minimumFieldOfView = hmdInfo.leftEye.recommendedFieldOfView; | |
121 hmdInfo.rightEye.minimumFieldOfView = hmdInfo.rightEye.recommendedFieldOfView; | |
122 | |
123 float ipd = Java_CardboardVRDevice_getIpd(env, j_cardboard_device_.obj()); | |
124 | |
125 hmdInfo.leftEye.eyeTranslation.x = ipd * -0.5f; | |
126 hmdInfo.leftEye.eyeTranslation.y = 0.0f; | |
127 hmdInfo.leftEye.eyeTranslation.z = 0.0f; | |
128 | |
129 hmdInfo.rightEye.eyeTranslation.x = ipd * 0.5f; | |
130 hmdInfo.rightEye.eyeTranslation.y = 0.0f; | |
131 hmdInfo.rightEye.eyeTranslation.z = 0.0f; | |
132 | |
133 ScopedJavaLocalRef<jintArray> j_screen_size(env, env->NewIntArray(2)); | |
134 Java_CardboardVRDevice_getScreenSize(env, | |
135 j_cardboard_device_.obj(), | |
136 j_screen_size.obj()); | |
137 | |
138 std::vector<int> screen_size; | |
139 base::android::JavaIntArrayToIntVector(env, | |
140 j_screen_size.obj(), | |
141 &screen_size); | |
142 | |
143 hmdInfo.leftEye.renderRect.x = 0; | |
144 hmdInfo.leftEye.renderRect.y = 0; | |
145 hmdInfo.leftEye.renderRect.width = screen_size[0] / 2.0; | |
146 hmdInfo.leftEye.renderRect.height = screen_size[1]; | |
147 | |
148 hmdInfo.rightEye.renderRect.x = screen_size[0] / 2.0; | |
149 hmdInfo.rightEye.renderRect.y = 0; | |
150 hmdInfo.rightEye.renderRect.width = screen_size[0] / 2.0; | |
151 hmdInfo.rightEye.renderRect.height = screen_size[1]; | |
152 } | |
153 | |
154 void CardboardVRDevice::GetOrientationPosition( | |
155 blink::WebVRVector4& orientation, | |
mdempsky
2015/05/11 19:25:01
Nit: Non-const reference parameters.
| |
156 blink::WebVRVector3& position) { | |
157 JNIEnv* env = AttachCurrentThread(); | |
158 ScopedJavaLocalRef<jfloatArray> j_head_matrix(env, env->NewFloatArray(16)); | |
159 Java_CardboardVRDevice_getSensorState(env, | |
160 j_cardboard_device_.obj(), | |
161 j_head_matrix.obj()); | |
162 | |
163 std::vector<float> head_matrix; | |
164 base::android::JavaFloatArrayToFloatVector(env, | |
165 j_head_matrix.obj(), | |
166 &head_matrix); | |
167 | |
168 MatrixToOrientationQuat(&head_matrix[0], orientation); | |
169 | |
170 position.x = -head_matrix[12]; | |
171 position.y = head_matrix[13]; | |
172 position.z = head_matrix[14]; | |
173 } | |
174 | |
175 void CardboardVRDevice::GetSensorState(blink::WebHMDSensorState* state) { | |
176 memset(state, 0, sizeof(blink::WebHMDSensorState)); | |
177 | |
178 state->timestamp = frame_index_; | |
179 state->frameIndex = frame_index_; | |
180 state->flags = blink::WebVRSensorStateOrientation | | |
181 blink::WebVRSensorStatePosition; | |
182 | |
183 GetOrientationPosition(state->orientation, state->position); | |
184 | |
185 frame_index_++; | |
186 } | |
187 | |
188 void CardboardVRDevice::ResetSensor() { | |
189 Java_CardboardVRDevice_resetSensor(AttachCurrentThread(), | |
190 j_cardboard_device_.obj()); | |
191 } | |
192 | |
193 } // namespace content | |
OLD | NEW |