Chromium Code Reviews| Index: chrome/browser/android/vr_shell/vr_util.cc |
| diff --git a/chrome/browser/android/vr_shell/vr_util.cc b/chrome/browser/android/vr_shell/vr_util.cc |
| index 69fdee8f6279dad1d138cd2cd6853f7847758aca..e3e9948f828416dd2b4a4bb45acb77c19078ba87 100644 |
| --- a/chrome/browser/android/vr_shell/vr_util.cc |
| +++ b/chrome/browser/android/vr_shell/vr_util.cc |
| @@ -2,14 +2,107 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| +#include "chrome/browser/android/vr_shell/vr_util.h" |
| + |
| #include <array> |
| #include <cmath> |
| -#include "chrome/browser/android/vr_shell/vr_util.h" |
| #include "third_party/gvr-android-sdk/src/ndk-beta/include/vr/gvr/capi/include/gvr_types.h" |
| namespace vr_shell { |
| +// Internal matrix layout: |
| +// |
| +// m[0][0], m[0][1], m[0][2], m[0][3], |
| +// m[1][0], m[1][1], m[1][2], m[1][3], |
| +// m[2][0], m[2][1], m[2][2], m[2][3], |
| +// m[3][0], m[3][1], m[3][2], m[3][3], |
| +// |
| +// The translation component is in the right column m[i][3]. |
| +// |
| +// The bottom row m[3][i] is (0, 0, 0, 1) for non-perspective transforms. |
| +// |
| +// These matrices are intended to be used to premultiply column vectors |
| +// for transforms, so successive transforms need to be left-multiplied. |
| + |
| +void setIdentityM(gvr::Mat4f& mat) { |
| + float* m = (float*)mat.m; |
| + for (int i = 0; i < 16; i++) { |
| + m[i] = 0; |
| + } |
| + for (int i = 0; i < 16; i += 5) { |
| + m[i] = 1.0f; |
| + } |
| +} |
| + |
| +// Left multiply a translation matrix. |
| +void translateM(gvr::Mat4f& tmat, gvr::Mat4f& mat, float x, float y, float z) { |
| + if (&tmat != &mat) { |
| + for (int i = 0; i < 4; ++i) { |
| + for (int j = 0; j < 4; ++j) { |
| + tmat.m[i][j] = mat.m[i][j]; |
| + } |
| + } |
| + } |
| + tmat.m[0][3] += x; |
| + tmat.m[1][3] += y; |
| + tmat.m[2][3] += z; |
| +} |
| + |
| +// Right multiply a translation matrix. |
| +void translateMRight(gvr::Mat4f& tmat, |
| + gvr::Mat4f& mat, |
| + float x, |
| + float y, |
| + float z) { |
| + if (&tmat != &mat) { |
| + for (int i = 0; i < 4; ++i) { |
| + for (int j = 0; j < 3; ++j) { |
| + tmat.m[i][j] = mat.m[i][j]; |
| + } |
| + } |
| + } |
| + |
| + for (int i = 0; i < 4; i++) { |
| + tmat.m[i][3] = |
| + mat.m[i][0] * x + mat.m[i][1] * y + mat.m[i][2] * z + mat.m[i][3]; |
| + } |
| +} |
| + |
| +// Left multiply a scale matrix. |
| +void scaleM(gvr::Mat4f& tmat, gvr::Mat4f& mat, float x, float y, float z) { |
| + if (&tmat != &mat) { |
| + for (int i = 0; i < 4; ++i) { |
| + for (int j = 0; j < 3; ++j) { |
| + tmat.m[i][j] = mat.m[i][j]; |
| + } |
| + } |
| + } |
| + // Multiply all rows including translation components. |
| + for (int j = 0; j < 4; ++j) { |
| + tmat.m[0][j] *= x; |
| + tmat.m[1][j] *= y; |
| + tmat.m[2][j] *= z; |
| + } |
| +} |
| + |
| +// Right multiply a scale matrix. |
| +void scaleMRight(gvr::Mat4f& tmat, gvr::Mat4f& mat, float x, float y, float z) { |
| + if (&tmat != &mat) { |
| + for (int i = 0; i < 4; ++i) { |
| + for (int j = 0; j < 3; ++j) { |
| + tmat.m[i][j] = mat.m[i][j]; |
| + } |
| + } |
| + } |
| + // Multiply columns, don't change translation components. |
| + for (int i = 0; i < 3; ++i) { |
| + tmat.m[i][0] *= x; |
| + tmat.m[i][1] *= y; |
| + tmat.m[i][2] *= z; |
| + } |
| +} |
| + |
| std::array<float, 16> MatrixToGLArray(const gvr::Mat4f& matrix) { |
| // Note that this performs a *transpose* to a column-major matrix array, as |
| // expected by GL. The input matrix has translation components at [i][3] for |
| @@ -34,6 +127,51 @@ gvr::Mat4f MatrixTranspose(const gvr::Mat4f& mat) { |
| return result; |
| } |
| +std::array<float, 4> MatrixVectorMul(const gvr::Mat4f& matrix, |
| + const std::array<float, 4>& vec) { |
| + std::array<float, 4> result; |
| + for (int i = 0; i < 4; ++i) { |
| + result[i] = 0; |
| + for (int k = 0; k < 4; ++k) { |
| + result[i] += matrix.m[i][k] * vec[k]; |
| + } |
| + } |
| + return result; |
| +} |
| + |
| +std::array<float, 3> MatrixVectorMul(const gvr::Mat4f& matrix, |
| + const std::array<float, 3>& vec) { |
| + // Use homogeneous coordinates for the multiplication. |
| + std::array<float, 4> vec_h = {{vec[0], vec[1], vec[2], 1.0f}}; |
| + std::array<float, 4> result; |
| + for (int i = 0; i < 4; ++i) { |
| + result[i] = 0; |
| + for (int k = 0; k < 4; ++k) { |
| + result[i] += matrix.m[i][k] * vec_h[k]; |
| + } |
| + } |
| + // Convert back from homogeneous coordinates. |
| + float rw = 1.0f / result[3]; |
| + return {{rw * result[0], rw * result[1], rw * result[2]}}; |
| +} |
| + |
| +gvr::Vec3f MatrixVectorMul(const gvr::Mat4f& m, gvr::Vec3f v) { |
| + gvr::Vec3f res; |
| + res.x = m.m[0][0] * v.x + m.m[0][1] * v.y + m.m[0][2] * v.z + m.m[0][3]; |
| + res.y = m.m[1][0] * v.x + m.m[1][1] * v.y + m.m[1][2] * v.z + m.m[1][3]; |
| + res.z = m.m[2][0] * v.x + m.m[2][1] * v.y + m.m[2][2] * v.z + m.m[2][3]; |
| + return res; |
| +} |
| + |
| +// Rotation only, ignore translation components. |
| +gvr::Vec3f MatrixVectorRotate(const gvr::Mat4f& m, gvr::Vec3f v) { |
| + gvr::Vec3f res; |
| + res.x = m.m[0][0] * v.x + m.m[0][1] * v.y + m.m[0][2] * v.z; |
| + res.y = m.m[1][0] * v.x + m.m[1][1] * v.y + m.m[1][2] * v.z; |
| + res.z = m.m[2][0] * v.x + m.m[2][1] * v.y + m.m[2][2] * v.z; |
| + return res; |
| +} |
| + |
| gvr::Mat4f MatrixMul(const gvr::Mat4f& matrix1, const gvr::Mat4f& matrix2) { |
| gvr::Mat4f result; |
| for (int i = 0; i < 4; ++i) { |
| @@ -47,6 +185,129 @@ gvr::Mat4f MatrixMul(const gvr::Mat4f& matrix1, const gvr::Mat4f& matrix2) { |
| return result; |
| } |
| +gvr::Mat4f invertM(gvr::Mat4f mat) { |
| + gvr::Mat4f resm; |
| + float* result = (float*)resm.m; |
| + float* m = (float*)mat.m; |
| + // Invert a 4 x 4 matrix using Cramer's Rule |
|
David Trainor- moved to gerrit
2016/09/08 05:49:30
End these comments with ., first letter upper case
mthiesse
2016/09/08 17:44:41
Done.
|
| + |
| + // transpose matrix |
| + const float src0 = m[0]; |
| + const float src4 = m[1]; |
| + const float src8 = m[2]; |
| + const float src12 = m[3]; |
| + |
| + const float src1 = m[4]; |
| + const float src5 = m[5]; |
| + const float src9 = m[6]; |
| + const float src13 = m[7]; |
| + |
| + const float src2 = m[8]; |
| + const float src6 = m[9]; |
| + const float src10 = m[10]; |
| + const float src14 = m[11]; |
| + |
| + const float src3 = m[12]; |
| + const float src7 = m[13]; |
| + const float src11 = m[14]; |
| + const float src15 = m[15]; |
| + |
| + // calculate pairs for first 8 elements (cofactors) |
| + const float atmp0 = src10 * src15; |
| + const float atmp1 = src11 * src14; |
| + const float atmp2 = src9 * src15; |
| + const float atmp3 = src11 * src13; |
| + const float atmp4 = src9 * src14; |
| + const float atmp5 = src10 * src13; |
| + const float atmp6 = src8 * src15; |
| + const float atmp7 = src11 * src12; |
| + const float atmp8 = src8 * src14; |
| + const float atmp9 = src10 * src12; |
| + const float atmp10 = src8 * src13; |
| + const float atmp11 = src9 * src12; |
| + |
| + // calculate first 8 elements (cofactors) |
| + const float dst0 = (atmp0 * src5 + atmp3 * src6 + atmp4 * src7) - |
| + (atmp1 * src5 + atmp2 * src6 + atmp5 * src7); |
| + const float dst1 = (atmp1 * src4 + atmp6 * src6 + atmp9 * src7) - |
| + (atmp0 * src4 + atmp7 * src6 + atmp8 * src7); |
| + const float dst2 = (atmp2 * src4 + atmp7 * src5 + atmp10 * src7) - |
| + (atmp3 * src4 + atmp6 * src5 + atmp11 * src7); |
| + const float dst3 = (atmp5 * src4 + atmp8 * src5 + atmp11 * src6) - |
| + (atmp4 * src4 + atmp9 * src5 + atmp10 * src6); |
| + const float dst4 = (atmp1 * src1 + atmp2 * src2 + atmp5 * src3) - |
| + (atmp0 * src1 + atmp3 * src2 + atmp4 * src3); |
| + const float dst5 = (atmp0 * src0 + atmp7 * src2 + atmp8 * src3) - |
| + (atmp1 * src0 + atmp6 * src2 + atmp9 * src3); |
| + const float dst6 = (atmp3 * src0 + atmp6 * src1 + atmp11 * src3) - |
| + (atmp2 * src0 + atmp7 * src1 + atmp10 * src3); |
| + const float dst7 = (atmp4 * src0 + atmp9 * src1 + atmp10 * src2) - |
| + (atmp5 * src0 + atmp8 * src1 + atmp11 * src2); |
| + |
| + // calculate pairs for second 8 elements (cofactors) |
| + const float btmp0 = src2 * src7; |
| + const float btmp1 = src3 * src6; |
| + const float btmp2 = src1 * src7; |
| + const float btmp3 = src3 * src5; |
| + const float btmp4 = src1 * src6; |
| + const float btmp5 = src2 * src5; |
| + const float btmp6 = src0 * src7; |
| + const float btmp7 = src3 * src4; |
| + const float btmp8 = src0 * src6; |
| + const float btmp9 = src2 * src4; |
| + const float btmp10 = src0 * src5; |
| + const float btmp11 = src1 * src4; |
| + |
| + // calculate second 8 elements (cofactors) |
| + const float dst8 = (btmp0 * src13 + btmp3 * src14 + btmp4 * src15) - |
| + (btmp1 * src13 + btmp2 * src14 + btmp5 * src15); |
| + const float dst9 = (btmp1 * src12 + btmp6 * src14 + btmp9 * src15) - |
| + (btmp0 * src12 + btmp7 * src14 + btmp8 * src15); |
| + const float dst10 = (btmp2 * src12 + btmp7 * src13 + btmp10 * src15) - |
| + (btmp3 * src12 + btmp6 * src13 + btmp11 * src15); |
| + const float dst11 = (btmp5 * src12 + btmp8 * src13 + btmp11 * src14) - |
| + (btmp4 * src12 + btmp9 * src13 + btmp10 * src14); |
| + const float dst12 = (btmp2 * src10 + btmp5 * src11 + btmp1 * src9) - |
| + (btmp4 * src11 + btmp0 * src9 + btmp3 * src10); |
| + const float dst13 = (btmp8 * src11 + btmp0 * src8 + btmp7 * src10) - |
| + (btmp6 * src10 + btmp9 * src11 + btmp1 * src8); |
| + const float dst14 = (btmp6 * src9 + btmp11 * src11 + btmp3 * src8) - |
| + (btmp10 * src11 + btmp2 * src8 + btmp7 * src9); |
| + const float dst15 = (btmp10 * src10 + btmp4 * src8 + btmp9 * src9) - |
| + (btmp8 * src9 + btmp11 * src10 + btmp5 * src8); |
| + |
| + // calculate determinant |
| + float det = src0 * dst0 + src1 * dst1 + src2 * dst2 + src3 * dst3; |
| + |
| + if (det == 0.0f) { |
| + det = 1E-20; |
| + } |
| + |
| + // calculate matrix inverse |
| + const float invdet = 1.0f / det; |
| + result[0] = dst0 * invdet; |
| + result[1] = dst1 * invdet; |
| + result[2] = dst2 * invdet; |
| + result[3] = dst3 * invdet; |
| + |
| + result[4] = dst4 * invdet; |
| + result[5] = dst5 * invdet; |
| + result[6] = dst6 * invdet; |
| + result[7] = dst7 * invdet; |
| + |
| + result[8] = dst8 * invdet; |
| + result[9] = dst9 * invdet; |
| + result[10] = dst10 * invdet; |
| + result[11] = dst11 * invdet; |
| + |
| + result[12] = dst12 * invdet; |
| + result[13] = dst13 * invdet; |
| + result[14] = dst14 * invdet; |
| + result[15] = dst15 * invdet; |
| + |
| + return resm; |
| +} |
| + |
| gvr::Mat4f PerspectiveMatrixFromView(const gvr::Rectf& fov, |
| float z_near, |
| float z_far) { |
| @@ -98,6 +359,39 @@ gvr::Recti CalculatePixelSpaceRect(const gvr::Sizei& texture_size, |
| return result; |
| } |
| +/** |
| + * Provides the direction the head is looking towards as a 3x1 unit vector. |
| + * </p><p> |
| + * Note that in OpenGL the forward vector points into the -Z direction. |
| + * Make sure to invert it if ever used to compute the basis of a right-handed |
| + * system. |
| + * |
| + */ |
| +gvr::Vec3f getForwardVector(gvr::Mat4f matrix) { |
| + gvr::Vec3f forward; |
| + float* fp = &forward.x; |
| + // Same as multiplying the inverse of the rotation component of the matrix by |
| + // (0, 0, -1, 0). |
| + for (int i = 0; i < 3; ++i) { |
| + fp[i] = -matrix.m[2][i]; |
| + } |
| + return forward; |
| +} |
| + |
| +/** |
| + * Provides the relative translation of the head as a 3x1 vector. |
| + * |
| + */ |
| +gvr::Vec3f getTranslation(gvr::Mat4f matrix) { |
| + gvr::Vec3f translation; |
| + float* tp = &translation.x; |
| + // Same as multiplying the matrix by (0, 0, 0, 1). |
| + for (int i = 0; i < 3; ++i) { |
| + tp[i] = matrix.m[i][3]; |
| + } |
| + return translation; |
| +} |
| + |
| GLuint CompileShader(GLenum shader_type, |
| const GLchar* shader_source, |
| std::string& error) { |
| @@ -115,7 +409,7 @@ GLuint CompileShader(GLenum shader_type, |
| GLint info_log_length; |
| glGetShaderiv(shader_handle, GL_INFO_LOG_LENGTH, &info_log_length); |
| GLchar* str_info_log = new GLchar[info_log_length + 1]; |
| - glGetShaderInfoLog(shader_handle, info_log_length, NULL, str_info_log); |
| + glGetShaderInfoLog(shader_handle, info_log_length, nullptr, str_info_log); |
| error = "Error compiling shader: "; |
| error += str_info_log; |
| delete[] str_info_log; |
| @@ -163,7 +457,8 @@ GLuint CreateAndLinkProgram(GLuint vertext_shader_handle, |
| glGetProgramiv(program_handle, GL_INFO_LOG_LENGTH, &info_log_length); |
| GLchar* str_info_log = new GLchar[info_log_length + 1]; |
| - glGetProgramInfoLog(program_handle, info_log_length, NULL, str_info_log); |
| + glGetProgramInfoLog(program_handle, info_log_length, nullptr, |
| + str_info_log); |
| error = "Error compiling program: "; |
| error += str_info_log; |
| delete[] str_info_log; |
| @@ -175,4 +470,78 @@ GLuint CreateAndLinkProgram(GLuint vertext_shader_handle, |
| return program_handle; |
| } |
| +float VectorLength(const gvr::Vec3f& vec) { |
| + return sqrt(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z); |
| +} |
| + |
| +void NormalizeVector(gvr::Vec3f& vec) { |
| + float len = VectorLength(vec); |
| + vec.x /= len; |
| + vec.y /= len; |
| + vec.z /= len; |
| +} |
| + |
| +float VectorDot(gvr::Vec3f& a, gvr::Vec3f& b) { |
| + return a.x * b.x + a.y * b.y + a.z * b.z; |
| +} |
| + |
| +void NormalizeQuat(gvr::Quatf& quat) { |
| + float len = sqrt(quat.qx * quat.qx + quat.qy * quat.qy + quat.qz * quat.qz + |
| + quat.qw * quat.qw); |
| + quat.qx /= len; |
| + quat.qy /= len; |
| + quat.qz /= len; |
| + quat.qw /= len; |
| +} |
| + |
| +gvr::Quatf QuatFromAxisAngle(float x, float y, float z, float angle) { |
| + gvr::Quatf res; |
| + float s = sin(angle / 2); |
| + res.qx = x * s; |
| + res.qy = y * s; |
| + res.qz = z * s; |
| + res.qw = cos(angle / 2); |
| + return res; |
| +} |
| + |
| +gvr::Quatf QuatMultiply(const gvr::Quatf& a, const gvr::Quatf& b) { |
| + gvr::Quatf res; |
| + res.qw = a.qw * b.qw - a.qx * b.qx - a.qy * b.qy - a.qz * b.qz; |
| + res.qx = a.qw * b.qx + a.qx * b.qw + a.qy * b.qz - a.qz * b.qy; |
| + res.qy = a.qw * b.qy - a.qx * b.qz + a.qy * b.qw + a.qz * b.qx; |
| + res.qz = a.qw * b.qz + a.qx * b.qy - a.qy * b.qx + a.qz * b.qw; |
| + return res; |
| +} |
| + |
| +gvr::Mat4f QuatToMatrix(const gvr::Quatf& quat) { |
| + // const float x = quat.qx; |
| + const float x2 = quat.qx * quat.qx; |
| + // const float y = quat.qy; |
| + const float y2 = quat.qy * quat.qy; |
| + // const float z = quat.qz; |
| + const float z2 = quat.qz * quat.qz; |
| + // const float w = quat.qw; |
| + const float xy = quat.qx * quat.qy; |
| + const float xz = quat.qx * quat.qz; |
| + const float xw = quat.qx * quat.qw; |
| + const float yz = quat.qy * quat.qz; |
| + const float yw = quat.qy * quat.qw; |
| + const float zw = quat.qz * quat.qw; |
| + |
| + const float m11 = 1.0f - 2.0f * y2 - 2.0f * z2; |
| + const float m12 = 2.0f * (xy - zw); |
| + const float m13 = 2.0f * (xz + yw); |
| + const float m21 = 2.0f * (xy + zw); |
| + const float m22 = 1.0f - 2.0f * x2 - 2.0f * z2; |
| + const float m23 = 2.0f * (yz - xw); |
| + const float m31 = 2.0f * (xz - yw); |
| + const float m32 = 2.0f * (yz + xw); |
| + const float m33 = 1.0f - 2.0f * x2 - 2.0f * y2; |
| + |
| + float ret[16] = {m11, m12, m13, 0.0f, m21, m22, m23, 0.0f, |
| + m31, m32, m33, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}; |
| + |
| + return *((gvr::Mat4f*)&ret); |
| +} |
| + |
| } // namespace vr_shell |