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

这一生的挚爱 提交于 2019-12-10 03:19:13

问题


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?


回答1:


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".




回答2:


I'm not sure I understand the question. Are you asking why the page tells you not to do something that only works "most" of the time?

It's really not that hard to understand. "Most such usages", by definition, means that there are some such usages do exhibit problems.

So you can either do the thing that will work sometimes and then break other times, or you can do the thing that will always work.

This is not a hard choice. Even moreso since doing it the right way means literally moving one line of code down a couple of lines.




回答3:


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?

Always use the modelview matrix to position the view point. This is even in the matrix' name model (i.e. placement of geometry) and view (placement of viewpoint).

The projection matrix is kind of the lens of the OpenGL "camera". You don't move your camera's lens, keeping the camera body in place to set the view, do you? Moving the lens has it's applications, like implementing tilt-/shift effects.



来源:https://stackoverflow.com/questions/9539796/glulookat-best-usage-on-gl-projection-matrix-or-on-gl-modelview-matrix

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