def get_default_format(*, gles: bool): fmt = QSurfaceFormat() fmt.setVersion(2, 0) fmt.setProfile(QSurfaceFormat.CoreProfile) fmt.setRenderableType( QSurfaceFormat.OpenGLES if gles else QSurfaceFormat.OpenGL) fmt.setDepthBufferSize(24) fmt.setStencilBufferSize(8) fmt.setOption(QSurfaceFormat.DebugContext) return fmt
class GLWidget(QOpenGLWidget): def __init__(self, parent=None): QOpenGLWidget.__init__(self, parent) self.gl_format = QSurfaceFormat() self.gl_format.setRenderableType(QSurfaceFormat.OpenGL) self.gl_format.setProfile(QSurfaceFormat.CoreProfile) self.gl_format.setVersion(4, 1) self.setFormat(self.gl_format) self.mouse_x = 0 self.mouse_y = 0 self.mouse_init = False self.mouse_pressed = False def paintGL(self): display_loop(c_double(0.0), c_uint(self.defaultFramebufferObject())) def resizeGL(self, width, height): width = c_double(self.size().width()) height = c_double(self.size().height()) dpi_ratio = c_double(self.devicePixelRatio()) resize_window(width, height, dpi_ratio) def initializeGL(self): width = c_double(self.size().width()) height = c_double(self.size().height()) dpi_ratio = c_double(self.devicePixelRatio()) load_gl_symbol() init_gl(width, height, dpi_ratio) print_gl_info() init_scene(width, height, dpi_ratio) def mousePressEvent(self, ev): if ev.button() == Qt.LeftButton: self.mouse_pressed = True def mouseReleaseEvent(self, ev): if ev.button() == Qt.LeftButton: self.mouse_pressed = False self.mouse_init = False def mouseMoveEvent(self, ev): pos = ev.localPos() if self.mouse_pressed: if not self.mouse_init: self.mouse_x = pos.x() self.mouse_y = pos.y() self.mouse_init = True else: dx = self.mouse_x - pos.x() dy = self.mouse_y - pos.y() self.mouse_x = pos.x() self.mouse_y = pos.y() handle_mouse(c_float(dx), c_float(dy), c_float(0.001)) self.update()
def defaultFormat(stereo_capable): fmt = QSurfaceFormat() fmt.setRenderableType(QSurfaceFormat.OpenGL) fmt.setVersion(3, 2) fmt.setProfile(QSurfaceFormat.CoreProfile) fmt.setSwapBehavior(QSurfaceFormat.DoubleBuffer) fmt.setRedBufferSize(8) fmt.setGreenBufferSize(8) fmt.setBlueBufferSize(8) fmt.setDepthBufferSize(8) fmt.setAlphaBufferSize(8) fmt.setStencilBufferSize(0) fmt.setStereo(stereo_capable) fmt.setSamples(0) return fmt
def defaultVtkFormat(stereo_capable): """ Po prostu skopiowałem to z https://github.com/Kitware/VTK/blob/master/GUISupport/Qt/QVTKRenderWindowAdapter.cxx i działa poprawnie bufor głębokości """ fmt = QSurfaceFormat() fmt.setRenderableType(QSurfaceFormat.OpenGL) fmt.setVersion(3, 2) fmt.setProfile(QSurfaceFormat.CoreProfile) fmt.setSwapBehavior(QSurfaceFormat.DoubleBuffer) fmt.setRedBufferSize(8) fmt.setGreenBufferSize(8) fmt.setBlueBufferSize(8) fmt.setDepthBufferSize(8) fmt.setAlphaBufferSize(8) fmt.setStencilBufferSize(0) fmt.setStereo(stereo_capable) fmt.setSamples(0) return fmt
def defaultSurfaceFormat(stereo_capable): """ Ported from: https://github.com/Kitware/VTK/blob/master/GUISupport/Qt/QVTKRenderWindowAdapter.cxx """ fmt = QSurfaceFormat() fmt.setRenderableType(QSurfaceFormat.OpenGL) fmt.setVersion(3, 2) fmt.setProfile(QSurfaceFormat.CoreProfile) fmt.setSwapBehavior(QSurfaceFormat.DoubleBuffer) fmt.setRedBufferSize(8) fmt.setGreenBufferSize(8) fmt.setBlueBufferSize(8) fmt.setDepthBufferSize(8) fmt.setAlphaBufferSize(8) fmt.setStencilBufferSize(0) fmt.setStereo(stereo_capable) fmt.setSamples(0) return fmt
# # author: Antony Ducommun dit Boudry ([email protected]) # license: GPL # import sys from PySide2.QtCore import QCoreApplication, QPoint, QSize, Qt from PySide2.QtGui import QSurfaceFormat from PySide2.QtWidgets import QApplication from window import Window if __name__ == '__main__': fmt = QSurfaceFormat() fmt.setRenderableType(QSurfaceFormat.OpenGL) fmt.setVersion(2, 1) fmt.setProfile(QSurfaceFormat.CoreProfile) fmt.setSwapBehavior(QSurfaceFormat.DoubleBuffer) fmt.setSwapInterval(1) fmt.setStereo(False) fmt.setSamples(0) fmt.setRedBufferSize(8) fmt.setGreenBufferSize(8) fmt.setBlueBufferSize(8) fmt.setAlphaBufferSize(8) fmt.setDepthBufferSize(24) fmt.setStencilBufferSize(1) QSurfaceFormat.setDefaultFormat(fmt) app = QApplication(sys.argv)
class GLWin(QWindow): requestRender = Signal() def __init__(self, parent=None): QWindow.__init__(self, parent) self.setSurfaceType(QSurface.OpenGLSurface) self.gl_format = QSurfaceFormat() self.gl_format.setRenderableType(QSurfaceFormat.OpenGL) self.gl_format.setProfile(QSurfaceFormat.CoreProfile) self.gl_format.setVersion(4, 1) self.setFormat(self.gl_format) if self.supportsOpenGL(): print("OpenGL supported !") self.mouse_x = 0 self.mouse_y = 0 self.mouse_init = False self.animating = False self.requestRender.connect(self.requestUpdate) # self.setMouseGrabEnabled(True) self.mouse_pressed = False def mousePressEvent(self, ev): if ev.button() == Qt.LeftButton: self.mouse_pressed = True def mouseReleaseEvent(self, ev): if ev.button() == Qt.LeftButton: self.mouse_pressed = False self.mouse_init = False def mouseMoveEvent(self, ev): pos = ev.localPos() if self.mouse_pressed: if not self.mouse_init: self.mouse_x = pos.x() self.mouse_y = pos.y() self.mouse_init = True else: dx = self.mouse_x - pos.x() dy = self.mouse_y - pos.y() self.mouse_x = pos.x() self.mouse_y = pos.y() handle_mouse(c_float(dx), c_float(dy), c_float(0.001)) def start(self): self.animating = True self.renderLater() def render(self): display_loop(c_double(0.0)) def renderLater(self): self.requestRender.emit() def renderNow(self): if not self.isExposed(): return self.render() self.gl_context.swapBuffers(self) if self.animating: self.renderLater() def init_context(self): self.gl_context = QOpenGLContext(self) self.gl_context.setFormat(self.gl_format) if self.gl_context.create(): print("Context created !") if self.gl_context.makeCurrent(self): print("Context made current !") def init_scene(self): width = c_double(self.size().width()) height = c_double(self.size().height()) dpi_ratio = c_double(self.devicePixelRatio()) load_gl_symbol() init_gl(width, height, dpi_ratio) print_gl_info() init_scene(width, height, dpi_ratio) def resize(self): if self.isExposed(): width = c_double(self.size().width()) height = c_double(self.size().height()) dpi_ratio = c_double(self.devicePixelRatio()) resize_window(width, height, dpi_ratio) def event(self, ev): if ev.type() == QEvent.UpdateRequest: self.renderNow() return True else: return super().event(ev) def exposeEvent(self, ev): self.renderLater() def resizeEvent(self, ev): self.resize() self.renderLater()