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