如何调整GLUT窗口的大小? [英] How to resize a GLUT window?

查看:287
本文介绍了如何调整GLUT窗口的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

from OpenGL.extensions import alternate
from OpenGL.GL import *
from OpenGL.GL.ARB.multitexture import *
from OpenGL.GLU import *
from OpenGL.GLUT import *


class TestTexture():

    def __init__(self):
        self.window_width = 800
        self.window_height = 800

    def init(self):
        glClearColor(0.0, 0.0, 0.0, 0.0)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        glEnable(GL_TEXTURE_2D)

    def display(self):
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glLoadIdentity()

        glBegin(GL_TRIANGLES)
        glVertex3f(-1.0, 0.0, 0.0)
        glVertex3f(1.0, 0.0, 0.0)
        glVertex3f(0.0, 1.0, 0.0)
        glEnd()

        glFlush()
        glutSwapBuffers()

    def reshape(self, w, h):
        self.window_width = w
        self.window_height = h
        glViewport(0, 0, self.window_width, self.window_height)

    def animate(self):
        glutPostRedisplay()

    def visible(self, vis):
        if (vis == GLUT_VISIBLE):
            glutIdleFunc(self.animate)
        else:
            glutIdleFunc(0)

    def key_pressed(self, *args):
        if args[0] == b"\x1b":
            sys.exit()

    def run(self):
        glutInit(sys.argv)
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
        glutInitWindowSize(self.window_width, self.window_height)
        glutInitWindowPosition(800, 100)
        glutCreateWindow(b'MCVE')
        glutDisplayFunc(self.display)
        glutReshapeFunc(self.reshape)
        glutIdleFunc(self.animate)
        glutVisibilityFunc(self.visible)
        glutKeyboardFunc(self.key_pressed)

        self.init()
        glutMainLoop()

if __name__ == "__main__":
    TestTexture().run()

我已经尝试了一些方法来适当调整窗口的大小和刷新,但是到目前为止还没有运气.调整窗口大小时,应该实时正确渲染场景,但不能正确渲染,而是仅在释放鼠标(未调用reshape函数)时或者在调整大小时才更新场景.但是最终结果绝对不是很酷.

I've already tried few things in order to resize&refresh properly this window but no luck so far. When the window is resized the scene should be rendered properly on real-time but it's not, instead the scene is updated only when you release the mouse (reshape function is not called) or maybe when it's being resized it gets a little chance to update but the final result is definitely not cool at all.

值得一提的是pyqt opengl应用正在调整大小,并且

Worth mentioning that a pyqt opengl app is resizing ok and the show window contents while dragging option is enabled.

已经在Windows7,PyOpenGL == 3.1.1和python3.5.1_x86上进行了测试

The test has been made on windows7, PyOpenGL==3.1.1 and python3.5.1_x86

问题是,如何实时正确地调整和刷新过剩窗口的大小?

So the question would be, how can I resize&refresh a glut window properly on realtime?

推荐答案

首先要做的事情glViewport不属于重塑处理程序(如果我每次写这篇文章时都有一分钱……).

First things first glViewport does not belong into the reshape handler (if I had a cent for every time I wrote this…).

您遇到的问题不是您可以从GLUT的外部"轻松解决的问题,因为它实质上可以归结为主循环内部实现的方式.为了在调整大小期间平滑地更新窗口的内容,主循环必须通过平滑地将调整大小事件与显示函数的调用交织在一起,以适应这种情况. glutPostRedisplay标记要重绘的主循环的方式将每隔几步调整大小步骤就会调用显示函数,但可能会伴随闪烁和抖动的重绘.

The problem you're running into is not something you can easily address from the "outside" of GLUT because it essentially boils down to how the innards of the main loop are implemented. For smooth updates of the window's contents during resize technically the main loop has to accommodate for this situation, by smoothly interleaving resize events with calls of the display function. The way glutPostRedisplay flags the main loop for redraw will call the display function every few resize steps, but it may accompanied by flicker and jerky redraws.

您最好的选择是做通常不愿做的事情:在调整大小处理程序的末尾调用显示函数(包括缓冲区交换),不要调用glutPostRedisplay.但是,这可能仍然容易闪烁,具体取决于WSI背景擦除的实现方式.这样做的问题是,调整大小事件可能会排长队,并且调整大小的步骤很长一段时间就会显示出来,从而滞后于用户操作.

Your best bet is to do something that's normally frowned upon: Call the display function (including buffer swap) at the end of the resize handler and do not call glutPostRedisplay. However this might be still prone to flicker, depending on how WSI background erasure is implemented. The problem with that is, that resize events may queue up and resize steps long begone show up lagging behind user action.

要真正平滑地进行尺寸调整更新,需要编写适当的主循环,以合并输入/调整大小事件,以避免出现队列滞后问题,并允许进行按尺寸调整的绘图操作而无需自动擦除背景以支持平滑更新.当前的GLUT实现无法做到这一点.

For truly smooth resize updates an appropriately written main loop is required, that coalesces input/resize events to avoid queue-lag issues and allows for on-resize drawing operation without automatic background erasure to support smooth updates. Current implementations of GLUT don't do this.

这篇关于如何调整GLUT窗口的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆