Why do objects in my window sometimes disappear and reappear?

Question:

The objects in my window sometimes disappear and reappear. This mainly happens when resizing the window. I suppose this is because my two methods conflict with each other with the glutMainLoopEvent() function.

I create the window like this:

    def create_display(self, window_name):
        glutInit()
        glutInitDisplayMode(GLUT_RGBA)                           # initialize colors
        glutInitWindowSize(self.get_width(), self.get_height())  # set windows size
        glutInitWindowPosition(0, 0)                             # set window position
        glutCreateWindow(f"{window_name}")                       # create window (with a name) and set window attribute
        glutSetWindow(self.get_window_id())
        glutDisplayFunc(self.update_display)
        glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS)  # prevent program from stopping

And as the display function this method gets called:

@staticmethod
    def update_display():
        glClearColor(1, 0, 0, 1)                            # set backdrop color to red
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)  # Remove everything from screen (i.e. displays all white)
        glLoadIdentity()                                    # Reset all graphic/shape's position
        glutSwapBuffers()                                   # Important for double buffering

(Also, without the glutSwapBuffers() function the window doesn’t get updated when I maximize it so black borders are created.)

But in my main loop I always call this method from another script before displaying anything:

@staticmethod
    def prepare():
        glClearColor(1, 0, 0, 1)        # set backdrop color to red
        glClear(GL_COLOR_BUFFER_BIT)    # clear everything

Also calling the display.update_display() method in the main function results in no object being rendered at all.

Only calling the renderer.prepare() method results in no updates being made for the window.

Asked By: Andreas Sabelfeld

||

Answers:

I figured it out. The glutSwapBuffers() function needs to be called after you finish drawing to the screen (kind of updates the screen with the changes I guess?).

So now I’m calling the display.update_display() method from the main loop and at the end I call glutSwapBuffers() right before the glutMainLoopEvent().

Answered By: Andreas Sabelfeld