QtOpenGL-共享的OpenGL上下文

时间:2018-12-07 13:45:26

标签: python qt opengl qtopengl

我有一个Qt4 + OpenGL + Python应用程序,该应用程序生成几何并将其绘制在QGLWidget上。

class GLWidget(QtOpenGL.QGLWidget):

   def initializeGL(self):
       (...)
       self.scene.buildScene() #this generates geometry and creates OpenGL Lists

   def paintGL(self):
       (...)
       self.scene.renderScene() # this calls glCallList on every object

这很好用。问题在于,根据输入的不同,几何图形生成可能需要几秒钟甚至几分钟的时间。由于应用程序停留在initializeGL()

中,因此此时没有Qt窗口出现

我试图通过使用共享资源的其他OpenGL上下文创建另一个线程来避免这种“滞后”:

class GLWidget(QtOpenGL.QGLWidget):
   def __init__(self, parent):
      (...)
      self.buildingThread = threading.Thread(target=self.buildingThreadFunc)

   def buildingThreadFunc(self):
      self.buildFormat = QtOpenGL.QGLFormat()
      self.buildingContext = QtOpenGL.QGLContext(self.buildFormat)
      self.buildingContext.create(self.context()) #This returns False
      self.buildingContext.device() #This returns None
      self.buildingContext.initialized() #This returns False

      self.scene.buildScene() #And finnally this fails because 
                              #there is no context to call `glGenLists`

   def initializeGL(self):
      (...)
      self.buildingThread.start()

   def paintGL(self):
      (...)
      self.scene.renderScene() #Here, every element on scene shall successively appears during geometry creation

核心问题是QGLContext::create返回False,但我不知道为什么。

此外:我关于另一个线程和上下文的想法正确吗?能行吗我想glGenLists和同时在QGLWidget上绘画可能会有问题,但是我希望至少其他小部件能够“响应”。

1 个答案:

答案 0 :(得分:1)

几年前I've read this blog post,介绍了如何使用多个渲染线程。我有一个在后台显示的GL加载小部件,另一个QGLWidget正在启动。 在博客文章的基础上,我进行了一个演示,演示了许多正在同时启动的加载小部件。

也看一下这个例子:

http://doc.qt.io/archives/qt-4.8/qt-demos-glhypnotizer-example.html