lookAt method

Matrix4 lookAt(
  1. Vector3 eye,
  2. Vector3 target,
  3. Vector3 up
)

Implementation

Matrix4 lookAt(Vector3 eye, Vector3 target, Vector3 up) {
  final te = storage;

  _matrix4z.sub2(eye, target);

  if (_matrix4z.length2 == 0) {
    // eye and target are in the same position

    _matrix4z.z = 1;
  }

  _matrix4z.normalize();
  _matrix4x.cross2(up, _matrix4z);

  if (_matrix4x.length2 == 0) {
    // up and z are parallel

    if (up.z.abs() == 1) {
      _matrix4z.x += 0.0001;
    } else {
      _matrix4z.z += 0.0001;
    }

    _matrix4z.normalize();
    _matrix4x.cross2(up, _matrix4z);
  }

  _matrix4x.normalize();
  _matrix4y.cross2(_matrix4z, _matrix4x);

  te[0] = _matrix4x.x.toDouble();
  te[4] = _matrix4y.x.toDouble();
  te[8] = _matrix4z.x.toDouble();
  te[1] = _matrix4x.y.toDouble();
  te[5] = _matrix4y.y.toDouble();
  te[9] = _matrix4z.y.toDouble();
  te[2] = _matrix4x.z.toDouble();
  te[6] = _matrix4y.z.toDouble();
  te[10] = _matrix4z.z.toDouble();

  return this;
}