Creating a lookAt matrix..

Have jReality programming problems or questions? Post them here.
Post Reply
kelano28
Posts: 8
Joined: Wed 1. Sep 2010, 20:26

Creating a lookAt matrix..

Post by kelano28 » Mon 20. Sep 2010, 22:39

I was wondering if there was any built in means of creating a lookAt matrix other than the P3 provided one. Right now I currently use:

Code: Select all

cameraNode.getTransformation().setMatrix(Rn.inverse(null, P3.makeLookatMatrix(null, pos, loc, 0.0, Pn.EUCLIDEAN)));
But unfortunately the up vector gets thrown off and my scene is rotated undesirably. I have also used MatrixBuilder to translate/rotate the camera but have achieved the same result.

I have also tried using some directX code I had to manually calculate the matrix myself, but this has not worked as the scene fails to render properly (I have to use encompass() to get it back to displaying properly, but that moves the camera).

Is there an easy way to tell P3 or a something else the desired up vector for a lookAt matrix? If not I will need to hard code the "Roll" value in P3.makeLookatMatrix() for now.

Any help is greatly appreciated!

User avatar
gunn
Posts: 323
Joined: Thu 14. Dec 2006, 09:56
Location: TU Berlin
Contact:

Re: Creating a lookAt matrix..

Post by gunn » Tue 21. Sep 2010, 13:08

I believe that you can use the fourth parameter to P3.makeLookatMatrix() to control the roll. Replace the 0.0 in your current code with an estimate of how much you need to rotate around the z-axis to get the desired up-vector to point up, you may have to reverse the sign, too.

Code: Select all

cameraNode.getTransformation().setMatrix(Rn.inverse(null, P3.makeLookatMatrix(null, pos, loc, 0.0, Pn.EUCLIDEAN)));
jReality core developer

kelano28
Posts: 8
Joined: Wed 1. Sep 2010, 20:26

Re: Creating a lookAt matrix..

Post by kelano28 » Tue 21. Sep 2010, 20:47

Thanks gunn for the response!

Unfortunately, as my post mentions, hard-coding roll values per camera position is undesirable for the task at hand. I am working on a semi-auto roll generator based on camera coordinates but this is also more of a hack than I would like.

If I do find something to fix this i'll be sure to post it back here in case.

Bram
Posts: 3
Joined: Fri 23. Jul 2010, 17:10

Re: Creating a lookAt matrix..

Post by Bram » Tue 5. Oct 2010, 17:12

I wanted to check if anyone else had any ideas about this. I'm running into the same problem, and really need a way to automatically specify a position for the camera to look at. Unless someone has a way to automatically calculate the roll, that won't work for me either.

Thanks!

ted
Posts: 57
Joined: Wed 25. Jul 2012, 22:53

Re: Creating a lookAt matrix..

Post by ted » Sat 28. Jul 2012, 05:21

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).

Post Reply