def _setupGL(self): #setup screen color if self.colorSpace in ['rgb','dkl','lms','hsv']: #these spaces are 0-centred desiredRGB = (self.rgb+1)/2.0#RGB in range 0:1 and scaled for contrast else: desiredRGB = self.rgb/255.0 GL.glClearColor(desiredRGB[0],desiredRGB[1],desiredRGB[2], 1.0) GL.glClearDepth(1.0) GL.glViewport(0, 0, int(self.size[0]), int(self.size[1])) GL.glMatrixMode(GL.GL_PROJECTION) # Reset The Projection Matrix GL.glLoadIdentity() GL.gluOrtho2D(-1,1,-1,1) GL.glMatrixMode(GL.GL_MODELVIEW)# Reset The Projection Matrix GL.glLoadIdentity() GL.glDisable(GL.GL_DEPTH_TEST) #GL.glEnable(GL.GL_DEPTH_TEST) # Enables Depth Testing #GL.glDepthFunc(GL.GL_LESS) # The Type Of Depth Test To Do GL.glEnable(GL.GL_BLEND) GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA) GL.glShadeModel(GL.GL_SMOOTH) # Color Shading (FLAT or SMOOTH) GL.glEnable(GL.GL_POINT_SMOOTH) #check for GL_ARB_texture_float (which is needed for shaders to be useful) #this needs to be done AFTER the context has been created if not GL.gl_info.have_extension('GL_ARB_texture_float'): self._haveShaders=False if self.winType=='pyglet' and self._haveShaders: #we should be able to compile shaders (don't just 'try') self._progSignedTexMask = _shaders.compileProgram(_shaders.vertSimple, _shaders.fragSignedColorTexMask)#fragSignedColorTexMask self._progSignedTex = _shaders.compileProgram(_shaders.vertSimple, _shaders.fragSignedColorTex) self._progSignedTexMask1D = _shaders.compileProgram(_shaders.vertSimple, _shaders.fragSignedColorTexMask1D) self._progSignedTexFont = _shaders.compileProgram(_shaders.vertSimple, _shaders.fragSignedColorTexFont) GL.glClear(GL.GL_COLOR_BUFFER_BIT|GL.GL_DEPTH_BUFFER_BIT) #identify gfx card vendor self.glVendor=GL.gl_info.get_vendor().lower() if sys.platform=='darwin': platform_specific.syncSwapBuffers(1) if haveFB: self._setupFrameBuffer()
def __init__(self, win, *args, **kwargs): """Set up the backend window according the params of the PsychoPy win Before PsychoPy 1.90.0 this code was executed in Window._setupPygame() :param: win is a PsychoPy Window (usually not fully created yet) """ BaseBackend.__init__(self, win) # sets up self.win=win as weakref self._TravisTesting = (os.environ.get('TRAVIS') == 'true') self._gammaErrorPolicy = win.gammaErrorPolicy self._origGammaRamp = None self._rampSize = None vsync = 0 # provide warning if stereo buffers are requested but unavailable if win.stereo and not GL.gl_info.have_extension('GL_STEREO'): logging.warning('A stereo window was requested but the graphics ' 'card does not appear to support GL_STEREO') win.stereo = False if sys.platform == 'darwin' and not win.useRetina and pyglet.version >= "1.3": raise ValueError("As of PsychoPy 1.85.3 OSX windows should all be " "set to useRetina=True (or remove the argument). " "Pyglet 1.3 appears to be forcing " "us to use retina on any retina-capable screen " "so setting to False has no effect.") # window framebuffer configuration bpc = kwargs.get('bpc', (8, 8, 8)) if isinstance(bpc, int): win.bpc = (bpc, bpc, bpc) else: win.bpc = bpc win.depthBits = int(kwargs.get('depthBits', 8)) if win.allowStencil: win.stencilBits = int(kwargs.get('stencilBits', 8)) else: win.stencilBits = 0 # multisampling sample_buffers = 0 aa_samples = 0 if win.multiSample: sample_buffers = 1 # get maximum number of samples the driver supports max_samples = (GL.GLint)() GL.glGetIntegerv(GL.GL_MAX_SAMPLES, max_samples) if (win.numSamples >= 2) and (win.numSamples <= max_samples.value): # NB - also check if divisible by two and integer? aa_samples = win.numSamples else: logging.warning( 'Invalid number of MSAA samples provided, must be ' 'integer greater than two. Disabling.') win.multiSample = False if pyglet.version < '1.4': allScrs = _default_display_.get_screens() else: allScrs = _default_display_.get_screens() # Screen (from Exp Settings) is 1-indexed, # so the second screen is Screen 1 if len(allScrs) < int(win.screen) + 1: logging.warn("Requested an unavailable screen number - " "using first available.") thisScreen = allScrs[0] else: thisScreen = allScrs[win.screen] if win.autoLog: logging.info('configured pyglet screen %i' % win.screen) # options that the user might want config = GL.Config(depth_size=win.depthBits, double_buffer=True, sample_buffers=sample_buffers, samples=aa_samples, stencil_size=win.stencilBits, stereo=win.stereo, vsync=vsync, red_size=win.bpc[0], green_size=win.bpc[1], blue_size=win.bpc[2]) # check if we can have this configuration validConfigs = thisScreen.get_matching_configs(config) if not validConfigs: # check which configs are invalid for the display raise RuntimeError( "Specified window configuration is not supported by this " "display.") # if fullscreen check screen size if win._isFullScr: win._checkMatchingSizes(win.clientSize, [thisScreen.width, thisScreen.height]) w = h = None else: w, h = win.clientSize if win.allowGUI: style = None else: style = 'borderless' try: self.winHandle = pyglet.window.Window(width=w, height=h, caption="PsychoPy", fullscreen=win._isFullScr, config=config, screen=thisScreen, style=style) except pyglet.gl.ContextException: # turn off the shadow window an try again pyglet.options['shadow_window'] = False self.winHandle = pyglet.window.Window(width=w, height=h, caption="PsychoPy", fullscreen=self._isFullScr, config=config, screen=thisScreen, style=style) logging.warning( "Pyglet shadow_window has been turned off. This is " "only an issue for you if you need multiple " "stimulus windows, in which case update your " "graphics card and/or graphics drivers.") if sys.platform == 'win32': # pyHook window hwnd maps to: # pyglet 1.14 -> window._hwnd # pyglet 1.2a -> window._view_hwnd if pyglet.version > "1.2": win._hw_handle = self.winHandle._view_hwnd else: win._hw_handle = self.winHandle._hwnd self._frameBufferSize = win.clientSize elif sys.platform == 'darwin': if win.useRetina: global retinaContext retinaContext = self.winHandle.context._nscontext view = retinaContext.view() bounds = view.convertRectToBacking_(view.bounds()).size if win.clientSize[0] == bounds.width: win.useRetina = False # the screen is not a retina display self._frameBufferSize = np.array( [int(bounds.width), int(bounds.height)]) else: self._frameBufferSize = win.clientSize try: # python 32bit (1.4. or 1.2 pyglet) win._hw_handle = self.winHandle._window.value except Exception: # pyglet 1.2 with 64bit python? win._hw_handle = self.winHandle._nswindow.windowNumber() elif sys.platform.startswith('linux'): win._hw_handle = self.winHandle._window self._frameBufferSize = win.clientSize if win.useFBO: # check for necessary extensions if not GL.gl_info.have_extension('GL_EXT_framebuffer_object'): msg = ("Trying to use a framebuffer object but " "GL_EXT_framebuffer_object is not supported. Disabled") logging.warn(msg) win.useFBO = False if not GL.gl_info.have_extension('GL_ARB_texture_float'): msg = ("Trying to use a framebuffer object but " "GL_ARB_texture_float is not supported. Disabling") logging.warn(msg) win.useFBO = False if pyglet.version < "1.2" and sys.platform == 'darwin': platform_specific.syncSwapBuffers(1) # add these methods to the pyglet window self.winHandle.setGamma = setGamma self.winHandle.setGammaRamp = setGammaRamp self.winHandle.getGammaRamp = getGammaRamp self.winHandle.set_vsync(True) self.winHandle.on_text = self.onText self.winHandle.on_text_motion = self.onCursorKey self.winHandle.on_key_press = self.onKey self.winHandle.on_mouse_press = event._onPygletMousePress self.winHandle.on_mouse_release = event._onPygletMouseRelease self.winHandle.on_mouse_scroll = event._onPygletMouseWheel if not win.allowGUI: # make mouse invisible. Could go further and make it 'exclusive' # (but need to alter x,y handling then) self.winHandle.set_mouse_visible(False) self.winHandle.on_resize = _onResize # avoid circular reference if not win.pos: # work out where the centre should be if win.useRetina: win.pos = [(thisScreen.width - win.clientSize[0] / 2) / 2, (thisScreen.height - win.clientSize[1] / 2) / 2] else: win.pos = [(thisScreen.width - win.clientSize[0]) / 2, (thisScreen.height - win.clientSize[1]) / 2] if not win._isFullScr: # add the necessary amount for second screen self.winHandle.set_location(int(win.pos[0] + thisScreen.x), int(win.pos[1] + thisScreen.y)) try: # to load an icon for the window iconFile = os.path.join(psychopy.prefs.paths['resources'], 'psychopy.ico') icon = pyglet.image.load(filename=iconFile) self.winHandle.set_icon(icon) except Exception: pass # doesn't matter # store properties of the system self._driver = pyglet.gl.gl_info.get_renderer()
def __init__(self, win, *args, **kwargs): """Set up the backend window according the params of the PsychoPy win Before PsychoPy 1.90.0 this code was executed in Window._setupPygame() :param: win is a PsychoPy Window (usually not fully created yet) """ BaseBackend.__init__(self, win) # sets up self.win=win as weakref if win.allowStencil: stencil_size = 8 else: stencil_size = 0 vsync = 0 # provide warning if stereo buffers are requested but unavailable if win.stereo and not GL.gl_info.have_extension('GL_STEREO'): logging.warning( 'A stereo window was requested but the graphics ' 'card does not appear to support GL_STEREO') win.stereo = False if sys.platform=='darwin' and not win.useRetina and pyglet.version >= "1.3": raise ValueError("As of PsychoPy 1.85.3 OSX windows should all be " "set to useRetina=True (or remove the argument). " "Pyglet 1.3 appears to be forcing " "us to use retina on any retina-capable screen " "so setting to False has no effect.") # multisampling sample_buffers = 0 aa_samples = 0 if win.multiSample: sample_buffers = 1 # get maximum number of samples the driver supports max_samples = (GL.GLint)() GL.glGetIntegerv(GL.GL_MAX_SAMPLES, max_samples) if (win.numSamples >= 2) and ( win.numSamples <= max_samples.value): # NB - also check if divisible by two and integer? aa_samples = win.numSamples else: logging.warning( 'Invalid number of MSAA samples provided, must be ' 'integer greater than two. Disabling.') win.multiSample = False # options that the user might want config = GL.Config(depth_size=8, double_buffer=True, sample_buffers=sample_buffers, samples=aa_samples, stencil_size=stencil_size, stereo=win.stereo, vsync=vsync) defDisp = pyglet.window.get_platform().get_default_display() allScrs = defDisp.get_screens() # Screen (from Exp Settings) is 1-indexed, # so the second screen is Screen 1 if len(allScrs) < int(win.screen) + 1: logging.warn("Requested an unavailable screen number - " "using first available.") thisScreen = allScrs[0] else: thisScreen = allScrs[win.screen] if win.autoLog: logging.info('configured pyglet screen %i' % self.screen) # if fullscreen check screen size if win._isFullScr: win._checkMatchingSizes(win.size, [thisScreen.width, thisScreen.height]) w = h = None else: w, h = win.size if win.allowGUI: style = None else: style = 'borderless' try: self.winHandle = pyglet.window.Window( width=w, height=h, caption="PsychoPy", fullscreen=win._isFullScr, config=config, screen=thisScreen, style=style) except pyglet.gl.ContextException: # turn off the shadow window an try again pyglet.options['shadow_window'] = False self.winHandle = pyglet.window.Window( width=w, height=h, caption="PsychoPy", fullscreen=self._isFullScr, config=config, screen=thisScreen, style=style) logging.warning( "Pyglet shadow_window has been turned off. This is " "only an issue for you if you need multiple " "stimulus windows, in which case update your " "graphics card and/or graphics drivers.") if sys.platform == 'win32': # pyHook window hwnd maps to: # pyglet 1.14 -> window._hwnd # pyglet 1.2a -> window._view_hwnd if pyglet.version > "1.2": win._hw_handle = self.winHandle._view_hwnd else: win._hw_handle = self.winHandle._hwnd elif sys.platform == 'darwin': if win.useRetina: global retinaContext retinaContext = self.winHandle.context._nscontext view = retinaContext.view() bounds = view.convertRectToBacking_(view.bounds()).size if win.size[0] == bounds.width: win.useRetina = False # the screen is not a retina display win.size = np.array([int(bounds.width), int(bounds.height)]) try: # python 32bit (1.4. or 1.2 pyglet) win._hw_handle = self.winHandle._window.value except Exception: # pyglet 1.2 with 64bit python? win._hw_handle = self.winHandle._nswindow.windowNumber() elif sys.platform.startswith('linux'): win._hw_handle = self.winHandle._window if win.useFBO: # check for necessary extensions if not GL.gl_info.have_extension('GL_EXT_framebuffer_object'): msg = ("Trying to use a framebuffer object but " "GL_EXT_framebuffer_object is not supported. Disabled") logging.warn(msg) win.useFBO = False if not GL.gl_info.have_extension('GL_ARB_texture_float'): msg = ("Trying to use a framebuffer object but " "GL_ARB_texture_float is not supported. Disabling") logging.warn(msg) win.useFBO = False if pyglet.version < "1.2" and sys.platform == 'darwin': platform_specific.syncSwapBuffers(1) # add these methods to the pyglet window self.winHandle.setGamma = setGamma self.winHandle.setGammaRamp = setGammaRamp self.winHandle.getGammaRamp = getGammaRamp self.winHandle.set_vsync(True) self.winHandle.on_text = event._onPygletText self.winHandle.on_key_press = event._onPygletKey self.winHandle.on_mouse_press = event._onPygletMousePress self.winHandle.on_mouse_release = event._onPygletMouseRelease self.winHandle.on_mouse_scroll = event._onPygletMouseWheel if not win.allowGUI: # make mouse invisible. Could go further and make it 'exclusive' # (but need to alter x,y handling then) self.winHandle.set_mouse_visible(False) self.winHandle.on_resize = _onResize # avoid circular reference if not win.pos: # work out where the centre should be if win.useRetina: win.pos = [(thisScreen.width - win.size[0]/2) / 2, (thisScreen.height - win.size[1]/2) / 2] else: win.pos = [(thisScreen.width - win.size[0]) / 2, (thisScreen.height - win.size[1]) / 2] if not win._isFullScr: # add the necessary amount for second screen self.winHandle.set_location(int(win.pos[0] + thisScreen.x), int(win.pos[1] + thisScreen.y)) try: # to load an icon for the window iconFile = os.path.join(psychopy.prefs.paths['resources'], 'psychopy.ico') icon = pyglet.image.load(filename=iconFile) self.winHandle.set_icon(icon) except Exception: pass # doesn't matter # store properties of the system self._driver = pyglet.gl.gl_info.get_renderer() self._origGammaRamp = self.getGammaRamp() self._rampSize = getGammaRampSize(self.screenID, self.xDisplay) self._TravisTesting = (os.environ.get('TRAVIS') == 'true')