/** * Creates a "look at" matrix for the given eye point. * * @param size The size of the matrix, minimum of 4 * @param eye The position of the camera * @param at The point that the camera is looking at * @param up The "up" vector * @return A rotational transform that corresponds to a camera looking at the given point */ public static MatrixNf createLookAt(int size, Vector3f eye, Vector3f at, Vector3f up) { if (size < 4) { throw new IllegalArgumentException("Minimum matrix size is 4"); } final Vector3f f = at.sub(eye).normalize(); up = up.normalize(); final Vector3f s = f.cross(up).normalize(); final Vector3f u = s.cross(f).normalize(); final MatrixNf mat = new MatrixNf(size); mat.set(0, 0, s.getX()); mat.set(0, 1, s.getY()); mat.set(0, 2, s.getZ()); mat.set(1, 0, u.getX()); mat.set(1, 1, u.getY()); mat.set(1, 2, u.getZ()); mat.set(2, 0, -f.getX()); mat.set(2, 1, -f.getY()); mat.set(2, 2, -f.getZ()); return mat.translate(eye.mul(-1).toVectorN()); }
/** * Creates a "look at" matrix for the given eye point. * * @param size The size of the matrix, minimum of 4 * @param eye The position of the camera * @param at The point that the camera is looking at * @param up The "up" vector * @return A rotational transform that corresponds to a camera looking at the given point */ public static MatrixNf createLookAt(int size, Vector3f eye, Vector3f at, Vector3f up) { if (size < 4) { throw new IllegalArgumentException("Minimum matrix size is 4"); } final Vector3f f = at.sub(eye).normalize(); up = up.normalize(); final Vector3f s = f.cross(up).normalize(); final Vector3f u = s.cross(f).normalize(); final MatrixNf mat = new MatrixNf(size); mat.set(0, 0, s.getX()); mat.set(0, 1, s.getY()); mat.set(0, 2, s.getZ()); mat.set(1, 0, u.getX()); mat.set(1, 1, u.getY()); mat.set(1, 2, u.getZ()); mat.set(2, 0, -f.getX()); mat.set(2, 1, -f.getY()); mat.set(2, 2, -f.getZ()); return mat.translate(eye.mul(-1).toVectorN()); }