8

原始问题

我想使用“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?

我四处寻找在线文档,我了解这些功能,只是不知道它们应该去哪里以及以什么顺序!

一段时间以后...

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

我主要使用两种方法。

方法一:

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

// 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);

这将重新创建用于在窗口内部的整个区域进行渲染的视口。使用 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.

您需要从“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( ... );

方法二:

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

希望这会帮助你。

一些 OpenGL 新手(可能我曾一度包括在内)尝试在 PROJECTION 矩阵上指定相机平移。不要这样做- 我认为它会弄乱照明和其他可能的东西。

4

1 回答 1

3

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

glViewport(...)在开始时调用一次。

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

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

然后:

glPerspective(...)
glMatrixMode(GL_MODELVIEW);

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

适用于我的简单目的。

于 2012-10-24T16:34:48.833 回答