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_math.cc

Issue 2335643002: Add VR Shell animation classes and unit test. (Closed)
Patch Set: Add VR Shell animation classes and unit test. 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
« no previous file with comments | « chrome/browser/android/vr_shell/vr_math.h ('k') | chrome/browser/android/vr_shell/vr_shell.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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" 5 #include "chrome/browser/android/vr_shell/vr_math.h"
6 6
7 #include <array> 7 #include <array>
8 #include <cmath> 8 #include <cmath>
9 9
10 #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"
11 11
12 namespace vr_shell { 12 namespace vr_shell {
13 13
14 // Internal matrix layout: 14 // Internal matrix layout:
15 // 15 //
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 } 98 }
99 } 99 }
100 // Multiply columns, don't change translation components. 100 // Multiply columns, don't change translation components.
101 for (int i = 0; i < 3; ++i) { 101 for (int i = 0; i < 3; ++i) {
102 tmat.m[i][0] *= x; 102 tmat.m[i][0] *= x;
103 tmat.m[i][1] *= y; 103 tmat.m[i][1] *= y;
104 tmat.m[i][2] *= z; 104 tmat.m[i][2] *= z;
105 } 105 }
106 } 106 }
107 107
108 std::array<float, 16> MatrixToGLArray(const gvr::Mat4f& matrix) {
109 // Note that this performs a *transpose* to a column-major matrix array, as
110 // expected by GL. The input matrix has translation components at [i][3] for
111 // use with row vectors and premultiplied transforms. In the output, the
112 // translation elements are at the end at positions 3*4+i.
113 std::array<float, 16> result;
114 for (int i = 0; i < 4; ++i) {
115 for (int j = 0; j < 4; ++j) {
116 result[j * 4 + i] = matrix.m[i][j];
117 }
118 }
119 return result;
120 }
121
122 gvr::Mat4f MatrixTranspose(const gvr::Mat4f& mat) { 108 gvr::Mat4f MatrixTranspose(const gvr::Mat4f& mat) {
123 gvr::Mat4f result; 109 gvr::Mat4f result;
124 for (int i = 0; i < 4; ++i) { 110 for (int i = 0; i < 4; ++i) {
125 for (int k = 0; k < 4; ++k) { 111 for (int k = 0; k < 4; ++k) {
126 result.m[i][k] = mat.m[k][i]; 112 result.m[i][k] = mat.m[k][i];
127 } 113 }
128 } 114 }
129 return result; 115 return result;
130 } 116 }
131 117
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 result.m[0][2] = A; 200 result.m[0][2] = A;
215 result.m[1][1] = Y; 201 result.m[1][1] = Y;
216 result.m[1][2] = B; 202 result.m[1][2] = B;
217 result.m[2][2] = C; 203 result.m[2][2] = C;
218 result.m[2][3] = D; 204 result.m[2][3] = D;
219 result.m[3][2] = -1; 205 result.m[3][2] = -1;
220 206
221 return result; 207 return result;
222 } 208 }
223 209
224 gvr::Rectf ModulateRect(const gvr::Rectf& rect, float width, float height) {
225 gvr::Rectf result = {rect.left * width, rect.right * width,
226 rect.bottom * height, rect.top * height};
227 return result;
228 }
229
230 gvr::Recti CalculatePixelSpaceRect(const gvr::Sizei& texture_size,
231 const gvr::Rectf& texture_rect) {
232 float width = static_cast<float>(texture_size.width);
233 float height = static_cast<float>(texture_size.height);
234 gvr::Rectf rect = ModulateRect(texture_rect, width, height);
235 gvr::Recti result = {
236 static_cast<int>(rect.left), static_cast<int>(rect.right),
237 static_cast<int>(rect.bottom), static_cast<int>(rect.top)};
238 return result;
239 }
240
241 gvr::Vec3f getForwardVector(const gvr::Mat4f& matrix) { 210 gvr::Vec3f getForwardVector(const gvr::Mat4f& matrix) {
242 gvr::Vec3f forward; 211 gvr::Vec3f forward;
243 float* fp = &forward.x; 212 float* fp = &forward.x;
244 // Same as multiplying the inverse of the rotation component of the matrix by 213 // Same as multiplying the inverse of the rotation component of the matrix by
245 // (0, 0, -1, 0). 214 // (0, 0, -1, 0).
246 for (int i = 0; i < 3; ++i) { 215 for (int i = 0; i < 3; ++i) {
247 fp[i] = -matrix.m[2][i]; 216 fp[i] = -matrix.m[2][i];
248 } 217 }
249 return forward; 218 return forward;
250 } 219 }
251 220
252 /** 221 /**
253 * Provides the relative translation of the head as a 3x1 vector. 222 * Provides the relative translation of the head as a 3x1 vector.
254 * 223 *
255 */ 224 */
256 gvr::Vec3f getTranslation(const gvr::Mat4f& matrix) { 225 gvr::Vec3f getTranslation(const gvr::Mat4f& matrix) {
257 gvr::Vec3f translation; 226 gvr::Vec3f translation;
258 float* tp = &translation.x; 227 float* tp = &translation.x;
259 // Same as multiplying the matrix by (0, 0, 0, 1). 228 // Same as multiplying the matrix by (0, 0, 0, 1).
260 for (int i = 0; i < 3; ++i) { 229 for (int i = 0; i < 3; ++i) {
261 tp[i] = matrix.m[i][3]; 230 tp[i] = matrix.m[i][3];
262 } 231 }
263 return translation; 232 return translation;
264 } 233 }
265 234
266 GLuint CompileShader(GLenum shader_type,
267 const GLchar* shader_source,
268 std::string& error) {
269 GLuint shader_handle = glCreateShader(shader_type);
270 if (shader_handle != 0) {
271 // Pass in the shader source.
272 int len = strlen(shader_source);
273 glShaderSource(shader_handle, 1, &shader_source, &len);
274 // Compile the shader.
275 glCompileShader(shader_handle);
276 // Get the compilation status.
277 GLint status;
278 glGetShaderiv(shader_handle, GL_COMPILE_STATUS, &status);
279 if (status == GL_FALSE) {
280 GLint info_log_length;
281 glGetShaderiv(shader_handle, GL_INFO_LOG_LENGTH, &info_log_length);
282 GLchar* str_info_log = new GLchar[info_log_length + 1];
283 glGetShaderInfoLog(shader_handle, info_log_length, nullptr, str_info_log);
284 error = "Error compiling shader: ";
285 error += str_info_log;
286 delete[] str_info_log;
287 glDeleteShader(shader_handle);
288 shader_handle = 0;
289 }
290 }
291
292 return shader_handle;
293 }
294
295 GLuint CreateAndLinkProgram(GLuint vertext_shader_handle,
296 GLuint fragment_shader_handle,
297 int num_attributes,
298 const GLchar** attributes,
299 std::string& error) {
300 GLuint program_handle = glCreateProgram();
301
302 if (program_handle != 0) {
303 // Bind the vertex shader to the program.
304 glAttachShader(program_handle, vertext_shader_handle);
305
306 // Bind the fragment shader to the program.
307 glAttachShader(program_handle, fragment_shader_handle);
308
309 // Bind attributes. This is optional, no need to supply them if
310 // using glGetAttribLocation to look them up. Useful for a single
311 // vertex array object (VAO) that is used with multiple shaders.
312 if (attributes != nullptr) {
313 for (int i = 0; i < num_attributes; i++) {
314 glBindAttribLocation(program_handle, i, attributes[i]);
315 }
316 }
317
318 // Link the two shaders together into a program.
319 glLinkProgram(program_handle);
320
321 // Get the link status.
322 GLint link_status;
323 glGetProgramiv(program_handle, GL_LINK_STATUS, &link_status);
324
325 // If the link failed, delete the program.
326 if (link_status == GL_FALSE) {
327 GLint info_log_length;
328 glGetProgramiv(program_handle, GL_INFO_LOG_LENGTH, &info_log_length);
329
330 GLchar* str_info_log = new GLchar[info_log_length + 1];
331 glGetProgramInfoLog(program_handle, info_log_length, nullptr,
332 str_info_log);
333 error = "Error compiling program: ";
334 error += str_info_log;
335 delete[] str_info_log;
336 glDeleteProgram(program_handle);
337 program_handle = 0;
338 }
339 }
340
341 return program_handle;
342 }
343
344 float VectorLength(const gvr::Vec3f& vec) { 235 float VectorLength(const gvr::Vec3f& vec) {
345 return sqrt(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z); 236 return sqrt(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z);
346 } 237 }
347 238
348 void NormalizeVector(gvr::Vec3f& vec) { 239 void NormalizeVector(gvr::Vec3f& vec) {
349 float len = VectorLength(vec); 240 float len = VectorLength(vec);
350 vec.x /= len; 241 vec.x /= len;
351 vec.y /= len; 242 vec.y /= len;
352 vec.z /= len; 243 vec.z /= len;
353 } 244 }
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 const float m32 = 2.0f * (yz + xw); 296 const float m32 = 2.0f * (yz + xw);
406 const float m33 = 1.0f - 2.0f * x2 - 2.0f * y2; 297 const float m33 = 1.0f - 2.0f * x2 - 2.0f * y2;
407 298
408 float ret[16] = {m11, m12, m13, 0.0f, m21, m22, m23, 0.0f, 299 float ret[16] = {m11, m12, m13, 0.0f, m21, m22, m23, 0.0f,
409 m31, m32, m33, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}; 300 m31, m32, m33, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
410 301
411 return *((gvr::Mat4f*)&ret); 302 return *((gvr::Mat4f*)&ret);
412 } 303 }
413 304
414 } // namespace vr_shell 305 } // namespace vr_shell
OLDNEW
« no previous file with comments | « chrome/browser/android/vr_shell/vr_math.h ('k') | chrome/browser/android/vr_shell/vr_shell.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698