How do I resize the 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 several things to resize and improve this window, but no luck so far. When the window changes, the scene should be properly rendered in real time, but it is not, instead the scene is only updated when you release the mouse (the change function is not called), or maybe when it changes it gets a small chance to update but the end result is definitely not cool.

It is worth mentioning that the pyqt opengl app is resizable and it is enabled to show window contents while dragging .

Test made on windows7, PyOpenGL == 3.1.1 and python3.5.1_x86

So the question is, how do I resize and update the window in excess in real time?

+3


source to share


1 answer


glViewport

Not in the permutation handler in the first place (if I had a cent for every time I wrote this ...).

The problem you are having is not something that you can easily eliminate from the "outside" of GLUT, because it essentially boils down to how the innards of the main loop are injected. In order to smoothly update the contents of a window during resizing, the main loop must technically accommodate this situation, smoothly interspersing change events with calls to the display function. The flag glutPostRedisplay

indicates that the main repainting loop will call the display function every few resizing steps, but may result in flickering and abrupt redrawing.



Your best bet is to do something that is generally frowned upon: call the display function (including buffer swap) at the end of the resize handler and not call glutPostRedisplay

. However, this may still be prone to flickering, depending on how the WSI background erase is performed. The problem is that resize events can queue up and resize steps that have long been beyond the user's reach.

Really smooth resizing requires a required writing loop that combines input / resize events to avoid queue latency issues and allows drawing operations without resizing without automatically erasing the background to support smooth updates. Current GLUT implementations do not.

+3


source







All Articles