Example #1
0
def init():
    ''' Startup initialisation of OpenGL. '''
    if gl is None:
        version, profile = get_profile_settings()
        # Initialise application-wide GL version
        fmt = QSurfaceFormat()
        fmt.setVersion(*version)
        # profile = QSurfaceFormat.CoreProfile if sys.platform == 'darwin' else QSurfaceFormat.CompatibilityProfile

        fmt.setProfile(profile or QSurfaceFormat.CompatibilityProfile)
        QSurfaceFormat.setDefaultFormat(fmt)

        if profile is not None:
            # Use a dummy context to get GL functions
            glprofile = QOpenGLVersionProfile(fmt)
            ctx = QOpenGLContext()
            globals()['gl'] = ctx.versionFunctions(glprofile)
            # Check and set OpenGL capabilities
            if not glprofile.hasProfiles():
                raise RuntimeError(
                    'No OpenGL version >= 3.3 support detected for this system!'
                )
        else:
            # If profile was none, PyQt5 is not shipped with any OpenGL function modules. Use PyOpenGL instead
            print(
                "Couldn't find OpenGL functions in Qt. Falling back to PyOpenGL"
            )
            globals()['gl'] = importlib.import_module('OpenGL.GL')

        globals()['_glvar_sizes'] = {
            gl.GL_FLOAT: 1,
            gl.GL_FLOAT_VEC2: 2,
            gl.GL_FLOAT_VEC3: 3,
            gl.GL_FLOAT_VEC4: 4,
            gl.GL_FLOAT_MAT2: 4,
            gl.GL_FLOAT_MAT3: 9,
            gl.GL_FLOAT_MAT4: 16,
            gl.GL_FLOAT_MAT2x3: 6,
            gl.GL_FLOAT_MAT2x4: 8,
            gl.GL_FLOAT_MAT3x2: 6,
            gl.GL_FLOAT_MAT3x4: 12,
            gl.GL_FLOAT_MAT4x2: 8,
            gl.GL_FLOAT_MAT4x3: 12,
            gl.GL_INT: 1,
            gl.GL_INT_VEC2: 2,
            gl.GL_INT_VEC3: 3,
            gl.GL_INT_VEC4: 4,
            gl.GL_UNSIGNED_INT: 1,
            gl.GL_UNSIGNED_INT_VEC2: 2,
            gl.GL_UNSIGNED_INT_VEC3: 3,
            gl.GL_UNSIGNED_INT_VEC4: 4,
            gl.GL_DOUBLE: 1,
            gl.GL_DOUBLE_VEC2: 2,
            gl.GL_DOUBLE_VEC3: 3,
            gl.GL_DOUBLE_VEC4: 4,
            gl.GL_DOUBLE_MAT2: 4,
            gl.GL_DOUBLE_MAT3: 9,
            gl.GL_DOUBLE_MAT4: 16,
            gl.GL_DOUBLE_MAT2x3: 6,
            gl.GL_DOUBLE_MAT2x4: 8,
            gl.GL_DOUBLE_MAT3x2: 6,
            gl.GL_DOUBLE_MAT3x4: 12,
            gl.GL_DOUBLE_MAT4x2: 8,
            gl.GL_DOUBLE_MAT4x3: 12
        }

    return gl