OpenGL - 正交投影矩阵,glViewport
我很难理解它们是如何工作的。首先,在 2d 游戏中,投影矩阵应该设置为正交,左、右、上、下与窗口匹配,对吗?但是当窗口大小调整时,我应该只改变 glViewport,而不是投影矩阵吗?如何保持纵横比?
有人可以在二维正交游戏中解释这两件事的用途,以便我可以更好地理解它吗?
感觉 OpenGL 在 2d 设置中做了很多无用的事情。当图像已经存在时,光栅化并计算片段,将顶点坐标转换为 NDC,只是为了通过 glViewport 转换回它们已经存在的位置。
另外,为什么在传统的免费 OpenGL 中我们必须制作自己的矩阵,而不是像 glViewport 那样我们自己的计算?
I'm having trouble wrapping my head around how these work. First, in a 2d game the projection matrix should be set up as ortho with left, right, top, bottom matching the window, right? But when the window resizes, should I just change glViewport, not the projection matrix? And how do I keep the aspect ratio?
Could someone explain the purposes of these two things, in 2d orthographical game, so that I can understand it better?
It feels like OpenGL is doing a lot of useless stuff in a 2d setup. Rasterizing and calculating fragments when the images are already there, converting vertex coordinates to NDC only to be converted back to what they already where by glViewport.
Also, how come in legacy free OpenGL we have to make our own matrices, but not our own calculations that glViewport does?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要混淆输入到 GL 的内容和输出到的内容。
用于计算正交矩阵的参数是与您的输入相关的参数。投影矩阵的输出始终是 [-1:1]x[-1:1]x[-1:1] 立方体。
视口将该立方体转换为渲染目标坐标。因此,这通常是您想要更改以匹配新窗口大小的内容(好吧,以及帧缓冲区本身)。
是的,GL 为 2D 渲染路径做了很多无用的事情。毕竟它是一个 3D API...最后
我要说的是,只要您的顶点着色器在我之前提到的立方体中输出,您就不必构建矩阵来执行 2D 变换。如果你想写视口的右上角,你总是可以直接将顶点作为 (0,0) (0,1) (1,0) (1,1) 传递,然后简单地输出它。
Don't confuse what you input to GL and what it outputs to.
the parameters you use to compute the Ortho matrix are the ones that correlate to your input. The output of a projection matrix is always the [-1:1]x[-1:1]x[-1:1] cube.
the viewport translates that cube to your render target coordinates. So that's typically what you want to change to match your new window size (well, that and the framebuffer itself).
Yes, GL does a lot of useless stuff for a 2D rendering path. It's a 3D API after all...
I'll finish by saying that you don't have to build matrices to do 2D transforms, so long as your vertex shader outputs in the cube I mentioned earlier. If you want to write the upper right corner of the viewport, you can always pass your vertices directly as (0,0) (0,1) (1,0) (1,1) and simply output that.
不确定您使用的 OpenGL 是什么语言,但如果您正在寻找 C++ 这个网站,http://nehe.gamedev .net/ 几乎涵盖了所有内容,并提供了从简单的“Hello World”一直到基本游戏和 3D 效果的教程。
希望有帮助
Not sure what language you are using OpenGL in, but if you are looking for C++ this site, http://nehe.gamedev.net/ pretty much covers everything and has tutorials from as simple as "Hello World" all the way up to basic games and 3d effects.
Hope it helps