Search code examples
openglglulookat

OpenGL LookAt Confusion


I read online that if we want to directly compute the world to view matrix we can use OpenGl's LookAt function If you have the camera position, the position where the camera is looking at and an up-vector.

However, here is my confusion, the function takes in the following:

lookAt(const Vector3& vEye, const Vector3& vAt, const Vector3& vUp)

What would be the "vAt" vector be if my camera is simply looking into the positive z axis with some arbitrary z-buffer value? I know that it's tot ell you which point the camera is looking at but if I am looking just down the z axis and not at any particular point what would the vector be?


Solution

  • It seems to me you're getting confused about axis systems. A brief explanation:

    Because you do need a camera (or an eye, as you prefer) to see the world, everything must be defined in camera coordinates (or View Space, again as you prefer).

    Normally, world is defined in a right-handed axis system. This means that a vector in the Z axis is the cross product of vectors in X and Y axis: Vz = Vx x Vy. If you change the order (Vz' = Vy x Vx) then you get a left-handed axis system, i.e., Vz' = - Vz

    The View Space is normally defined in an also right-handed coordinate system. Defining the X axis towards your right and the Y axis towards your up, makes the Z axis grows towards your back (or behind the camera, as you prefer). This means that everything you can see is in front of you, with a negative Z coordinate in your view space coordinate system, not in the world one.
    It doesn't matter at all. All you need is a good transform matrix that takes this into account.

    In order to build the typical "lookAt" matrix, you need the position of the camera (because everything must be translated there), a "target" to define the direction of view (a vector towards the negative Z axis) and an up-vector to define the tilt, the angle of your head related to your shoulders. The matrix members are calculated using cross products of these vectors. It can be also obtained using rotations, but cross products include rotations and they are cheaper.

    The way this tipical "lookAt" matrix is built needs those three data (position, target, up) defined in world coordinates system.

    FINALLY YOUR QUESTION: Your camera is at (xc,yc,zc) and looking at a point in the positive Z axis. That's not enough. Any point at Z axis is (0,0,zp). Choose "zp" related to "zc": If you want the camera to look at world positive Z-axis direction, then you need zp > zc. It doesn't matter how big "zp" is. Well, apart from numeric issues.

    Remember: everything in world coordinate system.