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 if I use glm::lookAt all is good. This is my code:
mat4 Transform::lookAt(const vec3 &eye, const vec3 ¢er, const vec3 &up)
{
vec3 w(glm::normalize(eye - center)) ;
vec3 u(glm::normalize(glm::cross(up, w)));
vec3 v(glm::cross(w, u)) ;
mat4 ret = mat4 (
vec4 (u.x,v.x,w.x,0),
vec4 (u.y,v.y,w.y,0),
vec4 (u.z,v.z,w.z,0),
vec4 (-u.x*eye.x-u.y*eye.y-u.z*eye.z,
-v.x*eye.x-v.y*eye.y-v.z*eye.z,
-w.x*eye.x-w.y*eye.y-w.z*eye.z,
1)
);
return ret;
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.
来源:https://stackoverflow.com/questions/19740463/lookat-function-im-going-crazy