LookAt function: I'm going crazy

后端 未结 1 621
清酒与你
清酒与你 2021-01-12 07:15

I must do a homework and I try to implement a lookAt function. I tried in many ways but the only result I got is a blue screen. The rest of my program works greatly, infact

1条回答
  •  一生所求
    2021-01-12 07:50

    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.

    0 讨论(0)
提交回复
热议问题