| OLD | NEW |
| 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 Loading... |
| 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 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 | 206 |
| 221 return result; | 207 return result; |
| 222 } | 208 } |
| 223 | 209 |
| 224 gvr::Rectf ModulateRect(const gvr::Rectf& rect, float width, float height) { | 210 gvr::Rectf ModulateRect(const gvr::Rectf& rect, float width, float height) { |
| 225 gvr::Rectf result = {rect.left * width, rect.right * width, | 211 gvr::Rectf result = {rect.left * width, rect.right * width, |
| 226 rect.bottom * height, rect.top * height}; | 212 rect.bottom * height, rect.top * height}; |
| 227 return result; | 213 return result; |
| 228 } | 214 } |
| 229 | 215 |
| 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) { | 216 gvr::Vec3f getForwardVector(const gvr::Mat4f& matrix) { |
| 242 gvr::Vec3f forward; | 217 gvr::Vec3f forward; |
| 243 float* fp = &forward.x; | 218 float* fp = &forward.x; |
| 244 // Same as multiplying the inverse of the rotation component of the matrix by | 219 // Same as multiplying the inverse of the rotation component of the matrix by |
| 245 // (0, 0, -1, 0). | 220 // (0, 0, -1, 0). |
| 246 for (int i = 0; i < 3; ++i) { | 221 for (int i = 0; i < 3; ++i) { |
| 247 fp[i] = -matrix.m[2][i]; | 222 fp[i] = -matrix.m[2][i]; |
| 248 } | 223 } |
| 249 return forward; | 224 return forward; |
| 250 } | 225 } |
| 251 | 226 |
| 252 /** | 227 /** |
| 253 * Provides the relative translation of the head as a 3x1 vector. | 228 * Provides the relative translation of the head as a 3x1 vector. |
| 254 * | 229 * |
| 255 */ | 230 */ |
| 256 gvr::Vec3f getTranslation(const gvr::Mat4f& matrix) { | 231 gvr::Vec3f getTranslation(const gvr::Mat4f& matrix) { |
| 257 gvr::Vec3f translation; | 232 gvr::Vec3f translation; |
| 258 float* tp = &translation.x; | 233 float* tp = &translation.x; |
| 259 // Same as multiplying the matrix by (0, 0, 0, 1). | 234 // Same as multiplying the matrix by (0, 0, 0, 1). |
| 260 for (int i = 0; i < 3; ++i) { | 235 for (int i = 0; i < 3; ++i) { |
| 261 tp[i] = matrix.m[i][3]; | 236 tp[i] = matrix.m[i][3]; |
| 262 } | 237 } |
| 263 return translation; | 238 return translation; |
| 264 } | 239 } |
| 265 | 240 |
| 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) { | 241 float VectorLength(const gvr::Vec3f& vec) { |
| 345 return sqrt(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z); | 242 return sqrt(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z); |
| 346 } | 243 } |
| 347 | 244 |
| 348 void NormalizeVector(gvr::Vec3f& vec) { | 245 void NormalizeVector(gvr::Vec3f& vec) { |
| 349 float len = VectorLength(vec); | 246 float len = VectorLength(vec); |
| 350 vec.x /= len; | 247 vec.x /= len; |
| 351 vec.y /= len; | 248 vec.y /= len; |
| 352 vec.z /= len; | 249 vec.z /= len; |
| 353 } | 250 } |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 const float m32 = 2.0f * (yz + xw); | 302 const float m32 = 2.0f * (yz + xw); |
| 406 const float m33 = 1.0f - 2.0f * x2 - 2.0f * y2; | 303 const float m33 = 1.0f - 2.0f * x2 - 2.0f * y2; |
| 407 | 304 |
| 408 float ret[16] = {m11, m12, m13, 0.0f, m21, m22, m23, 0.0f, | 305 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}; | 306 m31, m32, m33, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}; |
| 410 | 307 |
| 411 return *((gvr::Mat4f*)&ret); | 308 return *((gvr::Mat4f*)&ret); |
| 412 } | 309 } |
| 413 | 310 |
| 414 } // namespace vr_shell | 311 } // namespace vr_shell |
| OLD | NEW |