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

Side by Side Diff: chrome/browser/android/vr_shell/vr_util.cc

Issue 2301633002: Refactor Vr activity into ChromeTabbedActivity. (Closed)
Patch Set: Address comments and rebase Created 4 years, 3 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
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_util.h"
6
5 #include <array> 7 #include <array>
6 #include <cmath> 8 #include <cmath>
7 9
8 #include "chrome/browser/android/vr_shell/vr_util.h"
9 #include "third_party/gvr-android-sdk/src/ndk-beta/include/vr/gvr/capi/include/g vr_types.h" 10 #include "third_party/gvr-android-sdk/src/ndk-beta/include/vr/gvr/capi/include/g vr_types.h"
10 11
11 namespace vr_shell { 12 namespace vr_shell {
12 13
14 // Internal matrix layout:
15 //
16 // m[0][0], m[0][1], m[0][2], m[0][3],
17 // m[1][0], m[1][1], m[1][2], m[1][3],
18 // m[2][0], m[2][1], m[2][2], m[2][3],
19 // m[3][0], m[3][1], m[3][2], m[3][3],
20 //
21 // The translation component is in the right column m[i][3].
22 //
23 // The bottom row m[3][i] is (0, 0, 0, 1) for non-perspective transforms.
24 //
25 // These matrices are intended to be used to premultiply column vectors
26 // for transforms, so successive transforms need to be left-multiplied.
27
28 void SetIdentityM(gvr::Mat4f& mat) {
29 float* m = (float*)mat.m;
30 for (int i = 0; i < 16; i++) {
31 m[i] = 0;
32 }
33 for (int i = 0; i < 16; i += 5) {
34 m[i] = 1.0f;
35 }
36 }
37
38 // Left multiply a translation matrix.
39 void TranslateM(gvr::Mat4f& tmat, gvr::Mat4f& mat, float x, float y, float z) {
40 if (&tmat != &mat) {
41 for (int i = 0; i < 4; ++i) {
42 for (int j = 0; j < 4; ++j) {
43 tmat.m[i][j] = mat.m[i][j];
44 }
45 }
46 }
47 tmat.m[0][3] += x;
48 tmat.m[1][3] += y;
49 tmat.m[2][3] += z;
50 }
51
52 // Right multiply a translation matrix.
53 void TranslateMRight(gvr::Mat4f& tmat,
54 gvr::Mat4f& mat,
55 float x,
56 float y,
57 float z) {
58 if (&tmat != &mat) {
59 for (int i = 0; i < 4; ++i) {
60 for (int j = 0; j < 3; ++j) {
61 tmat.m[i][j] = mat.m[i][j];
62 }
63 }
64 }
65
66 for (int i = 0; i < 4; i++) {
67 tmat.m[i][3] =
68 mat.m[i][0] * x + mat.m[i][1] * y + mat.m[i][2] * z + mat.m[i][3];
69 }
70 }
71
72 // Left multiply a scale matrix.
73 void ScaleM(gvr::Mat4f& tmat, const gvr::Mat4f& mat,
74 float x, float y, float z) {
75 if (&tmat != &mat) {
76 for (int i = 0; i < 4; ++i) {
77 for (int j = 0; j < 3; ++j) {
78 tmat.m[i][j] = mat.m[i][j];
79 }
80 }
81 }
82 // Multiply all rows including translation components.
83 for (int j = 0; j < 4; ++j) {
84 tmat.m[0][j] *= x;
85 tmat.m[1][j] *= y;
86 tmat.m[2][j] *= z;
87 }
88 }
89
90 // Right multiply a scale matrix.
91 void ScaleMRight(gvr::Mat4f& tmat, const gvr::Mat4f& mat,
92 float x, float y, float z) {
93 if (&tmat != &mat) {
94 for (int i = 0; i < 4; ++i) {
95 for (int j = 0; j < 3; ++j) {
96 tmat.m[i][j] = mat.m[i][j];
97 }
98 }
99 }
100 // Multiply columns, don't change translation components.
101 for (int i = 0; i < 3; ++i) {
102 tmat.m[i][0] *= x;
103 tmat.m[i][1] *= y;
104 tmat.m[i][2] *= z;
105 }
106 }
107
13 std::array<float, 16> MatrixToGLArray(const gvr::Mat4f& matrix) { 108 std::array<float, 16> MatrixToGLArray(const gvr::Mat4f& matrix) {
14 // Note that this performs a *transpose* to a column-major matrix array, as 109 // Note that this performs a *transpose* to a column-major matrix array, as
15 // expected by GL. The input matrix has translation components at [i][3] for 110 // expected by GL. The input matrix has translation components at [i][3] for
16 // use with row vectors and premultiplied transforms. In the output, the 111 // use with row vectors and premultiplied transforms. In the output, the
17 // translation elements are at the end at positions 3*4+i. 112 // translation elements are at the end at positions 3*4+i.
18 std::array<float, 16> result; 113 std::array<float, 16> result;
19 for (int i = 0; i < 4; ++i) { 114 for (int i = 0; i < 4; ++i) {
20 for (int j = 0; j < 4; ++j) { 115 for (int j = 0; j < 4; ++j) {
21 result[j * 4 + i] = matrix.m[i][j]; 116 result[j * 4 + i] = matrix.m[i][j];
22 } 117 }
23 } 118 }
24 return result; 119 return result;
25 } 120 }
26 121
27 gvr::Mat4f MatrixTranspose(const gvr::Mat4f& mat) { 122 gvr::Mat4f MatrixTranspose(const gvr::Mat4f& mat) {
28 gvr::Mat4f result; 123 gvr::Mat4f result;
29 for (int i = 0; i < 4; ++i) { 124 for (int i = 0; i < 4; ++i) {
30 for (int k = 0; k < 4; ++k) { 125 for (int k = 0; k < 4; ++k) {
31 result.m[i][k] = mat.m[k][i]; 126 result.m[i][k] = mat.m[k][i];
32 } 127 }
33 } 128 }
34 return result; 129 return result;
35 } 130 }
36 131
132 std::array<float, 4> MatrixVectorMul(const gvr::Mat4f& matrix,
133 const std::array<float, 4>& vec) {
134 std::array<float, 4> result;
135 for (int i = 0; i < 4; ++i) {
136 result[i] = 0;
137 for (int k = 0; k < 4; ++k) {
138 result[i] += matrix.m[i][k] * vec[k];
139 }
140 }
141 return result;
142 }
143
144 std::array<float, 3> MatrixVectorMul(const gvr::Mat4f& matrix,
145 const std::array<float, 3>& vec) {
146 // Use homogeneous coordinates for the multiplication.
147 std::array<float, 4> vec_h = {{vec[0], vec[1], vec[2], 1.0f}};
148 std::array<float, 4> result;
149 for (int i = 0; i < 4; ++i) {
150 result[i] = 0;
151 for (int k = 0; k < 4; ++k) {
152 result[i] += matrix.m[i][k] * vec_h[k];
153 }
154 }
155 // Convert back from homogeneous coordinates.
156 float rw = 1.0f / result[3];
157 return {{rw * result[0], rw * result[1], rw * result[2]}};
158 }
159
160 gvr::Vec3f MatrixVectorMul(const gvr::Mat4f& m, const gvr::Vec3f& v) {
161 gvr::Vec3f res;
162 res.x = m.m[0][0] * v.x + m.m[0][1] * v.y + m.m[0][2] * v.z + m.m[0][3];
163 res.y = m.m[1][0] * v.x + m.m[1][1] * v.y + m.m[1][2] * v.z + m.m[1][3];
164 res.z = m.m[2][0] * v.x + m.m[2][1] * v.y + m.m[2][2] * v.z + m.m[2][3];
165 return res;
166 }
167
168 // Rotation only, ignore translation components.
169 gvr::Vec3f MatrixVectorRotate(const gvr::Mat4f& m, const gvr::Vec3f& v) {
170 gvr::Vec3f res;
171 res.x = m.m[0][0] * v.x + m.m[0][1] * v.y + m.m[0][2] * v.z;
172 res.y = m.m[1][0] * v.x + m.m[1][1] * v.y + m.m[1][2] * v.z;
173 res.z = m.m[2][0] * v.x + m.m[2][1] * v.y + m.m[2][2] * v.z;
174 return res;
175 }
176
37 gvr::Mat4f MatrixMul(const gvr::Mat4f& matrix1, const gvr::Mat4f& matrix2) { 177 gvr::Mat4f MatrixMul(const gvr::Mat4f& matrix1, const gvr::Mat4f& matrix2) {
38 gvr::Mat4f result; 178 gvr::Mat4f result;
39 for (int i = 0; i < 4; ++i) { 179 for (int i = 0; i < 4; ++i) {
40 for (int j = 0; j < 4; ++j) { 180 for (int j = 0; j < 4; ++j) {
41 result.m[i][j] = 0.0f; 181 result.m[i][j] = 0.0f;
42 for (int k = 0; k < 4; ++k) { 182 for (int k = 0; k < 4; ++k) {
43 result.m[i][j] += matrix1.m[i][k] * matrix2.m[k][j]; 183 result.m[i][j] += matrix1.m[i][k] * matrix2.m[k][j];
44 } 184 }
45 } 185 }
46 } 186 }
47 return result; 187 return result;
48 } 188 }
49 189
190 gvr::Mat4f InvertM(const gvr::Mat4f& mat) {
klausw (use chromium instead) 2016/09/08 19:47:04 This appears to be unused. Since inverting a 4x4 m
mthiesse 2016/09/09 15:16:38 Done.
191 gvr::Mat4f resm;
192 float* result = (float*)resm.m;
193 const float* m = (const float*)mat.m;
194 // Invert a 4 x 4 matrix using Cramer's Rule.
195
196 // Transpose matrix.
197 const float src0 = m[0];
198 const float src4 = m[1];
199 const float src8 = m[2];
200 const float src12 = m[3];
201
202 const float src1 = m[4];
203 const float src5 = m[5];
204 const float src9 = m[6];
205 const float src13 = m[7];
206
207 const float src2 = m[8];
208 const float src6 = m[9];
209 const float src10 = m[10];
210 const float src14 = m[11];
211
212 const float src3 = m[12];
213 const float src7 = m[13];
214 const float src11 = m[14];
215 const float src15 = m[15];
216
217 // Calculate pairs for first 8 elements (cofactors).
218 const float atmp0 = src10 * src15;
219 const float atmp1 = src11 * src14;
220 const float atmp2 = src9 * src15;
221 const float atmp3 = src11 * src13;
222 const float atmp4 = src9 * src14;
223 const float atmp5 = src10 * src13;
224 const float atmp6 = src8 * src15;
225 const float atmp7 = src11 * src12;
226 const float atmp8 = src8 * src14;
227 const float atmp9 = src10 * src12;
228 const float atmp10 = src8 * src13;
229 const float atmp11 = src9 * src12;
230
231 // Calculate first 8 elements (cofactors).
232 const float dst0 = (atmp0 * src5 + atmp3 * src6 + atmp4 * src7) -
233 (atmp1 * src5 + atmp2 * src6 + atmp5 * src7);
234 const float dst1 = (atmp1 * src4 + atmp6 * src6 + atmp9 * src7) -
235 (atmp0 * src4 + atmp7 * src6 + atmp8 * src7);
236 const float dst2 = (atmp2 * src4 + atmp7 * src5 + atmp10 * src7) -
237 (atmp3 * src4 + atmp6 * src5 + atmp11 * src7);
238 const float dst3 = (atmp5 * src4 + atmp8 * src5 + atmp11 * src6) -
239 (atmp4 * src4 + atmp9 * src5 + atmp10 * src6);
240 const float dst4 = (atmp1 * src1 + atmp2 * src2 + atmp5 * src3) -
241 (atmp0 * src1 + atmp3 * src2 + atmp4 * src3);
242 const float dst5 = (atmp0 * src0 + atmp7 * src2 + atmp8 * src3) -
243 (atmp1 * src0 + atmp6 * src2 + atmp9 * src3);
244 const float dst6 = (atmp3 * src0 + atmp6 * src1 + atmp11 * src3) -
245 (atmp2 * src0 + atmp7 * src1 + atmp10 * src3);
246 const float dst7 = (atmp4 * src0 + atmp9 * src1 + atmp10 * src2) -
247 (atmp5 * src0 + atmp8 * src1 + atmp11 * src2);
248
249 // Calculate pairs for second 8 elements (cofactors).
250 const float btmp0 = src2 * src7;
251 const float btmp1 = src3 * src6;
252 const float btmp2 = src1 * src7;
253 const float btmp3 = src3 * src5;
254 const float btmp4 = src1 * src6;
255 const float btmp5 = src2 * src5;
256 const float btmp6 = src0 * src7;
257 const float btmp7 = src3 * src4;
258 const float btmp8 = src0 * src6;
259 const float btmp9 = src2 * src4;
260 const float btmp10 = src0 * src5;
261 const float btmp11 = src1 * src4;
262
263 // Calculate second 8 elements (cofactors).
264 const float dst8 = (btmp0 * src13 + btmp3 * src14 + btmp4 * src15) -
265 (btmp1 * src13 + btmp2 * src14 + btmp5 * src15);
266 const float dst9 = (btmp1 * src12 + btmp6 * src14 + btmp9 * src15) -
267 (btmp0 * src12 + btmp7 * src14 + btmp8 * src15);
268 const float dst10 = (btmp2 * src12 + btmp7 * src13 + btmp10 * src15) -
269 (btmp3 * src12 + btmp6 * src13 + btmp11 * src15);
270 const float dst11 = (btmp5 * src12 + btmp8 * src13 + btmp11 * src14) -
271 (btmp4 * src12 + btmp9 * src13 + btmp10 * src14);
272 const float dst12 = (btmp2 * src10 + btmp5 * src11 + btmp1 * src9) -
273 (btmp4 * src11 + btmp0 * src9 + btmp3 * src10);
274 const float dst13 = (btmp8 * src11 + btmp0 * src8 + btmp7 * src10) -
275 (btmp6 * src10 + btmp9 * src11 + btmp1 * src8);
276 const float dst14 = (btmp6 * src9 + btmp11 * src11 + btmp3 * src8) -
277 (btmp10 * src11 + btmp2 * src8 + btmp7 * src9);
278 const float dst15 = (btmp10 * src10 + btmp4 * src8 + btmp9 * src9) -
279 (btmp8 * src9 + btmp11 * src10 + btmp5 * src8);
280
281 // Calculate determinant.
282 float det = src0 * dst0 + src1 * dst1 + src2 * dst2 + src3 * dst3;
283
284 if (det == 0.0f) {
285 det = 1E-20;
286 }
287
288 // Calculate matrix inverse.
289 const float invdet = 1.0f / det;
290 result[0] = dst0 * invdet;
291 result[1] = dst1 * invdet;
292 result[2] = dst2 * invdet;
293 result[3] = dst3 * invdet;
294
295 result[4] = dst4 * invdet;
296 result[5] = dst5 * invdet;
297 result[6] = dst6 * invdet;
298 result[7] = dst7 * invdet;
299
300 result[8] = dst8 * invdet;
301 result[9] = dst9 * invdet;
302 result[10] = dst10 * invdet;
303 result[11] = dst11 * invdet;
304
305 result[12] = dst12 * invdet;
306 result[13] = dst13 * invdet;
307 result[14] = dst14 * invdet;
308 result[15] = dst15 * invdet;
309
310 return resm;
311 }
312
50 gvr::Mat4f PerspectiveMatrixFromView(const gvr::Rectf& fov, 313 gvr::Mat4f PerspectiveMatrixFromView(const gvr::Rectf& fov,
51 float z_near, 314 float z_near,
52 float z_far) { 315 float z_far) {
53 gvr::Mat4f result; 316 gvr::Mat4f result;
54 const float x_left = -std::tan(fov.left * M_PI / 180.0f) * z_near; 317 const float x_left = -std::tan(fov.left * M_PI / 180.0f) * z_near;
55 const float x_right = std::tan(fov.right * M_PI / 180.0f) * z_near; 318 const float x_right = std::tan(fov.right * M_PI / 180.0f) * z_near;
56 const float y_bottom = -std::tan(fov.bottom * M_PI / 180.0f) * z_near; 319 const float y_bottom = -std::tan(fov.bottom * M_PI / 180.0f) * z_near;
57 const float y_top = std::tan(fov.top * M_PI / 180.0f) * z_near; 320 const float y_top = std::tan(fov.top * M_PI / 180.0f) * z_near;
58 321
59 assert(x_left < x_right && y_bottom < y_top && z_near < z_far && 322 assert(x_left < x_right && y_bottom < y_top && z_near < z_far &&
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 const gvr::Rectf& texture_rect) { 354 const gvr::Rectf& texture_rect) {
92 float width = static_cast<float>(texture_size.width); 355 float width = static_cast<float>(texture_size.width);
93 float height = static_cast<float>(texture_size.height); 356 float height = static_cast<float>(texture_size.height);
94 gvr::Rectf rect = ModulateRect(texture_rect, width, height); 357 gvr::Rectf rect = ModulateRect(texture_rect, width, height);
95 gvr::Recti result = { 358 gvr::Recti result = {
96 static_cast<int>(rect.left), static_cast<int>(rect.right), 359 static_cast<int>(rect.left), static_cast<int>(rect.right),
97 static_cast<int>(rect.bottom), static_cast<int>(rect.top)}; 360 static_cast<int>(rect.bottom), static_cast<int>(rect.top)};
98 return result; 361 return result;
99 } 362 }
100 363
364 /**
365 * Provides the direction the head is looking towards as a 3x1 unit vector.
366 * </p><p>
367 * Note that in OpenGL the forward vector points into the -Z direction.
368 * Make sure to invert it if ever used to compute the basis of a right-handed
klausw (use chromium instead) 2016/09/08 19:47:04 Please update or delete this sentence - OpenGL is
bshe 2016/09/09 14:42:10 also: comment should be moved to .h file
mthiesse 2016/09/09 15:16:38 As you say Klaus, this comment doesn't make sense,
369 * system.
370 *
371 */
372 gvr::Vec3f getForwardVector(const gvr::Mat4f& matrix) {
373 gvr::Vec3f forward;
374 float* fp = &forward.x;
375 // Same as multiplying the inverse of the rotation component of the matrix by
376 // (0, 0, -1, 0).
377 for (int i = 0; i < 3; ++i) {
378 fp[i] = -matrix.m[2][i];
379 }
380 return forward;
381 }
382
383 /**
384 * Provides the relative translation of the head as a 3x1 vector.
385 *
386 */
bshe 2016/09/09 14:42:10 ditto
mthiesse 2016/09/09 15:16:38 Done.
387 gvr::Vec3f getTranslation(const gvr::Mat4f& matrix) {
388 gvr::Vec3f translation;
389 float* tp = &translation.x;
390 // Same as multiplying the matrix by (0, 0, 0, 1).
391 for (int i = 0; i < 3; ++i) {
392 tp[i] = matrix.m[i][3];
393 }
394 return translation;
395 }
396
101 GLuint CompileShader(GLenum shader_type, 397 GLuint CompileShader(GLenum shader_type,
102 const GLchar* shader_source, 398 const GLchar* shader_source,
103 std::string& error) { 399 std::string& error) {
104 GLuint shader_handle = glCreateShader(shader_type); 400 GLuint shader_handle = glCreateShader(shader_type);
105 if (shader_handle != 0) { 401 if (shader_handle != 0) {
106 // Pass in the shader source. 402 // Pass in the shader source.
107 int len = strlen(shader_source); 403 int len = strlen(shader_source);
108 glShaderSource(shader_handle, 1, &shader_source, &len); 404 glShaderSource(shader_handle, 1, &shader_source, &len);
109 // Compile the shader. 405 // Compile the shader.
110 glCompileShader(shader_handle); 406 glCompileShader(shader_handle);
111 // Get the compilation status. 407 // Get the compilation status.
112 GLint status; 408 GLint status;
113 glGetShaderiv(shader_handle, GL_COMPILE_STATUS, &status); 409 glGetShaderiv(shader_handle, GL_COMPILE_STATUS, &status);
114 if (status == GL_FALSE) { 410 if (status == GL_FALSE) {
115 GLint info_log_length; 411 GLint info_log_length;
116 glGetShaderiv(shader_handle, GL_INFO_LOG_LENGTH, &info_log_length); 412 glGetShaderiv(shader_handle, GL_INFO_LOG_LENGTH, &info_log_length);
117 GLchar* str_info_log = new GLchar[info_log_length + 1]; 413 GLchar* str_info_log = new GLchar[info_log_length + 1];
118 glGetShaderInfoLog(shader_handle, info_log_length, NULL, str_info_log); 414 glGetShaderInfoLog(shader_handle, info_log_length, nullptr, str_info_log);
119 error = "Error compiling shader: "; 415 error = "Error compiling shader: ";
120 error += str_info_log; 416 error += str_info_log;
121 delete[] str_info_log; 417 delete[] str_info_log;
122 glDeleteShader(shader_handle); 418 glDeleteShader(shader_handle);
123 shader_handle = 0; 419 shader_handle = 0;
124 } 420 }
125 } 421 }
126 422
127 return shader_handle; 423 return shader_handle;
128 } 424 }
(...skipping 27 matching lines...) Expand all
156 // Get the link status. 452 // Get the link status.
157 GLint link_status; 453 GLint link_status;
158 glGetProgramiv(program_handle, GL_LINK_STATUS, &link_status); 454 glGetProgramiv(program_handle, GL_LINK_STATUS, &link_status);
159 455
160 // If the link failed, delete the program. 456 // If the link failed, delete the program.
161 if (link_status == GL_FALSE) { 457 if (link_status == GL_FALSE) {
162 GLint info_log_length; 458 GLint info_log_length;
163 glGetProgramiv(program_handle, GL_INFO_LOG_LENGTH, &info_log_length); 459 glGetProgramiv(program_handle, GL_INFO_LOG_LENGTH, &info_log_length);
164 460
165 GLchar* str_info_log = new GLchar[info_log_length + 1]; 461 GLchar* str_info_log = new GLchar[info_log_length + 1];
166 glGetProgramInfoLog(program_handle, info_log_length, NULL, str_info_log); 462 glGetProgramInfoLog(program_handle, info_log_length, nullptr,
463 str_info_log);
167 error = "Error compiling program: "; 464 error = "Error compiling program: ";
168 error += str_info_log; 465 error += str_info_log;
169 delete[] str_info_log; 466 delete[] str_info_log;
170 glDeleteProgram(program_handle); 467 glDeleteProgram(program_handle);
171 program_handle = 0; 468 program_handle = 0;
172 } 469 }
173 } 470 }
174 471
175 return program_handle; 472 return program_handle;
176 } 473 }
177 474
475 float VectorLength(const gvr::Vec3f& vec) {
476 return sqrt(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z);
477 }
478
479 void NormalizeVector(gvr::Vec3f& vec) {
480 float len = VectorLength(vec);
481 vec.x /= len;
482 vec.y /= len;
483 vec.z /= len;
484 }
485
486 float VectorDot(const gvr::Vec3f& a, const gvr::Vec3f& b) {
487 return a.x * b.x + a.y * b.y + a.z * b.z;
488 }
489
490 void NormalizeQuat(gvr::Quatf& quat) {
491 float len = sqrt(quat.qx * quat.qx + quat.qy * quat.qy + quat.qz * quat.qz +
492 quat.qw * quat.qw);
493 quat.qx /= len;
494 quat.qy /= len;
495 quat.qz /= len;
496 quat.qw /= len;
497 }
498
499 gvr::Quatf QuatFromAxisAngle(float x, float y, float z, float angle) {
500 gvr::Quatf res;
501 float s = sin(angle / 2);
502 res.qx = x * s;
503 res.qy = y * s;
504 res.qz = z * s;
505 res.qw = cos(angle / 2);
506 return res;
507 }
508
509 gvr::Quatf QuatMultiply(const gvr::Quatf& a, const gvr::Quatf& b) {
510 gvr::Quatf res;
511 res.qw = a.qw * b.qw - a.qx * b.qx - a.qy * b.qy - a.qz * b.qz;
512 res.qx = a.qw * b.qx + a.qx * b.qw + a.qy * b.qz - a.qz * b.qy;
513 res.qy = a.qw * b.qy - a.qx * b.qz + a.qy * b.qw + a.qz * b.qx;
514 res.qz = a.qw * b.qz + a.qx * b.qy - a.qy * b.qx + a.qz * b.qw;
515 return res;
516 }
517
518 gvr::Mat4f QuatToMatrix(const gvr::Quatf& quat) {
519 // const float x = quat.qx;
bshe 2016/09/09 14:42:10 remove these comments?
mthiesse 2016/09/09 15:16:38 Done.
520 const float x2 = quat.qx * quat.qx;
521 // const float y = quat.qy;
522 const float y2 = quat.qy * quat.qy;
523 // const float z = quat.qz;
524 const float z2 = quat.qz * quat.qz;
525 // const float w = quat.qw;
526 const float xy = quat.qx * quat.qy;
527 const float xz = quat.qx * quat.qz;
528 const float xw = quat.qx * quat.qw;
529 const float yz = quat.qy * quat.qz;
530 const float yw = quat.qy * quat.qw;
531 const float zw = quat.qz * quat.qw;
532
533 const float m11 = 1.0f - 2.0f * y2 - 2.0f * z2;
534 const float m12 = 2.0f * (xy - zw);
535 const float m13 = 2.0f * (xz + yw);
536 const float m21 = 2.0f * (xy + zw);
537 const float m22 = 1.0f - 2.0f * x2 - 2.0f * z2;
538 const float m23 = 2.0f * (yz - xw);
539 const float m31 = 2.0f * (xz - yw);
540 const float m32 = 2.0f * (yz + xw);
541 const float m33 = 1.0f - 2.0f * x2 - 2.0f * y2;
542
543 float ret[16] = {m11, m12, m13, 0.0f, m21, m22, m23, 0.0f,
544 m31, m32, m33, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
545
546 return *((gvr::Mat4f*)&ret);
547 }
548
178 } // namespace vr_shell 549 } // namespace vr_shell
OLDNEW
« chrome/browser/android/vr_shell/vr_util.h ('K') | « chrome/browser/android/vr_shell/vr_util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698