Try this, and if it doesn't work, try its inverse. It's untested, so no guarantees
Code: Select all
import static de.jreality.softviewer.VecMat.*;
...
/*
* Unit vector transformation looking at the origin
* from a given eye location.
*
* To look some point other than the origin, adjust the
* eye location translate the eye first by that point.
*/
double[] lookAtOrigin(double[] eye, double[] upwards)
{
double[] dir=new double[3], left = new double [3], up = new double[3];
// dir = normalize(eye))
vecAssign(dir, eye);
normalize(dir);
// left = normalize(cross(normalize(upwards), dir)
vecAssign(up,upwards);
normalize(up);
cross(up,dir, left);
normalize(left);
// up = cross(dir, left);
cross(dir,left, up);
normalize(up);
// translate = {dot(-eye, left), dot(-eye.x, up), dot(-eye.x, dir)};
// transform = {
// { left.x, left.y, left.z, translate.x },
// { up.x, up,y, up.z, translate.y },
// { dir.x, dir.y, dir.z, translate.z },
// { 0, 0, 0, 1 }
// };
multiply(eye,-1.);
return new double[] {
left[0], left[1], left[2], dot(eye, left),
up[0], up[1], up[2], dot(eye, up),
dir[0], dir[1], dir[2], dot(eye, dir),
0., 0., 0., 1.
};
}
If it works, it might be good to add it to someplace like VecMat. I'll be needing it myself soon, as well.
_____________________________________________________________
I just noticed this: as well in
de.jreality.math.MatrixBuilder:
Code: Select all
MatrixBuilder rotateFromTo(double[] v1, double[] v2)
A rotation which takes vector v1 to vector v2.
The documentation isn't clear, but it may be the same thing, and this in
de.jreality.math.P3:
Code: Select all
static double[] makeLookatMatrix(double[] m, double[] from, double[] to, double roll, int metric)
Creates an isometry that carries the from vector to the origin; and takes the normalized to vector to the (homogeneous) vector (0,0,-1,0).