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/android/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/utf_string_conversions.h" | |
15 #include "base/time/time.h" | |
16 #include "jni/CardboardVRDevice_jni.h" | |
17 | |
18 using base::android::AttachCurrentThread; | |
19 | |
20 namespace content { | |
21 | |
22 namespace { | |
23 | |
24 // Source: | |
25 // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQu
aternion/ | |
26 VRVector4Ptr MatrixToOrientationQuat(const float m[16]) { | |
27 VRVector4Ptr out = VRVector4::New(); | |
28 float trace = m[0] + m[5] + m[10]; | |
29 float root; | |
30 if (trace > 0.0f) { | |
31 root = sqrtf(1.0f + trace) * 2.0f; | |
32 out->x = (m[9] - m[6]) / root; | |
33 out->y = (m[2] - m[8]) / root; | |
34 out->z = (m[4] - m[1]) / root; | |
35 out->w = 0.25f * root; | |
36 } else if ((m[0] > m[5]) && (m[0] > m[10])) { | |
37 root = sqrtf(1.0f + m[0] - m[5] - m[10]) * 2.0f; | |
38 out->x = 0.25f * root; | |
39 out->y = (m[1] + m[4]) / root; | |
40 out->z = (m[2] + m[8]) / root; | |
41 out->w = (m[9] - m[6]) / root; | |
42 } else if (m[5] > m[10]) { | |
43 root = sqrtf(1.0f + m[5] - m[0] - m[10]) * 2.0f; | |
44 out->x = (m[1] + m[4]) / root; | |
45 out->y = 0.25f * root; | |
46 out->z = (m[6] + m[9]) / root; | |
47 out->w = (m[2] - m[8]) / root; | |
48 } else { | |
49 root = sqrtf(1.0f + m[10] - m[0] - m[5]) * 2.0f; | |
50 out->x = (m[2] + m[8]) / root; | |
51 out->y = (m[6] + m[9]) / root; | |
52 out->z = 0.25f * root; | |
53 out->w = (m[4] - m[1]) / root; | |
54 } | |
55 | |
56 return out.Pass(); | |
57 } | |
58 | |
59 } // namespace | |
60 | |
61 bool CardboardVRDevice::RegisterCardboardVRDevice(JNIEnv* env) { | |
62 return RegisterNativesImpl(env); | |
63 } | |
64 | |
65 CardboardVRDevice::CardboardVRDevice(VRDeviceProvider* provider) | |
66 : VRDevice(provider), frame_index_(0) { | |
67 j_cardboard_device_.Reset(Java_CardboardVRDevice_create( | |
68 AttachCurrentThread(), base::android::GetApplicationContext())); | |
69 } | |
70 | |
71 CardboardVRDevice::~CardboardVRDevice() { | |
72 Java_CardboardVRDevice_stopTracking(AttachCurrentThread(), | |
73 j_cardboard_device_.obj()); | |
74 } | |
75 | |
76 VRDeviceInfoPtr CardboardVRDevice::GetVRDevice() { | |
77 VRDeviceInfoPtr device = VRDeviceInfo::New(); | |
78 | |
79 JNIEnv* env = AttachCurrentThread(); | |
80 | |
81 ScopedJavaLocalRef<jstring> j_device_name = | |
82 Java_CardboardVRDevice_getDeviceName(env, j_cardboard_device_.obj()); | |
83 device->deviceName = | |
84 base::android::ConvertJavaStringToUTF8(env, j_device_name.obj()); | |
85 | |
86 ScopedJavaLocalRef<jfloatArray> j_fov(env, env->NewFloatArray(4)); | |
87 Java_CardboardVRDevice_getFieldOfView(env, j_cardboard_device_.obj(), | |
88 j_fov.obj()); | |
89 | |
90 std::vector<float> fov; | |
91 base::android::JavaFloatArrayToFloatVector(env, j_fov.obj(), &fov); | |
92 | |
93 device->hmdInfo = VRHMDInfo::New(); | |
94 VRHMDInfoPtr& hmdInfo = device->hmdInfo; | |
95 | |
96 hmdInfo->leftEye = VREyeParameters::New(); | |
97 hmdInfo->rightEye = VREyeParameters::New(); | |
98 VREyeParametersPtr& leftEye = hmdInfo->leftEye; | |
99 VREyeParametersPtr& rightEye = hmdInfo->rightEye; | |
100 | |
101 leftEye->recommendedFieldOfView = VRFieldOfView::New(); | |
102 leftEye->recommendedFieldOfView->upDegrees = fov[0]; | |
103 leftEye->recommendedFieldOfView->downDegrees = fov[1]; | |
104 leftEye->recommendedFieldOfView->leftDegrees = fov[2]; | |
105 leftEye->recommendedFieldOfView->rightDegrees = fov[3]; | |
106 | |
107 // Cardboard devices always assume a mirrored FOV, so this is just the left | |
108 // eye FOV with the left and right degrees swapped. | |
109 rightEye->recommendedFieldOfView = VRFieldOfView::New(); | |
110 rightEye->recommendedFieldOfView->upDegrees = fov[0]; | |
111 rightEye->recommendedFieldOfView->downDegrees = fov[1]; | |
112 rightEye->recommendedFieldOfView->leftDegrees = fov[3]; | |
113 rightEye->recommendedFieldOfView->rightDegrees = fov[2]; | |
114 | |
115 // Cardboard does not support configurable FOV. | |
116 leftEye->maximumFieldOfView = leftEye->recommendedFieldOfView.Clone(); | |
117 rightEye->maximumFieldOfView = rightEye->recommendedFieldOfView.Clone(); | |
118 leftEye->minimumFieldOfView = leftEye->recommendedFieldOfView.Clone(); | |
119 rightEye->minimumFieldOfView = rightEye->recommendedFieldOfView.Clone(); | |
120 | |
121 float ipd = Java_CardboardVRDevice_getIpd(env, j_cardboard_device_.obj()); | |
122 | |
123 leftEye->eyeTranslation = VRVector3::New(); | |
124 leftEye->eyeTranslation->x = ipd * -0.5f; | |
125 leftEye->eyeTranslation->y = 0.0f; | |
126 leftEye->eyeTranslation->z = 0.0f; | |
127 | |
128 rightEye->eyeTranslation = VRVector3::New(); | |
129 rightEye->eyeTranslation->x = ipd * 0.5f; | |
130 rightEye->eyeTranslation->y = 0.0f; | |
131 rightEye->eyeTranslation->z = 0.0f; | |
132 | |
133 ScopedJavaLocalRef<jintArray> j_screen_size(env, env->NewIntArray(2)); | |
134 Java_CardboardVRDevice_getScreenSize(env, j_cardboard_device_.obj(), | |
135 j_screen_size.obj()); | |
136 | |
137 std::vector<int> screen_size; | |
138 base::android::JavaIntArrayToIntVector(env, j_screen_size.obj(), | |
139 &screen_size); | |
140 | |
141 leftEye->renderRect = VRRect::New(); | |
142 leftEye->renderRect->x = 0; | |
143 leftEye->renderRect->y = 0; | |
144 leftEye->renderRect->width = screen_size[0] / 2.0; | |
145 leftEye->renderRect->height = screen_size[1]; | |
146 | |
147 rightEye->renderRect = VRRect::New(); | |
148 rightEye->renderRect->x = screen_size[0] / 2.0; | |
149 rightEye->renderRect->y = 0; | |
150 rightEye->renderRect->width = screen_size[0] / 2.0; | |
151 rightEye->renderRect->height = screen_size[1]; | |
152 | |
153 return device.Pass(); | |
154 } | |
155 | |
156 VRSensorStatePtr CardboardVRDevice::GetSensorState() { | |
157 VRSensorStatePtr state = VRSensorState::New(); | |
158 | |
159 state->timestamp = base::Time::Now().ToJsTime(); | |
160 state->frameIndex = frame_index_; | |
161 | |
162 JNIEnv* env = AttachCurrentThread(); | |
163 ScopedJavaLocalRef<jfloatArray> j_head_matrix(env, env->NewFloatArray(16)); | |
164 Java_CardboardVRDevice_getSensorState(env, j_cardboard_device_.obj(), | |
165 j_head_matrix.obj()); | |
166 | |
167 std::vector<float> head_matrix; | |
168 base::android::JavaFloatArrayToFloatVector(env, j_head_matrix.obj(), | |
169 &head_matrix); | |
170 | |
171 state->orientation = MatrixToOrientationQuat(&head_matrix[0]); | |
172 | |
173 state->position = VRVector3::New(); | |
174 state->position->x = -head_matrix[12]; | |
175 state->position->y = head_matrix[13]; | |
176 state->position->z = head_matrix[14]; | |
177 | |
178 frame_index_++; | |
179 | |
180 return state.Pass(); | |
181 } | |
182 | |
183 void CardboardVRDevice::ResetSensor() { | |
184 Java_CardboardVRDevice_resetSensor(AttachCurrentThread(), | |
185 j_cardboard_device_.obj()); | |
186 } | |
187 | |
188 } // namespace content | |
OLD | NEW |