PDA

View Full Version : How to locate camera facing down with gluLookAt equivalent?

theateist
08-01-2016, 09:00 AM
My world is a field. The coordinates and the axis are described in the image below:

2282

I want to locate a camera above the field, at location C=(W/2,H/2,-Z1), facing down. The extrinsic matrix RT that I should get for this is:

(1, 0, 0, W/2)
(0, 1, 0, H/2)
(0, 0, 1, Z1 )
(0, 0, 0, 1 )

I want to get the RT using gluLookAt equivalent method described here. I had to modify vz calculation by adding minus since I locate the camera along the -Z axis and not +Z axis.

static Mat4x4 lookAt(Vec3 eye, Vec3 target, Vec3 up)
{
// the vector to the target is in the negative z direction.
Vec3 vz = -(target - eye).normalize();
Vec3 vx = vnl_cross_3d(up, vz).normalize();
Vec3 vy = vnl_cross_3d(vz, vx);

Mat4x4 inverseViewMatrix;
inverseViewMatrix.set_row(0, Vec4( vx[0], vy[0], vz[0], eye[0] ));
inverseViewMatrix.set_row(1, Vec4( vx[1], vy[1], vz[1], eye[1] ));
inverseViewMatrix.set_row(2, Vec4( vx[2], vy[2], vz[2], eye[2] ));
inverseViewMatrix.set_row(3, Vec4( 0 , 0 , 0 , 1 ));

Mat4x4 result = vnl_inverse(inverseViewMatrix);
return result;
}

To get the desired RT here are the eye, target and up that I define:

Vec3 eye(W/2,H/2,-Z1); //location relative to world left-top (0,0,0)
Vec3 target(W/2,H/2,0); //location of point on field relative to world left-top (0,0,0)
Vec3 up(0,-1,0);
RT = lookAt(eye, target, up);
But, the RT I get is not the one that I should get (the one that described above). What eye, target and up I should define to the desired RT?

john_connor
08-01-2016, 09:32 AM
I want to locate a camera above the field, at location C=(W/2,H/2,-Z1), facing down.
...

What eye, target and up I should define to the desired RT?

eye = position where the camera is located: vec3(W/2,H/2,-Z1)
target = where you want to look at, e.g. vec3(0, 0, 0) would be the scenes origin
up = the cameras upward direction, vec3(0, 1, 0) is always a good choice, except you are looking exactly downwards vec3(0, -N, 0)

there is a math library for openGL, using it would save you some time you would otherwise spend searching for math mistakes:
http://glm.g-truc.net/0.9.7/index.html

GClements
08-01-2016, 09:40 AM
My world is a field. The coordinates and the axis are described in the image below:
If you're using a conventional OpenGL projection transformation, the image is incorrect. The axis labelled "-Z" should be "+Z", as OpenGL conventionally uses right-handed coordinate systems for model space and eye space (clip space and NDC are left-handed due to the projection transformation having a negative determinant).

What eye, target and up I should define to the desired RT?
target=(W/2,H/2,0), eye=(W/2,H/2,Z1), up=(0,1,0).

Also, your lookAt() function is wrong. First, vx, vy and vz are the rows of the matrix, not the columns. Second, you can't just place the translation component in the right-hand column. You need to multiply the matrix formed by vx,vy,vz by a translation of -eye. In this particular case, it will mostly work as the left-hand matrix is the identity matrix, so the only issue is that eye needs to be negated.

See the description of gluLookAt (https://www.opengl.org/sdk/docs/man2/xhtml/gluLookAt.xml).

theateist
08-01-2016, 02:32 PM
Are eye and target vectors in world coordinate systems?

GClements
08-01-2016, 04:12 PM
Are eye and target vectors in world coordinate systems?
RT*(0,0,0,1) will be eye, RT*(0,0,Z,1) will be on the line through eye and target, RT*(0,Y,Z,1) will be on the plane through eye and target parallel to up, and RT*(X,0,0,1) will be on the line through eye perpendicular to that plane.

OpenGL doesn't have "world" coordinates. Modern OpenGL has clip coordinates and normalised device coordinates; anything else is up to the application. The fixed-function pipeline also has object coordinates (i.e. the original coordinates passed to glVertex() etc) and eye coordinates (the result of transforming object coordinates by the model-view matrix).

gluLookAt() concatenates the generated matrix with the current matrix (which one depends upon the glMatrixMode() setting, but it's usually the model-view matrix), so the vectors are in whichever coordinate system the current matrix represents prior to concatenating the look-at matrix.