gluPerspective,glViewport,gluLookAt以及GL_PROJECTION和GL_MODELVIEW Matricies

use*_*501 8 opengl mode matrix glulookat

原始问题

我想使用'gluPerspective','glViewport'和'gluLookAt'来操纵我的相机和屏幕.

哪个函数适用于哪种矩阵模式?我应该以什么顺序使用它们?

例如,我试图像这样设置我的屏幕和相机:(但它不起作用!)

glMatrixMode(GL_PROJECTION) // Apply following to projection matrix - is this correct?
glLoadIdentity(); // Reset first
glPerspective(45.0, (double)w/(double)h, 1.0, 200.0); // Set perspective
glViewport(0, 0, w, h); // Set viewport

glMatrixMode(GL_MODELVIEW); // Apply following to modelview - should glViewport come under here?
glLoadIdentity(); // Reset first
gluLookAt(px, py, pz, cx, cy, cz, ux, uy, uz); // Set position, centre and then up vectors
// This surely comes after calling GL_MODELVIEW?
Run Code Online (Sandbox Code Playgroud)

我一直在寻找在线文档,我理解这些功能,而不是它们应该去的地方以及它们的顺序!

一段时间以后...

现在几个月后,我正在添加一个快速编辑,以显示我用于使用OpenGL渲染事物的系统.这是为了帮助将来看到这个问题的其他人.

我主要使用两种方法.

方法1:

此方法将所有内容组合在一

// Firstly, the window may have been resized so re-create the viewing area
glViewport(0, 0, width_of_window_rendering_area, height_of_window_rendering area);
Run Code Online (Sandbox Code Playgroud)

这将重新创建视口,以便在窗口内部的整个区域上进行渲染.使用sfml,你会做类似window.width()或window.height()或类似的东西,这取决于你使用的窗口工具包(glut,glfw,sdl等)......

// The second step is to add a projection matrix. There are three main ones I like to use
// Uncomment the one you want
glMatrixMode(GL_PROJECTION); // Tell OpenGL to manipulate the correct matrix stack
glLoadIdentity(); // Reset the projection matrix, or bad things happen after multiple calls to below functions!
// glOrtho( ... ) // Uncomment to use 2D rendering
// gluPerspective( ... ) // Uncomment to use (easy) 3D rendering
glFrustrum( ... ) // Uncomment to use (harder/less intuitive?) 3D rendering
glMatrixMode(GL_MODELVIEW); // I always prefer to leave OpenGL in the modelview manipulation mode.
    // I consider it the "default" and safest mode to leave OpenGL in, as any rogue
    // calls to functions changing its contents is likely to mess up your geometry
    // which should be visible as a problem on the screen, which tells you you need
    // to fix something! Manipulating the other matrix stacks may not show obvious
    // problems.
Run Code Online (Sandbox Code Playgroud)

您需要从'glOrtho','gluPerspective'和'glFrustrum'中选择三个中的一个.还有'gluOrtho2D',但要小心使用!(我更喜欢用'glOrtho'来指定近距离和远距离飞机.)你需要用函数的参数替换'...'.

// The third step is to clear the screen and set your camera / geometry position
glClear(GL_COLOR_BUFFER_BIT); // use bitwise OR ('||') with 'GL_DEPTH_BUFFER_BIT' and 'GL_STENCIL_BUFFER_BIT' if required
gluLookAt( ... );
// I like to use gluLookAt, or at least I _italic_used to! Now I implement my own camera...
 // That is another fun thing you should try if you are comfortable with 3D geometry and hard math!

// The fourth step is to draw your scene. You may want to put lighting stuff first or in with the drawing
glutWireTeapot( ... );
Run Code Online (Sandbox Code Playgroud)

方法2:

第二种方法与上面相同,但是将第一步移到单独的函数中.然后,您必须检测窗口调整大小事件并调用此函数.有了过剩,您可以指定回调.使用SFML,您可以在调整窗口大小时检测事件.我忘了SDL是如何工作的,但它是相似的.我还没有学会glfw是如何工作的.

希望这会对你有所帮助.

一些OpenGL新手(可能一次包括我自己)尝试在PROJECTION矩阵上指定摄像机翻译.不要这样做 - 我把它弄乱了灯光和其他东西.

Max*_*Max 3

我将重塑回调函数定义为:

glViewport(...)一开始就打电话一次。

然后用单位矩阵重新加载投影矩阵:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
Run Code Online (Sandbox Code Playgroud)

然后:

glPerspective(...)
glMatrixMode(GL_MODELVIEW);
Run Code Online (Sandbox Code Playgroud)

gluLookAt(...)如果您需要更改相机位置,可以随时致电。

适合我的简单目的。