39

Trying to understand gluLookAt, especially the last 3 parameters.

Can someone please explain ?

gluLookAt(camera[0], camera[1], camera[2], /* look from camera XYZ */ 
          0, 0, 0,  /* look at the origin */ 
          0, 1, 0); /* positive Y up vector */
  1. What exactly does it mean by "positive Y up vector" ?
  2. Is it possible to have the last up-vector 3 parameters as all 1s, e.g. 1, 1, 1 ? And, if it is possible, what exactly does it mean ?
  3. Is it possible for the up vector to have value more than 1, e.g. 2, 3, 4 ?

Thanks.

D A Vincent
  • 364
  • 2
  • 21
Johnnt Jazz
  • 391
  • 1
  • 3
  • 3

4 Answers4

74

Sketchup to the rescue!

GluLookAt params

Your image has an 'up' to it that can be separate from the world's up. The blue window in this image can be thought of as the 'near-plane' that your imagery is drawn on: your monitor, if you will. If all you supply is the eye-point and the at-point, that window is free to spin around. You need to give an extra 'up' direction to pin it down. OpenGL will normalize the vector that you supply if it isn't unit length. OpenGL will also project it down so that it forms a 90 degree angle with the 'z' vector defined by eye and at (unless you give an 'up' vector that is in exactly the same direction as the line from 'eye' to 'at'). Once 'in' (z) and 'up' (y) directions are defined, it's easy to calculate the 'right' or (x) direction from those two.

In this figure, the 'supplied' up vector is (0,1,0) if the blue axis is in the y direction. If you were to give (1,1,1), it would most likely rotate the image by 45 degrees because that's saying that the top of the blue window should be pointed toward that direction. Consequently the image of the guy would appear to be tipped (in the opposite direction).

JCooper
  • 6,395
  • 1
  • 25
  • 31
7

The "up vector" of gluLookAt is just the way the camera is oriented. If you have a camera at a position, looking directly at an object, there is one source of freedom still remaining: rotation. Imagine a camera pointed directly at an object, fixed in place. But, your camera can still rotate, spinning your image. The way OpenGL locks this rotation in place is with the "up vector."

Imagine (0, 0, 0) is directly at your camera. Now, the "up vector" is merely a coordinate around your camera. Once you have the "up vector," though, OpenGL will spin your camera around until directly the top of your camera is facing the coordinate of the "up vector".

If the "up vector" is at (0, 1, 0), then your camera will point normally, as the up vector is directly above the camera, so the top of the camera will be at the top, so your camera is oriented correctly. Move the up vector to (1, 1, 0), though, and in order to point the top of the camera to the "up vector," your camera will need to rotate be 45 degrees, rotating the entire image by 45 degrees.

This answer was not meant as an in-depth tutorial, but rather as a way to grasp the concept of the "up vector," to help better understand the other excellent answers to your question.

User 12692182
  • 927
  • 5
  • 16
6

first 3 parameters are camera position next 3 parameters are target position the last 3 parameters represent the rolling of camera.

Very important thing use gluLookAt after "glMatrixMode(GL_MODELVIEW);"

Other useful hint always specify it 0,0,1 for last 3 parameters. In general co-ordinates are written as x,y,z. z is up vector. the 0,1,0 leads in confusion as x,z,y.

so best thing leave it to 0,0,1.

Fennekin
  • 216
  • 3
  • 12
2

the last vector, also known as the cameras up-vector defines the orientation of the camera.

imagine a stick attached to the top of a "real" camera. the stick's direction is the up-vector.

by changing it from (0,1,0) you can do sideways rolling.

clamp
  • 33,000
  • 75
  • 203
  • 299
  • Still don't quite understand. What is the diff between up-vector when the parameters are (1, 0, 0) vs (0, 1,0) vs (0, 0, 1) ? Can't find any picture in the web site to aid my understanding. If anybody know where to find, please let me know. – Johnnt Jazz Apr 19 '11 at 14:16
  • 1
    @Johnnt: Think about an observer at a particular spot on your floor, looking toward the door. Now, that observer could be sitting, lying on their left side, or lying on their right side. You need to know the rotation of the observer's head in addition to their position and the direction they're looking. – Ben Voigt Apr 19 '11 at 14:36
  • 3
    Expanding on Ben's explanation, imagine your camera with an antenna on top. That's camera "up." As a hand-held video camera, imagine that you ROTATE THE CAMERA while keeping it pointed at your subject! Now camera-up can point in any direction, even upside down! That's what gluLookAt's up XYZ coordinates are for -- to tell GL which way the camera is being held. In MOST situations, 0,1,0 works, although there are problems if the camera is looking straight up or straight down, in which case 0,0,1 is a good starting point, though you may want to experiment. – Olie May 30 '11 at 14:45