如何在 OpenGL 中使用 glOrtho()?
我无法理解 glOrtho
的用法.有人可以解释一下它的用途吗?
I can't understand the usage of glOrtho
. Can someone explain what it is used for?
是用来设置x y 和z 坐标范围限制的吗?
Is it used to set the range of x y and z coordinates limit?
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
表示x、y、z的范围是-1到1?
It means that the x, y and z range is from -1 to 1?
推荐答案
看看这张图:
glOrtho
命令生成一个倾斜"投影,您可以在底行看到.无论顶点在 z 方向上有多远,它们都不会后退.
The glOrtho
command produces an "Oblique" projection that you see in the bottom row. No matter how far away vertexes are in the z direction, they will not recede into the distance.
每次我需要在 OpenGL 中制作 2D 图形(例如健康栏、菜单等)时,我都会使用 glOrtho每次调整窗口大小时都使用以下代码:
I use glOrtho every time I need to do 2D graphics in OpenGL (such as health bars, menus etc) using the following code every time the window is resized:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, windowWidth, windowHeight, 0.0f, 0.0f, 1.0f);
这会将 OpenGL 坐标重新映射到等效的像素值(X 从 0 到 windowWidth 和 Y 从 0 到 windowHeight).请注意,我翻转了 Y 值,因为 OpenGL 坐标从窗口的左下角开始.所以通过翻转,我得到了一个更传统的 (0,0),而不是从窗口的左上角开始.
This will remap the OpenGL coordinates into the equivalent pixel values (X going from 0 to windowWidth and Y going from 0 to windowHeight). Note that I've flipped the Y values because OpenGL coordinates start from the bottom left corner of the window. So by flipping, I get a more conventional (0,0) starting at the top left corner of the window rather.
请注意,Z 值是从 0 到 1 裁剪的.因此,当您为顶点位置指定 Z 值时要小心,如果它落在该范围之外,它将被裁剪.否则,如果它在该范围内,除了 Z 测试之外,它似乎对位置没有影响.
Note that the Z values are clipped from 0 to 1. So be careful when you specify a Z value for your vertex's position, it will be clipped if it falls outside that range. Otherwise if it's inside that range, it will appear to have no effect on the position except for Z tests.
相关文章