简体   繁体   中英

How to rotate the camera view around a point in OpenGL with gluLookAt

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?

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM