C++ 如何在 OpenGL 中使用 glOrtho()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2571402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use glOrtho() in OpenGL?
提问by ufk
I can't understand the usage of glOrtho
. Can someone explain what it is used for?
我无法理解glOrtho
. 有人可以解释一下它的用途吗?
Is it used to set the range of x y and z coordinates limit?
是用来设置xy和z坐标范围限制的吗?
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
It means that the x, y and z range is from -1 to 1?
这意味着 x、y 和 z 的范围是从 -1 到 1?
回答by Mikepote
Have a look at this picture: Graphical Projections
看看这张图片:图形投影
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.
该glOrtho
命令会生成您在底行看到的“倾斜”投影。无论顶点在 z 方向上有多远,它们都不会后退到远处。
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:
每次调整窗口大小时,我都会使用 glOrtho 使用以下代码在 OpenGL 中执行 2D 图形(例如健康栏、菜单等):
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, windowWidth, windowHeight, 0.0f, 0.0f, 1.0f);
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.
这会将 OpenGL 坐标重新映射到等效的像素值(X 从 0 到 windowWidth 和 Y 从 0 到 windowHeight)。请注意,我翻转了 Y 值,因为 OpenGL 坐标从窗口的左下角开始。所以通过翻转,我得到了一个更传统的 (0,0),而不是从窗口的左上角开始。
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.
请注意,Z 值是从 0 到 1 裁剪的。因此,当您为顶点位置指定 Z 值时要小心,如果它落在该范围之外,它将被裁剪。否则,如果它在该范围内,除了 Z 测试之外,它似乎对位置没有影响。
回答by ChrisF
glOrtho describes a transformation that produces a parallelprojection. The current matrix (see glMatrixMode) is multiplied by this matrix and the result replaces the current matrix, as if glMultMatrix were called with the following matrix as its argument:
glOrtho 描述了一种产生平行投影的变换。当前矩阵(参见 glMatrixMode)乘以这个矩阵,结果替换当前矩阵,就好像 glMultMatrix 是用以下矩阵作为参数调用的:
OpenGL documentation(my bold)
OpenGL 文档(我的粗体)
The numbers define the locations of the clipping planes (left, right, bottom, top, near and far).
这些数字定义了剪切平面的位置(左、右、下、上、近和远)。
The "normal" projection is a perspective projection that provides the illusion of depth. Wikipediadefines a parallel projection as:
“正常”投影是提供深度错觉的透视投影。维基百科将平行投影定义为:
Parallel projections have lines of projection that are parallel both in reality and in the projection plane.
Parallel projection corresponds to a perspective projection with a hypothetical viewpoint—e.g., one where the camera lies an infinite distance away from the object and has an infinite focal length, or "zoom".
平行投影具有在现实和投影平面中平行的投影线。
平行投影对应于具有假设视点的透视投影——例如,相机与物体相距无限远并且具有无限焦距或“变焦”。