From dcc2e3c7912cbb082e214df04090733ec6342b09 Mon Sep 17 00:00:00 2001 From: Learn OpenGL ES Date: Thu, 19 Sep 2013 14:35:33 -0400 Subject: Refactor mat4x4_look_at to reuse internal functions and data types. --- linmath.h | 63 +++++++++++++++++++++------------------------------------------ 1 file changed, 21 insertions(+), 42 deletions(-) diff --git a/linmath.h b/linmath.h index 7333d9a..1c147a8 100644 --- a/linmath.h +++ b/linmath.h @@ -357,56 +357,35 @@ static inline void mat4x4_perspective(mat4x4 m, float y_fov_in_degrees, float as m[3][2] = -((2.0f * f * n) / (f - n)); m[3][3] = 0.0f; } -static inline void mat4x4_look_at(mat4x4 m, - float eyeX, float eyeY, float eyeZ, - float centerX, float centerY, float centerZ, - float upX, float upY, float upZ) +static inline void mat4x4_look_at(mat4x4 m, vec3 eye, vec3 center, vec3 up) { /* Adapted from Android's OpenGL Matrix.java. */ // See the OpenGL GLUT documentation for gluLookAt for a description // of the algorithm. We implement it in a straightforward way: + vec3 f; + vec3_sub(f, center, eye); + vec3_norm(f, f); + + vec3 s; + vec3_mul_cross(s, f, up); + vec3_norm(s, s); + + vec3 u; + vec3_mul_cross(u, s, f); - float fx = centerX - eyeX; - float fy = centerY - eyeY; - float fz = centerZ - eyeZ; - - // Normalize f - vec3 f_vec = {fx, fy, fz}; - float rlf = 1.0f / vec3_len(f_vec); - fx *= rlf; - fy *= rlf; - fz *= rlf; - - // compute s = f x up (x means "cross product") - float sx = fy * upZ - fz * upY; - float sy = fz * upX - fx * upZ; - float sz = fx * upY - fy * upX; - - // and normalize s - vec3 s_vec = {sx, sy, sz}; - float rls = 1.0f / vec3_len(s_vec); - sx *= rls; - sy *= rls; - sz *= rls; - - // compute u = s x f - float ux = sy * fz - sz * fy; - float uy = sz * fx - sx * fz; - float uz = sx * fy - sy * fx; - - m[0][0] = sx; - m[0][1] = ux; - m[0][2] = -fx; + m[0][0] = s[0]; + m[0][1] = u[0]; + m[0][2] = -f[0]; m[0][3] = 0.0f; - m[1][0] = sy; - m[1][1] = uy; - m[1][2] = -fy; + m[1][0] = s[1]; + m[1][1] = u[1]; + m[1][2] = -f[1]; m[1][3] = 0.0f; - m[2][0] = sz; - m[2][1] = uz; - m[2][2] = -fz; + m[2][0] = s[2]; + m[2][1] = u[2]; + m[2][2] = -f[2]; m[2][3] = 0.0f; m[3][0] = 0.0f; @@ -414,7 +393,7 @@ static inline void mat4x4_look_at(mat4x4 m, m[3][2] = 0.0f; m[3][3] = 1.0f; - mat4x4_translate_in_place(m, -eyeX, -eyeY, -eyeZ); + mat4x4_translate_in_place(m, -eye[0], -eye[1], -eye[2]); } typedef float quat[4]; -- cgit v1.2.3-54-g00ecf