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()?
基础教程推荐
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 从 std::cin 读取密码 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01