我窗口中的对象有时会消失并重新出现。这主要发生在调整窗口大小时。我想这是因为我的两种方法与
glutMainLoopEvent()
功能相互冲突。
我这样创建窗口:
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
作为显示函数,此方法被调用:
@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
(此外,如果没有 glutSwapBuffers() 函数,当我最大化它时窗口不会更新,因此会创建黑色边框。)
但在我的主循环中,我总是在显示任何内容之前从另一个脚本调用此方法:
@staticmethod
def prepare():
glClearColor(1, 0, 0, 1) # set backdrop color to red
glClear(GL_COLOR_BUFFER_BIT) # clear everything
还在主函数中调用
display.update_display()
方法导致根本没有对象被渲染。
仅调用
renderer.prepare()
方法不会导致窗口更新。
我想通了。完成绘制到屏幕后需要调用
glutSwapBuffers()
函数(我猜是用更改更新屏幕?)。
所以现在我从主循环调用
display.update_display()
方法,最后我在glutSwapBuffers()
之前调用glutMainLoopEvent()
。