| 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" |
| 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 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 const gvr::Rectf& texture_rect) { | 231 const gvr::Rectf& texture_rect) { |
| 92 float width = static_cast<float>(texture_size.width); | 232 float width = static_cast<float>(texture_size.width); |
| 93 float height = static_cast<float>(texture_size.height); | 233 float height = static_cast<float>(texture_size.height); |
| 94 gvr::Rectf rect = ModulateRect(texture_rect, width, height); | 234 gvr::Rectf rect = ModulateRect(texture_rect, width, height); |
| 95 gvr::Recti result = { | 235 gvr::Recti result = { |
| 96 static_cast<int>(rect.left), static_cast<int>(rect.right), | 236 static_cast<int>(rect.left), static_cast<int>(rect.right), |
| 97 static_cast<int>(rect.bottom), static_cast<int>(rect.top)}; | 237 static_cast<int>(rect.bottom), static_cast<int>(rect.top)}; |
| 98 return result; | 238 return result; |
| 99 } | 239 } |
| 100 | 240 |
| 241 gvr::Vec3f getForwardVector(const gvr::Mat4f& matrix) { |
| 242 gvr::Vec3f forward; |
| 243 float* fp = &forward.x; |
| 244 // Same as multiplying the inverse of the rotation component of the matrix by |
| 245 // (0, 0, -1, 0). |
| 246 for (int i = 0; i < 3; ++i) { |
| 247 fp[i] = -matrix.m[2][i]; |
| 248 } |
| 249 return forward; |
| 250 } |
| 251 |
| 252 /** |
| 253 * Provides the relative translation of the head as a 3x1 vector. |
| 254 * |
| 255 */ |
| 256 gvr::Vec3f getTranslation(const gvr::Mat4f& matrix) { |
| 257 gvr::Vec3f translation; |
| 258 float* tp = &translation.x; |
| 259 // Same as multiplying the matrix by (0, 0, 0, 1). |
| 260 for (int i = 0; i < 3; ++i) { |
| 261 tp[i] = matrix.m[i][3]; |
| 262 } |
| 263 return translation; |
| 264 } |
| 265 |
| 101 GLuint CompileShader(GLenum shader_type, | 266 GLuint CompileShader(GLenum shader_type, |
| 102 const GLchar* shader_source, | 267 const GLchar* shader_source, |
| 103 std::string& error) { | 268 std::string& error) { |
| 104 GLuint shader_handle = glCreateShader(shader_type); | 269 GLuint shader_handle = glCreateShader(shader_type); |
| 105 if (shader_handle != 0) { | 270 if (shader_handle != 0) { |
| 106 // Pass in the shader source. | 271 // Pass in the shader source. |
| 107 int len = strlen(shader_source); | 272 int len = strlen(shader_source); |
| 108 glShaderSource(shader_handle, 1, &shader_source, &len); | 273 glShaderSource(shader_handle, 1, &shader_source, &len); |
| 109 // Compile the shader. | 274 // Compile the shader. |
| 110 glCompileShader(shader_handle); | 275 glCompileShader(shader_handle); |
| 111 // Get the compilation status. | 276 // Get the compilation status. |
| 112 GLint status; | 277 GLint status; |
| 113 glGetShaderiv(shader_handle, GL_COMPILE_STATUS, &status); | 278 glGetShaderiv(shader_handle, GL_COMPILE_STATUS, &status); |
| 114 if (status == GL_FALSE) { | 279 if (status == GL_FALSE) { |
| 115 GLint info_log_length; | 280 GLint info_log_length; |
| 116 glGetShaderiv(shader_handle, GL_INFO_LOG_LENGTH, &info_log_length); | 281 glGetShaderiv(shader_handle, GL_INFO_LOG_LENGTH, &info_log_length); |
| 117 GLchar* str_info_log = new GLchar[info_log_length + 1]; | 282 GLchar* str_info_log = new GLchar[info_log_length + 1]; |
| 118 glGetShaderInfoLog(shader_handle, info_log_length, NULL, str_info_log); | 283 glGetShaderInfoLog(shader_handle, info_log_length, nullptr, str_info_log); |
| 119 error = "Error compiling shader: "; | 284 error = "Error compiling shader: "; |
| 120 error += str_info_log; | 285 error += str_info_log; |
| 121 delete[] str_info_log; | 286 delete[] str_info_log; |
| 122 glDeleteShader(shader_handle); | 287 glDeleteShader(shader_handle); |
| 123 shader_handle = 0; | 288 shader_handle = 0; |
| 124 } | 289 } |
| 125 } | 290 } |
| 126 | 291 |
| 127 return shader_handle; | 292 return shader_handle; |
| 128 } | 293 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 156 // Get the link status. | 321 // Get the link status. |
| 157 GLint link_status; | 322 GLint link_status; |
| 158 glGetProgramiv(program_handle, GL_LINK_STATUS, &link_status); | 323 glGetProgramiv(program_handle, GL_LINK_STATUS, &link_status); |
| 159 | 324 |
| 160 // If the link failed, delete the program. | 325 // If the link failed, delete the program. |
| 161 if (link_status == GL_FALSE) { | 326 if (link_status == GL_FALSE) { |
| 162 GLint info_log_length; | 327 GLint info_log_length; |
| 163 glGetProgramiv(program_handle, GL_INFO_LOG_LENGTH, &info_log_length); | 328 glGetProgramiv(program_handle, GL_INFO_LOG_LENGTH, &info_log_length); |
| 164 | 329 |
| 165 GLchar* str_info_log = new GLchar[info_log_length + 1]; | 330 GLchar* str_info_log = new GLchar[info_log_length + 1]; |
| 166 glGetProgramInfoLog(program_handle, info_log_length, NULL, str_info_log); | 331 glGetProgramInfoLog(program_handle, info_log_length, nullptr, |
| 332 str_info_log); |
| 167 error = "Error compiling program: "; | 333 error = "Error compiling program: "; |
| 168 error += str_info_log; | 334 error += str_info_log; |
| 169 delete[] str_info_log; | 335 delete[] str_info_log; |
| 170 glDeleteProgram(program_handle); | 336 glDeleteProgram(program_handle); |
| 171 program_handle = 0; | 337 program_handle = 0; |
| 172 } | 338 } |
| 173 } | 339 } |
| 174 | 340 |
| 175 return program_handle; | 341 return program_handle; |
| 176 } | 342 } |
| 177 | 343 |
| 344 float VectorLength(const gvr::Vec3f& vec) { |
| 345 return sqrt(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z); |
| 346 } |
| 347 |
| 348 void NormalizeVector(gvr::Vec3f& vec) { |
| 349 float len = VectorLength(vec); |
| 350 vec.x /= len; |
| 351 vec.y /= len; |
| 352 vec.z /= len; |
| 353 } |
| 354 |
| 355 float VectorDot(const gvr::Vec3f& a, const gvr::Vec3f& b) { |
| 356 return a.x * b.x + a.y * b.y + a.z * b.z; |
| 357 } |
| 358 |
| 359 void NormalizeQuat(gvr::Quatf& quat) { |
| 360 float len = sqrt(quat.qx * quat.qx + quat.qy * quat.qy + quat.qz * quat.qz + |
| 361 quat.qw * quat.qw); |
| 362 quat.qx /= len; |
| 363 quat.qy /= len; |
| 364 quat.qz /= len; |
| 365 quat.qw /= len; |
| 366 } |
| 367 |
| 368 gvr::Quatf QuatFromAxisAngle(float x, float y, float z, float angle) { |
| 369 gvr::Quatf res; |
| 370 float s = sin(angle / 2); |
| 371 res.qx = x * s; |
| 372 res.qy = y * s; |
| 373 res.qz = z * s; |
| 374 res.qw = cos(angle / 2); |
| 375 return res; |
| 376 } |
| 377 |
| 378 gvr::Quatf QuatMultiply(const gvr::Quatf& a, const gvr::Quatf& b) { |
| 379 gvr::Quatf res; |
| 380 res.qw = a.qw * b.qw - a.qx * b.qx - a.qy * b.qy - a.qz * b.qz; |
| 381 res.qx = a.qw * b.qx + a.qx * b.qw + a.qy * b.qz - a.qz * b.qy; |
| 382 res.qy = a.qw * b.qy - a.qx * b.qz + a.qy * b.qw + a.qz * b.qx; |
| 383 res.qz = a.qw * b.qz + a.qx * b.qy - a.qy * b.qx + a.qz * b.qw; |
| 384 return res; |
| 385 } |
| 386 |
| 387 gvr::Mat4f QuatToMatrix(const gvr::Quatf& quat) { |
| 388 const float x2 = quat.qx * quat.qx; |
| 389 const float y2 = quat.qy * quat.qy; |
| 390 const float z2 = quat.qz * quat.qz; |
| 391 const float xy = quat.qx * quat.qy; |
| 392 const float xz = quat.qx * quat.qz; |
| 393 const float xw = quat.qx * quat.qw; |
| 394 const float yz = quat.qy * quat.qz; |
| 395 const float yw = quat.qy * quat.qw; |
| 396 const float zw = quat.qz * quat.qw; |
| 397 |
| 398 const float m11 = 1.0f - 2.0f * y2 - 2.0f * z2; |
| 399 const float m12 = 2.0f * (xy - zw); |
| 400 const float m13 = 2.0f * (xz + yw); |
| 401 const float m21 = 2.0f * (xy + zw); |
| 402 const float m22 = 1.0f - 2.0f * x2 - 2.0f * z2; |
| 403 const float m23 = 2.0f * (yz - xw); |
| 404 const float m31 = 2.0f * (xz - yw); |
| 405 const float m32 = 2.0f * (yz + xw); |
| 406 const float m33 = 1.0f - 2.0f * x2 - 2.0f * y2; |
| 407 |
| 408 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}; |
| 410 |
| 411 return *((gvr::Mat4f*)&ret); |
| 412 } |
| 413 |
| 178 } // namespace vr_shell | 414 } // namespace vr_shell |
| OLD | NEW |