def setContext(cls, major_version: int, minor_version: int, core=False, profile=None): """Set OpenGL context, given major, minor version + core using QOpenGLContext Unfortunately, what you get back does not have to be the requested version. """ new_format = QSurfaceFormat() new_format.setMajorVersion(major_version) new_format.setMinorVersion(minor_version) if core: profile_ = QSurfaceFormat.CoreProfile else: profile_ = QSurfaceFormat.CompatibilityProfile if profile is not None: profile_ = profile new_format.setProfile(profile_) new_context = QOpenGLContext() new_context.setFormat(new_format) success = new_context.create() if success: return new_context else: Logger.log( "e", "Failed creating OpenGL context (%d, %d, core=%s)" % (major_version, minor_version, core)) return None
def setDefaultFormat(cls, major_version: int, minor_version: int, core=False, profile=None) -> None: """Set the default format for each new OpenGL context :param major_version: :param minor_version: :param core: (optional) True for QSurfaceFormat.CoreProfile, False for CompatibilityProfile :param profile: (optional) QSurfaceFormat.CoreProfile, CompatibilityProfile or NoProfile, overrules option core """ new_format = QSurfaceFormat() new_format.setMajorVersion(major_version) new_format.setMinorVersion(minor_version) if core: profile_ = QSurfaceFormat.CoreProfile else: profile_ = QSurfaceFormat.CompatibilityProfile if profile is not None: profile_ = profile new_format.setProfile(profile_) QSurfaceFormat.setDefaultFormat(new_format) cls.major_version = major_version cls.minor_version = minor_version cls.profile = profile_
def setContext(cls, major_version: int, minor_version: int, core=False, profile=None): new_format = QSurfaceFormat() new_format.setMajorVersion(major_version) new_format.setMinorVersion(minor_version) if core: profile_ = QSurfaceFormat.CoreProfile else: profile_ = QSurfaceFormat.CompatibilityProfile if profile is not None: profile_ = profile new_format.setProfile(profile_) new_context = QOpenGLContext() new_context.setFormat(new_format) success = new_context.create() if success: return new_context else: Logger.log( "e", "Failed creating OpenGL context (%d, %d, core=%s)" % (major_version, minor_version, core)) return None
def setDefaultFormat(cls, major_version, minor_version, core = False, profile = None): new_format = QSurfaceFormat() new_format.setMajorVersion(major_version) new_format.setMinorVersion(minor_version) if core: profile_ = QSurfaceFormat.CoreProfile else: profile_ = QSurfaceFormat.CompatibilityProfile if profile is not None: profile_ = profile new_format.setProfile(profile_) QSurfaceFormat.setDefaultFormat(new_format) cls.major_version = major_version cls.minor_version = minor_version cls.profile = profile_
def setContext(cls, major_version, minor_version, core = False, profile = None): new_format = QSurfaceFormat() new_format.setMajorVersion(major_version) new_format.setMinorVersion(minor_version) if core: profile_ = QSurfaceFormat.CoreProfile else: profile_ = QSurfaceFormat.CompatibilityProfile if profile is not None: profile_ = profile new_format.setProfile(profile_) new_context = QOpenGLContext() new_context.setFormat(new_format) success = new_context.create() if success: return new_context else: Logger.log("e", "Failed creating OpenGL context (%d, %d, core=%s)" % (major_version, minor_version, core)) return None
def detectBestOpenGLVersion(cls): Logger.log("d", "Trying OpenGL context 4.1...") ctx = cls.setContext(4, 1, core=True) if ctx is not None: fmt = ctx.format() profile = fmt.profile() # First test: we hope for this if ((fmt.majorVersion() == 4 and fmt.minorVersion() >= 1) or (fmt.majorVersion() > 4)) and profile == QSurfaceFormat.CoreProfile: Logger.log( "d", "Yay, we got at least OpenGL 4.1 core: %s", cls.versionAsText(fmt.majorVersion(), fmt.minorVersion(), profile)) # https://riverbankcomputing.com/pipermail/pyqt/2017-January/038640.html # PyQt currently only implements 2.0, 2.1 and 4.1Core # If eg 4.5Core would be detected and used here, PyQt would not be able to handle it. major_version = 4 minor_version = 1 # CURA-6092: Check if we're not using software backed 4.1 context; A software 4.1 context # is much slower than a hardware backed 2.0 context gl_window = QWindow() gl_window.setSurfaceType(QWindow.OpenGLSurface) gl_window.showMinimized() gl_format = QSurfaceFormat() gl_format.setMajorVersion(major_version) gl_format.setMinorVersion(minor_version) gl_format.setProfile(profile) gl_context = QOpenGLContext() gl_context.setFormat(gl_format) gl_context.create() gl_context.makeCurrent(gl_window) gl_profile = QOpenGLVersionProfile() gl_profile.setVersion(major_version, minor_version) gl_profile.setProfile(profile) gl = gl_context.versionFunctions( gl_profile ) # type: Any #It's actually a protected class in PyQt that depends on the requested profile and the implementation of your graphics card. gpu_type = "Unknown" # type: str result = None if gl: result = gl.initializeOpenGLFunctions() if not result: Logger.log("e", "Could not initialize OpenGL to get gpu type") else: # WORKAROUND: Cura/#1117 Cura-packaging/12 # Some Intel GPU chipsets return a string, which is not undecodable via PyQt5. # This workaround makes the code fall back to a "Unknown" renderer in these cases. try: gpu_type = gl.glGetString(gl.GL_RENDERER) #type: str except UnicodeDecodeError: Logger.log( "e", "DecodeError while getting GL_RENDERER via glGetString!" ) Logger.log("d", "OpenGL renderer type for this OpenGL version: %s", gpu_type) if "software" in gpu_type.lower(): Logger.log( "w", "Unfortunately OpenGL 4.1 uses software rendering") else: return major_version, minor_version, QSurfaceFormat.CoreProfile else: Logger.log("d", "Failed to create OpenGL context 4.1.") # Fallback: check min spec Logger.log("d", "Trying OpenGL context 2.0...") ctx = cls.setContext(2, 0, profile=QSurfaceFormat.NoProfile) if ctx is not None: fmt = ctx.format() profile = fmt.profile() if fmt.majorVersion() >= 2 and fmt.minorVersion() >= 0: Logger.log( "d", "We got at least OpenGL context 2.0: %s", cls.versionAsText(fmt.majorVersion(), fmt.minorVersion(), profile)) return 2, 0, QSurfaceFormat.NoProfile else: Logger.log( "d", "Current OpenGL context is too low: %s" % cls.versionAsText(fmt.majorVersion(), fmt.minorVersion(), profile)) return None, None, None else: Logger.log("d", "Failed to create OpenGL context 2.0.") return None, None, None
self.translate = False def wheelEvent(self,event) : numPixels = event.pixelDelta(); if numPixels.x() > 0 : self.modelPos.m_z += self.ZOOM elif numPixels.x() < 0 : self.modelPos.m_z -= self.ZOOM self.update(); if __name__ == '__main__': app = QApplication(sys.argv) format=QSurfaceFormat() format.setSamples(4); format.setMajorVersion(4); format.setMinorVersion(1); format.setProfile(QSurfaceFormat.CoreProfile); # now set the depth buffer to 24 bits format.setDepthBufferSize(24); # set that as the default format for all windows QSurfaceFormat.setDefaultFormat(format); window = MainWindow() window.setFormat(format) window.resize(1024,720) window.show() sys.exit(app.exec_())