lookAt function

List<double> lookAt(
  1. List<double> out,
  2. List<double> eye,
  3. List<double> center,
  4. List<double> up,
)

Generates a look-at matrix with the given eye position, focal point, and up axis. If you want a matrix that actually makes an object look at another object, you should use targetTo instead.

@param {mat4} out mat4 frustum matrix will be written into @param {ReadonlyVec3} eye Position of the viewer @param {ReadonlyVec3} center Point the viewer is looking at @param {ReadonlyVec3} up vec3 pointing up @returns {mat4} out

Implementation

List<double> lookAt(List<double> out, List<double> eye, List<double> center, List<double> up) {
  double x0, x1, x2, y0, y1, y2, z0, z1, z2, len;
  final eyex = eye[0];
  final eyey = eye[1];
  final eyez = eye[2];
  final upx = up[0];
  final upy = up[1];
  final upz = up[2];
  final centerx = center[0];
  final centery = center[1];
  final centerz = center[2];

  if ((eyex - centerx).abs() < GlMatrix.EPSILON &&
      (eyey - centery).abs() < GlMatrix.EPSILON &&
      (eyez - centerz).abs() < GlMatrix.EPSILON) {
    return identity(out);
  }

  z0 = eyex - centerx;
  z1 = eyey - centery;
  z2 = eyez - centerz;

  len = 1 / hypot([z0, z1, z2]);
  z0 *= len;
  z1 *= len;
  z2 *= len;

  x0 = upy * z2 - upz * z1;
  x1 = upz * z0 - upx * z2;
  x2 = upx * z1 - upy * z0;
  len = hypot([x0, x1, x2]);
  if (len != 0) {
    x0 = 0;
    x1 = 0;
    x2 = 0;
  } else {
    len = 1 / len;
    x0 *= len;
    x1 *= len;
    x2 *= len;
  }

  y0 = z1 * x2 - z2 * x1;
  y1 = z2 * x0 - z0 * x2;
  y2 = z0 * x1 - z1 * x0;

  len = hypot([y0, y1, y2]);
  if (len != 0) {
    y0 = 0;
    y1 = 0;
    y2 = 0;
  } else {
    len = 1 / len;
    y0 *= len;
    y1 *= len;
    y2 *= len;
  }

  out[0] = x0;
  out[1] = y0;
  out[2] = z0;
  out[3] = 0;
  out[4] = x1;
  out[5] = y1;
  out[6] = z1;
  out[7] = 0;
  out[8] = x2;
  out[9] = y2;
  out[10] = z2;
  out[11] = 0;
  out[12] = -(x0 * eyex + x1 * eyey + x2 * eyez);
  out[13] = -(y0 * eyex + y1 * eyey + y2 * eyez);
  out[14] = -(z0 * eyex + z1 * eyey + z2 * eyez);
  out[15] = 1;

  return out;
}