About LookAt() function

烂漫一生 提交于 2019-12-12 03:03:05

问题


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.



来源:https://stackoverflow.com/questions/16264698/about-lookat-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!