At first, you need understand Opengl Matrix Column Major storage way by reference
http://blog.csdn.net/fanbird2008/article/details/17524875
glulookat define a view matrix so that it can be used transform object from world space to view space.
if the Matrix defined by glulookat is m, some object's world coordinate vector is v, then its view coordinate will be mv.
注: 下面计算中的m已经是三个基向量的转置(逆 ), 因此从世界坐标空间(v)变换到眼睛空间就是mv.
The implementation of gluLookAt
from http://www.mesa3d.org
void GLAPIENTRY gluLookAt(GLdouble eyex, GLdouble eyey, GLdouble eyez, GLdouble centerx, GLdouble centery, GLdouble centerz, GLdouble upx, GLdouble upy, GLdouble upz) { float forward[3], side[3], up[3]; GLfloat m[4][4]; forward[0] = centerx - eyex; forward[1] = centery - eyey; forward[2] = centerz - eyez; up[0] = upx; up[1] = upy; up[2] = upz; normalize(forward); /* Side = forward x up */ cross(forward, up, side); normalize(side); /* Recompute up as: up = side x forward */ cross(side, forward, up); __gluMakeIdentityf(&m[0][0]); m[0][0] = side[0]; m[1][0] = side[1]; m[2][0] = side[2]; m[0][1] = up[0]; m[1][1] = up[1]; m[2][1] = up[2]; m[0][2] = -forward[0]; m[1][2] = -forward[1]; m[2][2] = -forward[2]; glMultMatrixf(&m[0][0]); glTranslated(-eyex, -eyey, -eyez); }
Q: why forward vector took negative direct?
A: Since centerx - eyex etc denotes direct of sight rather than camera coordinate system z-direct.
camera coordinate system z-direct is exactly opposite direct of direct of sight.