0

I’m trying to use the gluLookAt function to set up the correct view transformation for the camera’s position and orientation. In my program there is a sphere centered in the origin and when the arrows key are pressed the camera moves according (left, right, up and down) around the sphere.

I tried to use this transformation: gluLookAt(xc, yc, zc, 0.0, 0.0, 0.0, 0.0, 1,0, 0,0);

To verify its correctness I printed out the three axes. It doesn’t seem to properly work. When I move up and down the y axis doesn’t seem to move.

Can you help understand what is wrong with that transformation?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982

1 Answers1

0

Constant up vector is wrong !

When your xc=0,yc=?,zc=0 (which is when you move up/down in y axis) then your up vector is (anti)parallel to your view direction which means gluLookAt can not create 3 perpendicular basis vectors using cross product which it needs for constructing the transform matrix.

In such case the up vector should be different ... I usually use dot product

if (dot(a,b)/(|a|.|b|)>0.9)

to detect near to (anti)parallel vectors and set different one in your case its simplied like this:

if (fabs(yc*yc)/(xc*xc+yc*yc+zc*zc)>(0.9*0.9)) use different up vector

You can use (+/-1,0,0) or (0,0,+/-1) which one and which sign depends on your coordinate system conventions (so your view does not twist/mirror/rotate on going through this singularity).

The 0.9 threshold means:

cos(da) = 0.9
da = acos(0.9)
da = 25.84 deg

which is the max allowed angular difference between the two vectors ...

Spektre
  • 49,595
  • 11
  • 110
  • 380