/// <summary> /// Build a look at view matrix. /// </summary> /// <param name="eye">The eye.</param> /// <param name="center">The center.</param> /// <param name="up">Up.</param> /// <returns></returns> public static mat4 lookAt(vec3 eye, vec3 center, vec3 up) { vec3 forward = (center - eye).normalize(); vec3 right = forward.cross(up).normalize(); vec3 standardUp = right.cross(forward); mat4 Result = new mat4(1); // Left (X) Axis Result[0, 0] = right.x; Result[0, 1] = standardUp.x; Result[0, 2] = -forward.x; // Up (Y) Axis Result[1, 0] = right.y; Result[1, 1] = standardUp.y; Result[1, 2] = -forward.y; // Forward (Z) Axis Result[2, 0] = right.z; Result[2, 1] = standardUp.z; Result[2, 2] = -forward.z; // Translation Result[3, 0] = -right.dot(eye); // dot(s, eye); Result[3, 1] = -standardUp.dot(eye); // dot(u, eye); Result[3, 2] = forward.dot(eye); // dot(f, eye); return(Result); }
/// <summary> /// Build a look at view matrix. /// </summary> /// <param name="eye">The eye.</param> /// <param name="center">The center.</param> /// <param name="up">Up.</param> /// <returns></returns> public static mat4 lookAt(vec3 eye, vec3 center, vec3 up) { vec3 f = (center - eye); f.Normalize(); vec3 s = f.cross(up); s.Normalize(); vec3 u = s.cross(f); mat4 Result = new mat4(1); Result[0, 0] = s.x; Result[1, 0] = s.y; Result[2, 0] = s.z; Result[0, 1] = u.x; Result[1, 1] = u.y; Result[2, 1] = u.z; Result[0, 2] = -f.x; Result[1, 2] = -f.y; Result[2, 2] = -f.z; Result[3, 0] = -s.dot(eye); // dot(s, eye); Result[3, 1] = -u.dot(eye); // dot(u, eye); Result[3, 2] = f.dot(eye); // dot(f, eye); return(Result); }