Results 1 to 5 of 5

Thread: How to locate camera facing down with gluLookAt equivalent?

  1. #1
    Newbie Newbie
    Join Date
    Aug 2016
    Posts
    2

    How to locate camera facing down with gluLookAt equivalent?

    My world is a field. The coordinates and the axis are described in the image below:

    Nrcrq.jpg

    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:
    Code :
    (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.
    Code :
    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:
    Code :
    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?

  2. #2
    Senior Member Regular Contributor
    Join Date
    May 2016
    Posts
    477
    Quote Originally Posted by theateist View Post
    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

  3. #3
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,105
    Quote Originally Posted by theateist View Post
    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).

    Quote Originally Posted by theateist View Post
    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.

  4. #4
    Newbie Newbie
    Join Date
    Aug 2016
    Posts
    2
    Are eye and target vectors in world coordinate systems?

  5. #5
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,105
    Quote Originally Posted by theateist View Post
    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.

Similar Threads

  1. Billboard facing the camera has wrong rotation near 180 degrees
    By Geosearchef in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 05-18-2015, 01:03 AM
  2. Camera Rotation Using gluLookAt
    By Reaperc89 in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 10-19-2011, 09:15 PM
  3. camera as object and gluLookAt
    By akashiraffee in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 02-28-2009, 03:25 PM
  4. gluLookAt equivalent for Symbian?
    By miranda2112 in forum OpenGL ES
    Replies: 7
    Last Post: 04-06-2005, 02:14 PM
  5. newbie question: camera isn't facing correctly?
    By in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 12-26-2003, 08:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean