Search code examples
openglmatrixglulookat

gluLookAt() best usage, on GL_PROJECTION matrix or on GL_MODELVIEW matrix


The OpenGL documentation say that the gluLookAt() calls should be done on the GL_MODELVIEW matrix:

http://www.opengl.org/resources/faq/technical/viewing.htm

In fact the docs link to an article which say that using gluLookAt() on the GL_PROJECTION matrix is extremly bad:

Help stamp out GL_PROJECTION abuse

On the other hand, there are a lot of tutorials and examples where gluLookAt() is in fact called on the GL_PROJECTION matrix. And at the same time, while the article mentioned above does say to NOT use it on the GL_PROJECTION matrix, it sais that in most such usages there's no visible problem from doing so.

My questions is then: what's the best way to use gluLookAt() in real life projects? Is it really that much of a no no to use it on the GL_PROJECTION matrix?


Solution

  • The gluLookAt() function gives you a transformation matrix that transforms a rotation of an object in your scene. So this is clearly a model transformation and must go into the GL_MODELVIEW matrix.

    As for the why, this becomes clearer when you do it yourself in a vertex shader:

    While the position of the vertices are indeed just v.pos * modelview * projection, the modelview matrix is needed without the projection component for other calculations (for example lighting and fog). That's why the fixed function pipeline seperates these two matrices.

    So you should stick to the paradigms that are specified in the API and especially never trust any of the myriads of bad opengl "tutorials".