def test_wrappers(): """Test gloo wrappers""" with Canvas(): gl.use_gl('desktop debug') gloo.clear('#112233') # make it so that there's something non-zero # check presets assert_raises(ValueError, gloo.set_state, preset='foo') for state in gloo.get_state_presets().keys(): gloo.set_state(state) assert_raises(ValueError, gloo.set_blend_color, (0., 0.)) # bad color assert_raises(TypeError, gloo.set_hint, 1, 2) # need strs assert_raises(TypeError, gloo.get_parameter, 1) # need str # this doesn't exist in ES 2.0 namespace assert_raises(ValueError, gloo.set_hint, 'fog_hint', 'nicest') # test bad enum assert_raises(RuntimeError, gloo.set_line_width, -1) # check read_pixels x = gloo.read_pixels() assert_true(isinstance(x, np.ndarray)) assert_true(isinstance(gloo.read_pixels((0, 0, 1, 1)), np.ndarray)) assert_raises(ValueError, gloo.read_pixels, (0, 0, 1)) # bad port y = gloo.read_pixels(alpha=False, out_type=np.ubyte) assert_equal(y.shape, x.shape[:2] + (3, )) assert_array_equal(x[..., :3], y) y = gloo.read_pixels(out_type='float') assert_allclose(x / 255., y) # now let's (indirectly) check our set_* functions viewport = (0, 0, 1, 1) blend_color = (0., 0., 0.) _funs = dict( viewport=viewport, # checked hint=('generate_mipmap_hint', 'nicest'), depth_range=(1., 2.), front_face='cw', # checked cull_face='front', line_width=1., polygon_offset=(1., 1.), blend_func=('zero', 'one'), blend_color=blend_color, blend_equation='func_add', scissor=(0, 0, 1, 1), stencil_func=('never', 1, 2, 'back'), stencil_mask=4, stencil_op=('zero', 'zero', 'zero', 'back'), depth_func='greater', depth_mask=True, color_mask=(True, True, True, True), sample_coverage=(0.5, True)) gloo.set_state(**_funs) gloo.clear((1., 1., 1., 1.), 0.5, 1) gloo.flush() gloo.finish() # check some results assert_array_equal(gloo.get_parameter('viewport'), viewport) assert_equal(gloo.get_parameter('front_face'), gl.GL_CW) assert_equal(gloo.get_parameter('blend_color'), blend_color + (1, ))
def test_wrappers(): """Test gloo wrappers""" with Canvas(): gl.use_gl('desktop debug') gloo.clear('#112233') # make it so that there's something non-zero # check presets assert_raises(ValueError, gloo.set_state, preset='foo') for state in gloo.get_state_presets().keys(): gloo.set_state(state) assert_raises(ValueError, gloo.set_blend_color, (0., 0.)) # bad color assert_raises(TypeError, gloo.set_hint, 1, 2) # need strs assert_raises(TypeError, gloo.get_parameter, 1) # need str # this doesn't exist in ES 2.0 namespace assert_raises(ValueError, gloo.set_hint, 'fog_hint', 'nicest') # test bad enum assert_raises(RuntimeError, gloo.set_line_width, -1) # check read_pixels x = gloo.read_pixels() assert_true(isinstance(x, np.ndarray)) assert_true(isinstance(gloo.read_pixels((0, 0, 1, 1)), np.ndarray)) assert_raises(ValueError, gloo.read_pixels, (0, 0, 1)) # bad port y = gloo.read_pixels(alpha=False, out_type=np.ubyte) assert_equal(y.shape, x.shape[:2] + (3,)) assert_array_equal(x[..., :3], y) y = gloo.read_pixels(out_type='float') assert_allclose(x/255., y) # now let's (indirectly) check our set_* functions viewport = (0, 0, 1, 1) blend_color = (0., 0., 0.) _funs = dict(viewport=viewport, # checked hint=('generate_mipmap_hint', 'nicest'), depth_range=(1., 2.), front_face='cw', # checked cull_face='front', line_width=1., polygon_offset=(1., 1.), blend_func=('zero', 'one'), blend_color=blend_color, blend_equation='func_add', scissor=(0, 0, 1, 1), stencil_func=('never', 1, 2, 'back'), stencil_mask=4, stencil_op=('zero', 'zero', 'zero', 'back'), depth_func='greater', depth_mask=True, color_mask=(True, True, True, True), sample_coverage=(0.5, True)) gloo.set_state(**_funs) gloo.clear((1., 1., 1., 1.), 0.5, 1) gloo.flush() gloo.finish() # check some results assert_array_equal(gloo.get_parameter('viewport'), viewport) assert_equal(gloo.get_parameter('front_face'), gl.GL_CW) assert_equal(gloo.get_parameter('blend_color'), blend_color + (1,))
def test_wrappers_glir(): """ Test that special wrapper functions do what they must do """ glir = install_dummy_glir() # Test clear() function gloo.clear() cmds = glir.clear() assert len(cmds) == 1 assert cmds[0][0] == 'FUNC' assert cmds[0][1] == 'glClear' # gloo.clear(True, False, False) cmds = glir.clear() assert len(cmds) == 1 assert cmds[0][0] == 'FUNC' assert cmds[0][1] == 'glClear' assert cmds[0][2] == gl.GL_COLOR_BUFFER_BIT # gloo.clear('red') cmds = glir.clear() assert len(cmds) == 2 assert cmds[0][0] == 'FUNC' assert cmds[0][1] == 'glClearColor' assert cmds[1][0] == 'FUNC' assert cmds[1][1] == 'glClear' # gloo.clear('red', 4, 3) cmds = glir.clear() assert len(cmds) == 4 assert cmds[0][1] == 'glClearColor' assert cmds[1][1] == 'glClearDepth' assert cmds[2][1] == 'glClearStencil' assert cmds[3][1] == 'glClear' # Test set_state() function gloo.set_state(foo=True, bar=False) cmds = set(glir.clear()) assert len(cmds) == 2 assert ('FUNC', 'glEnable', 'foo') in cmds assert ('FUNC', 'glDisable', 'bar') in cmds # gloo.set_state(viewport=(0, 0, 10, 10), clear_color='red') cmds = sorted(glir.clear()) assert len(cmds) == 2 assert cmds[0][1] == 'glClearColor' assert cmds[1][1] == 'glViewport' # presets = gloo.get_state_presets() a_preset = list(presets.keys())[0] gloo.set_state(a_preset) cmds = sorted(glir.clear()) assert len(cmds) == len(presets[a_preset]) reset_glir()
class VisualWrapper: """Wrapper around ``vispy.scene.VisualNode`` objects. Meant to be subclassed. "Hidden" properties: * ``_master_transform`` * ``_order`` * ``_parent`` Parameters ---------- central_node : vispy.scene.VisualNode Central node/control point with which to interact with the visual. Stored as ``_node``. Attributes ---------- opacity visible scale blending translate z_index Notes ----- It is recommended to use the backported ``vispy`` nodes at ``_vispy.scene.visuals`` for various bug fixes. """ def __init__(self, central_node): self._node = central_node self._blending = 'translucent' self.events = EmitterGroup(source=self, auto_connect=True, blending=Event, opacity=Event, visible=Event) _blending_modes = set(get_state_presets().keys()) @property def _master_transform(self): """vispy.visuals.transforms.STTransform: Central node's firstmost transform. """ # whenever a new parent is set, the transform is reset # to a NullTransform so we reset it here if not isinstance(self._node.transform, STTransform): self._node.transform = STTransform() return self._node.transform @property def _order(self): """int: Order in which the visual is drawn in the scenegraph. Lower values are closer to the viewer. """ return self._node.order @_order.setter def _order(self, order): # workaround for opacity (see: #22) order = -order self.z_index = order # end workaround self._node.order = order @property def _parent(self): """vispy.scene.Node: Parent node. """ return self._node.parent @_parent.setter def _parent(self, parent): self._node.parent = parent @property def opacity(self): """float: Opacity value between 0.0 and 1.0. """ return self._node.opacity @opacity.setter def opacity(self, opacity): if not 0.0 <= opacity <= 1.0: raise ValueError('opacity must be between 0.0 and 1.0; ' f'got {opacity}') self._node.opacity = opacity self.events.opacity() @property def blending(self): """{'opaque', 'translucent', 'additive'}: Blending mode. Selects a preset blending mode in vispy that determines how RGB and alpha values get mixed. 'opaque' Allows for only the top layer to be visible and corresponds to depth_test=True, cull_face=False, blend=False. 'translucent' Allows for multiple layers to be blended with different opacity and corresponds to depth_test=True, cull_face=False, blend=True, blend_func=('src_alpha', 'one_minus_src_alpha'). 'additive' Allows for multiple layers to be blended together with different colors and opacity. Useful for creating overlays. It corresponds to depth_test=False, cull_face=False, blend=True, blend_func=('src_alpha', 'one'). """ return self._blending @blending.setter def blending(self, blending): if blending not in self._blending_modes: raise ValueError('expected one of ' "{'opaque', 'translucent', 'additive'}; " f'got {blending}') self._node.set_gl_state(blending) self._blending = blending self.events.blending() @property def visible(self): """bool: Whether the visual is currently being displayed. """ return self._node.visible @visible.setter def visible(self, visibility): self._node.visible = visibility self.events.visible() @property def scale(self): """sequence of float: Scale factors. """ return self._master_transform.scale @scale.setter def scale(self, scale): self._master_transform.scale = scale @property def translate(self): """sequence of float: Translation values. """ return self._master_transform.translate @translate.setter def translate(self, translate): self._master_transform.translate = translate @property def z_index(self): return -self._master_transform.translate[2] @z_index.setter def z_index(self, index): tr = self._master_transform tl = tr.translate tl[2] = -index tr.translate = tl