About LookAt() function

2019-09-04 14:32发布

问题:

I need to implement a quaternion for rotating a camera. Before implementing quaternion, I use LookAt(&eye, &at, &up) for expressing a camera coordinate (vpn, vup, ...).

vec3 eye = vec3(0.0,0.0,-10.0);
vec3 to = vec3(0.0,0.0,0.0); // initial
vec3 up = vec3(0.0,1.0,0.0);

vec3 d = to - eye;

and at display callback.

        m = LookAt(eye,to,up);

        glUniformMatrix4fv(ModelView,1,GL_TRUE,m);

and Add an rotation (it is still euclidean rotation, and keyboard interation)

        case 'a':
                temp = RotateX(1.0) * vec4(d,0);
                temp1 = RotateX(1.0) * vec4(up,0);
                d = vec3(temp.x, temp.y, temp.z);
                up = vec3(temp1.x, temp1.y, temp1.z);
                eye = to - d;
                break;
        case 'd':
                temp = RotateY(1.0) * vec4(d,0);
                temp1 = RotateY(1.0) * vec4(up,0);
                d = vec3(temp.x, temp.y, temp.z);
                up = vec3(temp1.x, temp1.y, temp1.z);
                eye = to - d;
                break;

So my question is, LookAt function is only making camera coordinate? Is there any rotation matrix to make camera coordinate? As you see I make rotate my camera by using some rotation not in LookAt, I will this rotation by using quaternion. However LookAt() uses some rotation, I will implement quaternion version of LookAt to avoid gimbal lock

回答1:

All what LookAt does is a translation T (so that the new origin is at the point eye) followed by a rotation R. The rotation is defined by building an orthonormal basis from the 3 vectors (direction defined by the vector from eye to center, the up vector directly specified, and the right vector which is defined to be perpendicular to both). The final transorm produced by LookAt would be R*T.

You can use LookAt without any gimbal lock problems if you specify your input vectors correctly, but you can also describe your camera by a position vector (defining T) and orientation quaternion (defining R), and wouldn't have to use LookAt at all.