在 OpenGL 中,如何调整正在调整大小的窗口?
我正在绘制几个与窗口高度和窗口高度无关的形状(例如圆形)。宽度。由于窗口始终以给定的大小开始,因此它们可以正确绘制,但是当调整窗口大小时,宽高比会变得混乱。
无论窗口大小如何,如何正确绘制形状?
I am drawing several shapes (such as circles) that are keyed off of the window height & width. Since the window always starts at a given size, they are drawn correctly, but when the window is resized, it messes the aspect ratio up.
How can I draw the shapes properly, regardless of window size?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您绝对不想让对象的大小显式依赖于窗口大小。
正如 genpfault 已经建议的那样,只要窗口大小发生变化,就调整投影矩阵。
调整窗口大小时要做的事情:
调整视口
调整剪刀矩形(仅当您启用
GL_SCISSOR_TEST
时)调整投影矩阵
如果是旧版(固定功能管道)OpenGL,您可以通过以下方式完成:
或
或
(假设
left、right、bottom
和top
都相等并且ratio=width/height
)You definitely don't want to make the size of your objects explicitly dependent on the window size.
As already suggested by genpfault, adjust your projection matrix whenever the window size changes.
Things to do on window resize:
Adjust viewport
Adjust scissor rectangle (only if you have
GL_SCISSOR_TEST
enabled)Adjust projection matrix
In case of legacy (fixed function pipeline) OpenGL, you can do it the following ways:
or
or
(assuming that
left, right, bottom
andtop
are all equal andratio=width/height
)如果您使用类似 gluPerspective() 的东西,只需使用窗口宽度/高度比:
If you're using something like gluPerspective() just use the window width/height ratio:
您应该设置某种窗口处理函数,每当调整 OpenGL 窗口大小时都会调用该函数。当aspectRatio > 时,您需要处理这种情况。 1,并且分别当aspectRatio <= 1时。不这样做可能会导致屏幕调整大小后几何体掉落到屏幕外。
You should setup some sort of window handler function that is called whenever your OpenGL window is resized. You need to take care of the case when aspectRatio > 1, and when aspectRatio <= 1 separately. Not doing so may result in geometry falling offscreen after a screen resize.
我也在学习opengl。我昨天遇到了这个问题,但我还没有找到正确的答案。今天,我已经解决了这个问题。在我的小程序中,当调整窗口大小改变宽度或高度时,对象会改变大小。这是我的代码(请原谅我的错误):
I am learning opengl too. I faced this problem yesterday and I have not found the correct answer. Today, I have solve the problem. In my little program, the object changes size when resizing the window changing width or heigt. This is my code (please, forgive my errors):
有很多方法可以做到这一点。
以下示例向您展示了我在项目中的做法及其工作原理。
该示例仅向您展示了一个矩形框,当窗口调整大小时,该矩形框不会更改其大小。你可以直接复制粘贴到你的项目中(考虑到我是openGl初学者)
There are many methods to do that.
Following example shows you how I'm doing it in my projects and its working.
The example shows you simply a rectangle box which do not changes it size when window is resizing. You can directly copy and paste it in your project(consider that i'm a openGl beginner)