How to use glOrtho() in OpenGL?(如何在 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.
这篇关于如何在 OpenGL 中使用 glOrtho()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 OpenGL 中使用 glOrtho()?


基础教程推荐
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 这个宏可以转换成函数吗? 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09