Exemplo n.º 1
0
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, ))
Exemplo n.º 2
0
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,))
Exemplo n.º 3
0
 def on_mouse_move(self, event):
     x, y = event.pos
     h = gloo.get_parameter('viewport')[3]
     self.data['a_position'][self.index] = x, h - y
     self.data['a_size'][self.index] = 5
     self.data['a_fg_color'][self.index] = 0, 0, 0, 1
     self.index = (self.index + 1) % 500
Exemplo n.º 4
0
 def on_mouse_move(self, event):
     x, y = event.pos
     h = gloo.get_parameter('viewport')[3]
     self.data['a_position'][self.index] = x, h - y
     self.data['a_size'][self.index] = 5
     self.data['a_fg_color'][self.index] = 0, 0, 0, 1
     self.index = (self.index + 1) % 500
Exemplo n.º 5
0
def on_passive_motion(x, y):
    global index
    _, _, _, h = gloo.get_parameter('viewport')
    data['a_position'][index] = x, h - y
    data['a_size'][index] = 5
    data['a_fg_color'][index] = 0, 0, 0, 1
    index = (index + 1) % 500
    glut.glutPostRedisplay()
Exemplo n.º 6
0
 def draw(self):
     viewport = gloo.get_parameter('viewport')
     size = viewport[2:4]
     x_view_offset = (size[0] - self.size[0]) // 2
     y_view_offset = (size[1] - self.size[1]) // 2
     x = self.x + x_view_offset
     y = self.y + 15 + y_view_offset
     gloo.set_viewport(x, y, self.size[0], self.size[1])
     self.text.pos = (0, 0)
     self.text.draw()
Exemplo n.º 7
0
 def draw(self):
     viewport = gloo.get_parameter('viewport')
     size = viewport[2:4]
     x_view_offset = (size[0] - self.size[0]) // 2
     y_view_offset = (size[1] - self.size[1]) // 2
     x =  self.x +      x_view_offset
     y =  self.y + 15 + y_view_offset
     gloo.set_viewport(x, y, self.size[0], self.size[1])
     self.text.pos = (0, 0)
     self.text.draw()
Exemplo n.º 8
0
Arquivo: text.py Projeto: tonysyu/deli
def _text_to_vbo(text, font, anchor_x, anchor_y, lowres_size):
    """Convert text characters to vertex buffer object."""
    # XXX: This function may benefit from a LRU cache.

    text_vtype = np.dtype([('a_position', 'f4', 2),
                           ('a_texcoord', 'f4', 2)])
    vertices = np.zeros(len(text) * 4, dtype=text_vtype)
    prev = None
    width = height = ascender = descender = 0
    ratio, slop = 1. / font.ratio, font.slop
    x_off = -slop
    # Need to make sure we have a unicode string here (Py2.7 mis-interprets
    # characters like "•" otherwise)
    if sys.version[0] == '2' and isinstance(text, str):
        text = text.decode('utf-8')
    # Need to store the original viewport, because the font[char] will
    # trigger SDF rendering, which changes our viewport
    orig_viewport = get_parameter('viewport')

    positions = []
    texcoords = []
    # XXX: Iterating over characters in Python is a really bad idea.
    for char in text:
        glyph = font[char]
        kerning = glyph['kerning'].get(prev, 0.) * ratio
        x0 = x_off + glyph['offset'][0] * ratio + kerning
        y0 = glyph['offset'][1] * ratio + slop
        x1 = x0 + glyph['size'][0]
        y1 = y0 - glyph['size'][1]
        u0, v0, u1, v1 = glyph['texcoords']
        positions.extend(rect_extents_to_corners(x0, y0, x1, y1))
        texcoords.extend(rect_extents_to_corners(u0, v0, u1, v1))
        x_move = glyph['advance'] * ratio + kerning
        x_off += x_move
        width += x_move
        ascender = max(ascender, y0 - slop)
        descender = min(descender, y1 + slop)
        height = max(height, glyph['size'][1] - 2*slop)
        prev = char
    vertices['a_position'] = positions
    vertices['a_texcoord'] = texcoords

    # Also analyse chars with large ascender and descender, otherwise the
    # vertical alignment can be very inconsistent
    for char in 'hy':
        glyph = font[char]
        y0 = glyph['offset'][1] * ratio + slop
        y1 = y0 - glyph['size'][1]
        ascender = max(ascender, y0 - slop)
        descender = min(descender, y1 + slop)
        height = max(height, glyph['size'][1] - 2*slop)

    set_viewport(*orig_viewport)

    # Tight bounding box (loose would be width, font.height /.asc / .desc)
    width -= glyph['advance'] * ratio - (glyph['size'][0] - 2*slop)
    dx = dy = 0

    if anchor_y == 'top':
        dy = -ascender
    elif anchor_y in ('center', 'middle'):
        dy = -(height / 2 + descender)
    elif anchor_y == 'bottom':
        dy = -descender

    if anchor_x == 'right':
        dx = -width
    elif anchor_x == 'center':
        dx = -width / 2.

    vertices['a_position'] += (dx, dy)
    vertices['a_position'] /= lowres_size

    # XXX: Subtract descender to include that in the height
    text_extent = (0, descender, width, height - descender)
    return VertexBuffer(vertices), text_extent