Search code examples
pyopenglfreeglutglutcreatewindow

PyOpenGL Glut Window


So I recently started to use PyOpenGL's GLUT module and cannot find any simple tutorials on it (links to any would be appreciated) and I just want to create a glut window using glutCreateWindow('window'), but as soon as the window pops up it disappears. I tried using glutMainLoop() in my main function but it just gives an error.

from OpenGL.GLU import *
from OpenGL.GL import *

glutInit()

def main():
    glutCreateWindow('window')
    glutMainLoop()

if __name__=='__main__':main()

Solution

  • You must set glutDisplayFunc callback. The glut main loop invokes the display call back.

    Minimal example:

    from OpenGL.GLUT import *
    from OpenGL.GLU import *
    from OpenGL.GL import *
    
    glutInit()
    
    def display():
        glClearColor(1, 0, 0, 0) # red
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
    
        # your rendering goes here
        # [...]
    
        glutSwapBuffers()
        glutPostRedisplay()
    
    def main():
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA)
        glutCreateWindow('window')
        glutDisplayFunc(display)
        glutMainLoop()
    
    if __name__=='__main__':
        main()
    

    glutInitDisplayMode sets the initial display mode. glutSwapBuffers swaps the buffers of the current window ant and thus updates the display. glutPostRedisplay marks the current window as redisplayed and therefore causes the display to be continuously redrawn, which is necessary for animations.

    See also Immediate mode and legacy OpenGL