简体   繁体   English

如何调整GLUT窗口的大小?

[英]How to resize a GLUT window?

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()

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. 当窗口调整大小时,场景应该实时正确渲染,但事实并非如此,只有当您释放鼠标时才更新场景(不调用重塑功能)或者当调整大小时,它会有一点机会更新但最终的结果绝对不是很酷。

Worth mentioning that a pyqt opengl app is resizing ok and the show window contents while dragging option is enabled. 值得一提的是,pyqt opengl应用程序正在调整ok和显示窗口内容,同时启用了拖动选项。

The test has been made on windows7, PyOpenGL==3.1.1 and python3.5.1_x86 测试是在windows7,PyOpenGL == 3.1.1和python3.5.1_x86上进行的

So the question would be, how can I resize&refresh a glut window properly on realtime? 所以问题是,我怎样才能实时调整大小和刷新过剩窗口?

First things first glViewport does not belong into the reshape handler (if I had a cent for every time I wrote this…). 首先,第一件事glViewport不属于重塑处理程序(如果我每次写这个都有一分钱......)。

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. 您遇到的问题不是您可以从GLUT的“外部”轻松解决的问题,因为它基本上归结为如何实现主循环的内部结构。 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标记重绘的主循环的方式将每隔几个调整大小步骤调用显示函数,但它可能伴随闪烁和不稳定的重绘。

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 . 最好的办法是做一些通常不赞成的事情:在调整大小处理程序结束时调用显示函数(包括缓冲区交换),不要调用glutPostRedisplay However this might be still prone to flicker, depending on how WSI background erasure is implemented. 然而,这可能仍然容易闪烁,这取决于WSI背景擦除的实现方式。 The problem with that is, that resize events may queue up and resize steps long begone show up lagging behind user action. 问题在于,调整大小事件可能会排队并调整大步长度,这些步骤显示滞后于用户操作。

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的当前实现不会这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM