LookAt function: I'm going crazy

时光毁灭记忆、已成空白 提交于 2019-12-01 03:44:43

I saw you use the glm library for matrix operations, so from the glm code the lookat implementation looks like this:

mat4x4 lookAt(vec3  const & eye, vec3  const & center, vec3  const & up)
{
    vec3  f = normalize(center - eye);
    vec3  u = normalize(up);
    vec3  s = normalize(cross(f, u));
    u = cross(s, f);

    mat4x4 Result(1);
    Result[0][0] = s.x;
    Result[1][0] = s.y;
    Result[2][0] = s.z;
    Result[0][1] = u.x;
    Result[1][1] = u.y;
    Result[2][1] = u.z;
    Result[0][2] =-f.x;
    Result[1][2] =-f.y;
    Result[2][2] =-f.z;
    Result[3][0] =-dot(s, eye);
    Result[3][1] =-dot(u, eye);
    Result[3][2] = dot(f, eye);
    return Result;
}

You first normalize the vectors you will use(f is the direction you look at, u the up and s is the right vector). Then to make sure the up vector is perpendicular to the direction and right vectors you recalculate it as their cross product, because when you give an up vector you can't make sure its perpendicular to the eye-center vector(view direction), they're just form a plane which gives you the right vector.

The matrix is constructed from these. For more detail how does it works check the http://www.songho.ca/opengl/gl_transform.html page. In short:this is a matrix which creates you a new coordinate system, so the coloumns are the axises. Then at the last coloumn the translation matrix is applied.

(Look at the identity matrix:

AXIS     TRANSFORM
x  y  z  transl.
1, 0, 0, 0
0, 1, 0, 0,
0, 0, 1, 0
0, 0, 0, 1

This gives you the standard coordinate system with no translation.)

Then you multiply this with projection and model matrixes (p*v*m), the order is important. When you write your implementation make sure you use coloumn major matrixes, because of opengl, or transpose them.

I hope it helps.

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