示例#1
0
 def _append_screenshot(self):
     if self.sector is None:
         # self._images.append(ImageGrab.grab())
         self._images.append(im.fromarray(_screenshot()))
     else:
         # self._images.append(ImageGrab.grab(bbox=self.sector))
         self._images.append(im.fromarray(_screenshot()))
示例#2
0
def test_use_framebuffer():
    """Test drawing to a framebuffer"""
    shape = (100, 300)  # for some reason Windows wants a tall window...
    data = np.random.rand(*shape).astype(np.float32)
    use_shape = shape + (3,)
    with Canvas(size=shape[::-1]) as c:
        orig_tex = Texture2D(data)
        fbo_tex = Texture2D(use_shape, format='rgb')
        rbo = RenderBuffer(shape, 'color')
        fbo = FrameBuffer(color=fbo_tex)
        c.context.glir.set_verbose(True)
        assert_equal(c.size, shape[::-1])
        set_viewport((0, 0) + c.size)
        with fbo:
            draw_texture(orig_tex)
        draw_texture(fbo_tex)
        out_tex = _screenshot()[::-1, :, 0].astype(np.float32)
        assert_equal(out_tex.shape, c.size[::-1])
        assert_raises(TypeError, FrameBuffer.color_buffer.fset, fbo, 1.)
        assert_raises(TypeError, FrameBuffer.depth_buffer.fset, fbo, 1.)
        assert_raises(TypeError, FrameBuffer.stencil_buffer.fset, fbo, 1.)
        fbo.color_buffer = rbo
        fbo.depth_buffer = RenderBuffer(shape)
        fbo.stencil_buffer = None
        print((fbo.color_buffer, fbo.depth_buffer, fbo.stencil_buffer))
        clear(color='black')
        with fbo:
            clear(color='black')
            draw_texture(orig_tex)
            out_rbo = _screenshot()[:, :, 0].astype(np.float32)
    assert_allclose(data * 255., out_tex, atol=1)
    assert_allclose(data * 255., out_rbo, atol=1)
示例#3
0
def test_use_framebuffer():
    """Test drawing to a framebuffer"""
    shape = (100, 100)
    data = np.random.rand(*shape).astype(np.float32)
    orig_tex = Texture2D(data)
    use_shape = shape + (3,)
    fbo_tex = Texture2D(shape=use_shape, dtype=np.ubyte, format='rgb')
    rbo = ColorBuffer(shape=shape)
    fbo = FrameBuffer(color=fbo_tex)
    with Canvas(size=(100, 100)) as c:
        set_viewport((0, 0) + c.size)
        with fbo:
            draw_texture(orig_tex)
        draw_texture(fbo_tex)
        out_tex = _screenshot()[::-1, :, 0].astype(np.float32)
        assert_raises(TypeError, FrameBuffer.color_buffer.fset, fbo, 1.)
        assert_raises(TypeError, FrameBuffer.depth_buffer.fset, fbo, 1.)
        assert_raises(TypeError, FrameBuffer.stencil_buffer.fset, fbo, 1.)
        fbo.color_buffer = rbo
        fbo.depth_buffer = DepthBuffer(shape)
        fbo.stencil_buffer = None
        print((fbo.color_buffer, fbo.depth_buffer, fbo.stencil_buffer))
        clear(color='black')
        with fbo:
            clear(color='black')
            draw_texture(orig_tex)
            out_rbo = _screenshot()[:, :, 0].astype(np.float32)
    assert_allclose(data * 255., out_tex, atol=1)
    assert_allclose(data * 255., out_rbo, atol=1)
示例#4
0
def test_use_framebuffer():
    """Test drawing to a framebuffer"""
    shape = (100, 300)  # for some reason Windows wants a tall window...
    data = np.random.rand(*shape).astype(np.float32)
    use_shape = shape + (3, )
    with Canvas(size=shape[::-1]) as c:
        orig_tex = Texture2D(data)
        fbo_tex = Texture2D(use_shape, format='rgb')
        rbo = RenderBuffer(shape, 'color')
        fbo = FrameBuffer(color=fbo_tex)
        c._glir.set_verbose(True)
        assert_equal(c.size, shape[::-1])
        set_viewport((0, 0) + c.size)
        with fbo:
            draw_texture(orig_tex)
        draw_texture(fbo_tex)
        out_tex = _screenshot()[::-1, :, 0].astype(np.float32)
        assert_equal(out_tex.shape, c.size[::-1])
        assert_raises(TypeError, FrameBuffer.color_buffer.fset, fbo, 1.)
        assert_raises(TypeError, FrameBuffer.depth_buffer.fset, fbo, 1.)
        assert_raises(TypeError, FrameBuffer.stencil_buffer.fset, fbo, 1.)
        fbo.color_buffer = rbo
        fbo.depth_buffer = RenderBuffer(shape)
        fbo.stencil_buffer = None
        print((fbo.color_buffer, fbo.depth_buffer, fbo.stencil_buffer))
        clear(color='black')
        with fbo:
            clear(color='black')
            draw_texture(orig_tex)
            out_rbo = _screenshot()[:, :, 0].astype(np.float32)
    assert_allclose(data * 255., out_tex, atol=1)
    assert_allclose(data * 255., out_rbo, atol=1)
示例#5
0
def test_use_uniforms():
    """Test using uniform arrays"""
    VERT_SHADER = """
    attribute vec2 a_pos;
    varying vec2 v_pos;

    void main (void)
    {
        v_pos = a_pos;
        gl_Position = vec4(a_pos, 0., 1.);
    }
    """

    FRAG_SHADER = """
    varying vec2 v_pos;
    uniform vec3 u_color[2];

    void main()
    {
        gl_FragColor = vec4((u_color[0] + u_color[1]) / 2., 1.);
    }
    """
    shape = (500, 500)
    with Canvas(size=shape) as c:
        c.set_current()
        c.context.glir.set_verbose(True)
        assert_equal(c.size, shape[::-1])
        shape = (3, 3)
        set_viewport((0, 0) + shape)
        program = Program(VERT_SHADER, FRAG_SHADER)
        program['a_pos'] = [[-1., -1.], [1., -1.], [-1., 1.], [1., 1.]]
        program['u_color'] = np.ones((2, 3))
        c.context.clear('k')
        c.set_current()
        program.draw('triangle_strip')
        out = _screenshot()
        assert_allclose(out[:, :, 0] / 255., np.ones(shape), atol=1. / 255.)

        # now set one element
        program['u_color[1]'] = np.zeros(3, np.float32)
        c.context.clear('k')
        program.draw('triangle_strip')
        out = _screenshot()
        assert_allclose(out[:, :, 0] / 255.,
                        127.5 / 255. * np.ones(shape),
                        atol=1. / 255.)

        # and the other
        assert_raises(ValueError, program.__setitem__, 'u_color',
                      np.zeros(3, np.float32))
        program['u_color'] = np.zeros((2, 3), np.float32)
        program['u_color[0]'] = np.ones(3, np.float32)
        c.context.clear((0.33, ) * 3)
        program.draw('triangle_strip')
        out = _screenshot()
        assert_allclose(out[:, :, 0] / 255.,
                        127.5 / 255. * np.ones(shape),
                        atol=1. / 255.)
    def draw(self):
        if self._glsl:
            fragment = fragment_template % self._glsl
            self._glsl = None

            # Check to see if the shader will compile successfully before we
            # set it. We do this here because the ShaderWatcher runs in a
            # different thread and so can't access the GL context.

            frag_handle = gl.glCreateShader(gl.GL_FRAGMENT_SHADER)
            gl.glShaderSource(frag_handle, fragment)
            gl.glCompileShader(frag_handle)
            status = gl.glGetShaderParameter(frag_handle, gl.GL_COMPILE_STATUS)
            if not status:
                errors = gl.glGetShaderInfoLog(frag_handle)
                errors = self.process_errors(errors)
                print("Shader failed to compile:", file=sys.stderr)
                print(errors, file=sys.stderr)

                # Switch to error shader

                self._glsl = error_shader
                self.update()
            else:
                self.program.set_shaders(vertex, fragment)
            gl.glDeleteShader(frag_handle)

        if self._interactive:
            self.program.draw()

            if self._ffmpeg_pipe is not None:
                img = _screenshot()
                self.write_video_frame(img)

            self._render_frame_index += 1
            if self._render_frame_count is not None and self._render_frame_index >= self._render_frame_count:
                app.quit()
                return

            self.advance_time()
        else:
            with self._fbo:
                rs = list(self._render_size)

                if self._tile_coord[0] + rs[0] > self._output_size[0]:
                    rs[0] = self._output_size[0] - self._tile_coord[0]

                if self._tile_coord[1] + rs[1] > self._output_size[1]:
                    rs[1] = self._output_size[1] - self._tile_coord[1]

                gloo.set_viewport(0, 0, *rs)
                self.program['iOffset'] = self._tile_coord
                self.program.draw()
                img = _screenshot()
                row = self._output_size[1] - self._tile_coord[1] - rs[1]
                col = self._tile_coord[0]
                self._img[row:row + rs[1], col:col + rs[0], :] = img
示例#7
0
    def draw(self):
        if self._glsl:
            fragment = fragment_template % self._glsl
            self._glsl = None

            # Check to see if the shader will compile successfully before we
            # set it. We do this here because the ShaderWatcher runs in a
            # different thread and so can't access the GL context.

            frag_handle = gl.glCreateShader(gl.GL_FRAGMENT_SHADER)
            gl.glShaderSource(frag_handle, fragment)
            gl.glCompileShader(frag_handle)
            status = gl.glGetShaderParameter(frag_handle, gl.GL_COMPILE_STATUS)
            if not status:
                errors = gl.glGetShaderInfoLog(frag_handle)
                errors = self.process_errors(errors)
                print("Shader failed to compile:", file=sys.stderr)
                print(errors, file=sys.stderr)

                # Switch to error shader

                self._glsl = error_shader
                self.update()
            else:
                self.program.set_shaders(vertex, fragment)
            gl.glDeleteShader(frag_handle)

        if self._interactive:
            self.program.draw()

            if self._ffmpeg_pipe is not None:
                img = _screenshot()
                self.write_video_frame(img)

            self._render_frame_index += 1
            if self._render_frame_count is not None and self._render_frame_index >= self._render_frame_count:
                app.quit()
                return

            self.advance_time()
        else:
            with self._fbo:
                rs = list(self._render_size)

                if self._tile_coord[0] + rs[0] > self._output_size[0]:
                    rs[0] = self._output_size[0] - self._tile_coord[0]

                if self._tile_coord[1] + rs[1] > self._output_size[1]:
                    rs[1] = self._output_size[1] - self._tile_coord[1]

                gloo.set_viewport(0, 0, *rs)
                self.program['iOffset'] = self._tile_coord
                self.program.draw()
                img = _screenshot()
                row = self._output_size[1] - self._tile_coord[1] - rs[1]
                col = self._tile_coord[0]
                self._img[row:row + rs[1], col:col + rs[0], :] = img
示例#8
0
def test_use_uniforms():
    """Test using uniform arrays"""
    VERT_SHADER = """
    attribute vec2 a_pos;
    varying vec2 v_pos;

    void main (void)
    {
        v_pos = a_pos;
        gl_Position = vec4(a_pos, 0., 1.);
    }
    """

    FRAG_SHADER = """
    varying vec2 v_pos;
    uniform vec3 u_color[2];

    void main()
    {
        gl_FragColor = vec4((u_color[0] + u_color[1]) / 2., 1.);
    }
    """
    shape = (500, 500)
    with Canvas(size=shape) as c:
        c.set_current()
        c.context.glir.set_verbose(True)
        assert_equal(c.size, shape[::-1])
        shape = (3, 3)
        set_viewport((0, 0) + shape)
        program = Program(VERT_SHADER, FRAG_SHADER)
        program['a_pos'] = [[-1., -1.], [1., -1.], [-1., 1.], [1., 1.]]
        program['u_color'] = np.ones((2, 3))
        c.context.clear('k')
        c.set_current()
        program.draw('triangle_strip')
        out = _screenshot()
        assert_allclose(out[:, :, 0] / 255., np.ones(shape), atol=1. / 255.)

        # now set one element
        program['u_color[1]'] = np.zeros(3, np.float32)
        c.context.clear('k')
        program.draw('triangle_strip')
        out = _screenshot()
        assert_allclose(out[:, :, 0] / 255., 127.5 / 255. * np.ones(shape),
                        atol=1. / 255.)

        # and the other
        assert_raises(ValueError, program.__setitem__, 'u_color',
                      np.zeros(3, np.float32))
        program['u_color'] = np.zeros((2, 3), np.float32)
        program['u_color[0]'] = np.ones(3, np.float32)
        c.context.clear((0.33,) * 3)
        program.draw('triangle_strip')
        out = _screenshot()
        assert_allclose(out[:, :, 0] / 255., 127.5 / 255. * np.ones(shape),
                        atol=1. / 255.)
示例#9
0
 def on_key_press(ev):
     print(ev.key.name)
     if ev.key.name in 'S':
         print("Saving...")
         res = _screenshot()
         vispy_file.write_png('config_space/{}_shot.png'.format(seq), res)
         print("Done")
示例#10
0
def screenshot(file='screenshot.png', alpha=True):
    """Save a screenshot of active vispy 3D canvas.

    Parameters
    ----------
    file :      str, optional
                Filename
    alpha :     bool, optional
                If True, alpha channel will be saved

    See Also
    --------
    :func:`navis.Viewer.screenshot`
                Take screenshot of specific canvas.

    """
    if alpha:
        mode = 'RGBA'
    else:
        mode = 'RGB'

    im = png.from_array(_screenshot(alpha=alpha), mode=mode)
    im.save(file)

    return
示例#11
0
def _update_process_check(canvas, val, paint=True, ignore_fail=False):
    """Update, process, and check result"""
    if paint:
        canvas.update()
        canvas.app.process_events()
        canvas.app.process_events()
    canvas._backend._vispy_set_current()
    print('           check %s' % val)
    # check screenshot to see if it's all one color
    ss = _screenshot()
    try:
        assert_allclose(ss.shape[:2], _win_size[::-1])
    except Exception:
        print('!!!!!!!!!! FAIL  bad size %s' % list(ss.shape[:2]))
        sleep(_err_sleep_time)
        if not ignore_fail:
            raise
    goal = val * np.ones(ss.shape)
    try:
        assert_allclose(ss, goal, atol=1)  # can be off by 1 due to rounding
    except Exception:
        print('!!!!!!!!!! FAIL  %s' % np.unique(ss))
        sleep(_err_sleep_time)
        if not ignore_fail:
            raise
示例#12
0
 def animation(self, t):
     """ Added for animation with MoviePy """
     self.program['u_clock'] = t
     gloo.clear('black')
     _, im = self.cap.read()
     self.program['texture'][...] = im
     self.program.draw('triangle_strip')
     return _screenshot((0, 0, self.size[0], self.size[1]))[:, :, :3]
示例#13
0
 def on_draw(self, event):
     # Render in the FBO.
     with self._fbo:
         gloo.clear((1,1,1,1))
         gloo.set_viewport(0, 0, *self.true_size)
         self.program.draw(gl.GL_TRIANGLE_STRIP)
         # Retrieve the contents of the FBO texture.
         self.shadowsArray = _screenshot((0, 0, self.true_size[0], self.true_size[1]))
     # Immediately exit the application.
     app.quit()
示例#14
0
def test_markers():
    """Test basic marker / point-sprite support"""
    # this is probably too basic, but it at least ensures that point sprites
    # work for people
    with TestingCanvas() as c:
        marker = Markers()
        marker.set_data(np.array([[50, 50]], np.float32))
        c.draw_visual(marker)
        marker = _screenshot(alpha=False)
        assert 10 < (marker == 255).sum() < 100
示例#15
0
def test_markers():
    """Test basic marker / point-sprite support"""
    # this is probably too basic, but it at least ensures that point sprites
    # work for people
    with TestingCanvas() as c:
        marker = Markers()
        marker.set_data(np.array([[50, 50]], np.float32))
        c.draw_visual(marker)
        marker = _screenshot(alpha=False)
        assert 10 < (marker == 255).sum() < 100
示例#16
0
def test_use_texture3D():
    """Test using a 3D texture"""
    vals = [0, 200, 100, 0, 255, 0, 100]
    d, h, w = len(vals), 3, 5
    data = np.zeros((d, h, w), np.float32)

    VERT_SHADER = """
    attribute vec2 a_pos;
    varying vec2 v_pos;

    void main (void)
    {
        v_pos = a_pos;
        gl_Position = vec4(a_pos, 0., 1.);
    }
    """

    FRAG_SHADER = """
    uniform sampler3D u_texture;
    varying vec2 v_pos;
    uniform float i;
    void main()
    {
        gl_FragColor = texture3D(u_texture,
                                 vec3((v_pos.y+1.)/2., (v_pos.x+1.)/2., i));
        gl_FragColor.a = 1.;
    }
    """
    # populate the depth "slices" with different gray colors in the bottom left
    for ii, val in enumerate(vals):
        data[ii, :2, :3] = val / 255.
    with Canvas(size=(100, 100)) as c:
        if not has_pyopengl():
            t = Texture3D(data)
            assert_raises(ImportError, t.glir.flush, c.context.shared.parser)
            return
        program = Program(VERT_SHADER, FRAG_SHADER)
        program['a_pos'] = [[-1., -1.], [1., -1.], [-1., 1.], [1., 1.]]
        tex = Texture3D(data, interpolation='nearest')
        assert_equal(tex.width, w)
        assert_equal(tex.height, h)
        assert_equal(tex.depth, d)
        program['u_texture'] = tex
        for ii, val in enumerate(vals):
            set_viewport(0, 0, w, h)
            clear(color='black')
            iii = (ii + 0.5) / float(d)
            print(ii, iii)
            program['i'] = iii
            program.draw('triangle_strip')
            out = _screenshot()[:, :, 0].astype(int)[::-1]
            expected = np.zeros_like(out)
            expected[:2, :3] = val
            assert_allclose(out, expected, atol=1. / 255.)
示例#17
0
def test_use_texture3D():
    """Test using a 3D texture"""
    vals = [0, 200, 100, 0, 255, 0, 100]
    d, h, w = len(vals), 3, 5
    data = np.zeros((d, h, w), np.float32)

    VERT_SHADER = """
    attribute vec2 a_pos;
    varying vec2 v_pos;

    void main (void)
    {
        v_pos = a_pos;
        gl_Position = vec4(a_pos, 0., 1.);
    }
    """

    FRAG_SHADER = """
    uniform sampler3D u_texture;
    varying vec2 v_pos;
    uniform float i;
    void main()
    {
        gl_FragColor = texture3D(u_texture,
                                 vec3((v_pos.y+1.)/2., (v_pos.x+1.)/2., i));
        gl_FragColor.a = 1.;
    }
    """
    # populate the depth "slices" with different gray colors in the bottom left
    for ii, val in enumerate(vals):
        data[ii, :2, :3] = val / 255.
    with Canvas(size=(100, 100)) as c:
        if not has_pyopengl():
            t = Texture3D(data)
            assert_raises(ImportError, t.glir.flush, c.context.shared.parser)
            return
        program = Program(VERT_SHADER, FRAG_SHADER)
        program['a_pos'] = [[-1., -1.], [1., -1.], [-1., 1.], [1., 1.]]
        tex = Texture3D(data, interpolation='nearest')
        assert_equal(tex.width, w)
        assert_equal(tex.height, h)
        assert_equal(tex.depth, d)
        program['u_texture'] = tex
        for ii, val in enumerate(vals):
            set_viewport(0, 0, w, h)
            clear(color='black')
            iii = (ii + 0.5) / float(d)
            print(ii, iii)
            program['i'] = iii
            program.draw('triangle_strip')
            out = _screenshot()[:, :, 0].astype(int)[::-1]
            expected = np.zeros_like(out)
            expected[:2, :3] = val
            assert_allclose(out, expected, atol=1./255.)
示例#18
0
 def on_draw(self, event):
     # Render in the FBO.
     with self._fbo:
         gloo.clear('black')
         gloo.set_viewport(0, 0, *self.size)
         self.program.draw()
         # Retrieve the contents of the FBO texture.
         self.im = _screenshot((0, 0, self.size[0], self.size[1]))
     self._time = time() - self._t0
     # Immediately exit the application.
     app.quit()
示例#19
0
文件: wrap.py 项目: zimka/vis_mol
def show_mol_surf(mol_obj=None,
                  surf_datas=None,
                  l_par=None,
                  reduced_mol=False,
                  **kwargs):
    canvas = scene.SceneCanvas(keys='interactive', bgcolor=(1, 1, 1, 1))
    view = canvas.central_widget.add_view()

    num = None
    if surf_datas is not None:
        if isinstance(surf_datas, tuple):
            colors = ColorIter(len(surf_datas))
            for data in surf_datas:
                if num is not None and len(data) != num:
                    raise TypeError("different dimensions of surfaces")
                num = len(data)
                build_surface(data=data, view=view, color=next(colors))
        else:
            build_surface(data=surf_datas, view=view, color=next(ColorIter(1)))
            num = len(surf_datas)

    if mol_obj is not None:
        mol = mol_obj.copy()
        if surf_datas is not None:
            if l_par is not None:
                mol.pos /= l_par
                mol.pos *= num
        vis_mol_wrap(mol, view=view, reduced_mol=reduced_mol)

    if num is None:
        num = 50
    sr = kwargs.get('setrange', num)
    cam = scene.TurntableCamera(elevation=30, azimuth=30)
    if kwargs.get('show_axis', True):
        axis = myXYZAxis(length=num + 10, parent=view.scene)
    cam.set_range((-sr, sr), (-sr, sr), (-sr, sr))
    view.camera = cam
    cube_size = kwargs.get('cube_size', 0.)
    scene.visuals.Cube(size=cube_size * num / 2,
                       color=(0.9, 0.9, 0.3, 0.4),
                       edge_color="black",
                       parent=view.scene)
    canvas.show()

    if kwargs.get('quit', False):
        app.process_events()
    else:
        app.run()

    if kwargs.get('screenshot', False):
        name = kwargs.get("screenname", 'screenshot.png')
        im = _screenshot((0, 0, canvas.size[0], canvas.size[1]))
        imsave(name, im)
示例#20
0
 def grabscreenshot(event):
     if m.done:
         return  # Grab only once
     m.frame += 1
     if m.frame in frames:
         frames.remove(m.frame)
         im = _screenshot((0, 0, c.size[0], c.size[1]))
         # Ensure we don't have alpha silliness
         im = np.array(im)
         im[:, :, 3] = 255
         m.images.append(im)
     if not frames:
         m.done = True
示例#21
0
 def screenshot(self, linewidth=26, start=0):
     with self.fbo:
         gloo.set_line_width(linewidth)
         gloo.set_viewport(0, 0, 2048, 2048)
         gloo.set_state(blend=True, depth_test=True)
         gloo.clear(color="white", depth=True)
         self.program.draw("lines", self.index_buffer)
         im = _screenshot((0, 0, 2048, 2048))[:, :, :3]
     for i in range(start, 9999):
         if not osp.exists(f"{self.prefix}_s{i:04d}.png"):
             print(f"Save to {self.prefix}_s{i:04d}.png")
             cv2.imwrite(f"{self.prefix}_s{i:04d}.png", im[:, :, ::-1])
             break
示例#22
0
文件: make.py 项目: rougier/vispy
 def grabscreenshot(event):
     if m.done:
         return  # Grab only once
     m.frame += 1
     if m.frame in frames:
         frames.remove(m.frame)
         im = _screenshot((0, 0, c.size[0], c.size[1]))
         # Ensure we don't have alpha silliness
         im = np.array(im)
         im[:, :, 3] = 255
         m.images.append(im)
     if not frames:
         m.done = True
示例#23
0
def test_use_framebuffer():
    """Test drawing to a framebuffer"""
    shape = (100, 300)  # for some reason Windows wants a tall window...
    data = np.random.rand(*shape).astype(np.float32)
    use_shape = shape + (3, )
    with Canvas(size=shape[::-1]) as c:
        c.app.process_events()
        c.set_current()
        if c.app.backend_name.lower() == 'pyqt5':
            # PyQt5 on OSX for some reason sets this to 1024x768...
            c.size = shape[::-1]
        c.app.process_events()
        orig_tex = Texture2D(data)
        fbo_tex = Texture2D(use_shape, format='rgb')
        rbo = RenderBuffer(shape, 'color')
        fbo = FrameBuffer(color=fbo_tex)
        c.context.glir.set_verbose(True)
        assert c.size == shape[::-1]
        c.set_current()
        set_viewport((0, 0) + c.size)
        with fbo:
            draw_texture(orig_tex)
        draw_texture(fbo_tex)
        out_tex = _screenshot()[::-1, :, 0].astype(np.float32)
        assert out_tex.shape == c.size[::-1]
        assert_raises(TypeError, FrameBuffer.color_buffer.fset, fbo, 1.)
        assert_raises(TypeError, FrameBuffer.depth_buffer.fset, fbo, 1.)
        assert_raises(TypeError, FrameBuffer.stencil_buffer.fset, fbo, 1.)
        fbo.color_buffer = rbo
        fbo.depth_buffer = RenderBuffer(shape)
        fbo.stencil_buffer = None
        print((fbo.color_buffer, fbo.depth_buffer, fbo.stencil_buffer))
        clear(color='black')
        with fbo:
            clear(color='black')
            draw_texture(orig_tex)
            out_rbo = _screenshot()[:, :, 0].astype(np.float32)
    assert_allclose(data * 255., out_tex, atol=1)
    assert_allclose(data * 255., out_rbo, atol=1)
示例#24
0
    def write(self):
        image = _screenshot(alpha=True)
        write_png('covers2/%s.png' % self.time, image)

        data = {}
        data.update(self.input_manager.smoothed_inputs)
        data['time'] = self.time
        data['definition'] = self.fractal.definition['name']

        with open('covers2/%s.yml' % self.time, 'w') as f:
            yaml.dump(data, stream=f)

        print "Wrote image and data for %s" % self.time
示例#25
0
def test_use_framebuffer():
    """Test drawing to a framebuffer"""
    shape = (100, 300)  # for some reason Windows wants a tall window...
    data = np.random.rand(*shape).astype(np.float32)
    use_shape = shape + (3,)
    with Canvas(size=shape[::-1]) as c:
        c.app.process_events()
        c.set_current()
        if c.app.backend_name.lower() == 'pyqt5':
            # PyQt5 on OSX for some reason sets this to 1024x768...
            c.size = shape[::-1]
        c.app.process_events()
        orig_tex = Texture2D(data)
        fbo_tex = Texture2D(use_shape, format='rgb')
        rbo = RenderBuffer(shape, 'color')
        fbo = FrameBuffer(color=fbo_tex)
        c.context.glir.set_verbose(True)
        assert c.size == shape[::-1]
        c.set_current()
        set_viewport((0, 0) + c.size)
        with fbo:
            draw_texture(orig_tex)
        draw_texture(fbo_tex)
        out_tex = _screenshot()[::-1, :, 0].astype(np.float32)
        assert out_tex.shape == c.size[::-1]
        assert_raises(TypeError, FrameBuffer.color_buffer.fset, fbo, 1.)
        assert_raises(TypeError, FrameBuffer.depth_buffer.fset, fbo, 1.)
        assert_raises(TypeError, FrameBuffer.stencil_buffer.fset, fbo, 1.)
        fbo.color_buffer = rbo
        fbo.depth_buffer = RenderBuffer(shape)
        fbo.stencil_buffer = None
        print((fbo.color_buffer, fbo.depth_buffer, fbo.stencil_buffer))
        clear(color='black')
        with fbo:
            clear(color='black')
            draw_texture(orig_tex)
            out_rbo = _screenshot()[:, :, 0].astype(np.float32)
    assert_allclose(data * 255., out_tex, atol=1)
    assert_allclose(data * 255., out_rbo, atol=1)
示例#26
0
    def on_key_press(self, event):
        if event.key == "q":
            self.show(False, False)
            self.app.quit()
            return
        elif event.key == "p" or event.key == " ":
            self._paused = not self._paused
            self.update_timer_state()
        elif event.key == "s":
            img = _screenshot()
            self.write_img(img)
        elif event.key == "a":
            print("Size/pos args: --size %dx%d --pos %d,%d" %
                  (self.physical_size[0],
                   self.physical_size[1],
                   self.position[0],
                   self.position[1]))
        elif event.key == "f":
            self._profile = not self._profile
            if self._profile:
                def print_profile(fps):
                    print("%.2f ms/frame" % (1000.0 / float(fps)))
                    return False

                self.measure_fps(1.0, print_profile)
            else:
                self.measure_fps(1.0, False)
            self.update_timer_state()

        elif event.key == keys.LEFT or event.key == keys.RIGHT:
            self._paused = True
            self.update_timer_state()
            step = 1.0 / 60.0
            if keys.ALT in event.modifiers:
                step *= 0.1
                if keys.SHIFT in event.modifiers:
                    step *= 0.1
            else:
                if keys.SHIFT in event.modifiers:
                    step *= 10.0
                if keys.CONTROL in event.modifiers:
                    step *= 100.0

            if event.key == keys.LEFT:
                step *= -1.0

            self.program['iGlobalTime'] += step

            self.print_t()

            self.update()
示例#27
0
    def on_key_press(self, event):
        if event.key == "q":
            self.show(False, False)
            self.app.quit()
            return
        elif event.key == "p" or event.key == " ":
            self._paused = not self._paused
            self.update_timer_state()
        elif event.key == "s":
            img = _screenshot()
            self.write_img(img)
        elif event.key == "a":
            print("Size/pos args: --size %dx%d --pos %d,%d" %
                  (self.physical_size[0],
                   self.physical_size[1],
                   self.position[0],
                   self.position[1]))
        elif event.key == "f":
            self._profile = not self._profile
            if self._profile:
                def print_profile(fps):
                    print("%.2f ms/frame" % (1000.0 / float(fps)))
                    return False

                self.measure_fps(1.0, print_profile)
            else:
                self.measure_fps(1.0, False)
            self.update_timer_state()

        elif event.key == keys.LEFT or event.key == keys.RIGHT:
            self._paused = True
            self.update_timer_state()
            step = 1.0 / 60.0
            if keys.ALT in event.modifiers:
                step *= 0.1
                if keys.SHIFT in event.modifiers:
                    step *= 0.1
            else:
                if keys.SHIFT in event.modifiers:
                    step *= 10.0
                if keys.CONTROL in event.modifiers:
                    step *= 100.0

            if event.key == keys.LEFT:
                step *= -1.0

            self.program['time'] += step

            self.print_t()

            self.update()
示例#28
0
 def on_draw(self, _):
     if self._done:
         return  # Grab only once
     self._current_frame += 1
     if self._current_frame in self._frames_to_grab:
         self._frames_to_grab.remove(self._current_frame)
         if isinstance(self._canvas, SceneCanvas):
             im = self._canvas.render(alpha=True)
         else:
             im = _screenshot()
         self._collected_images.append(im)
     if not self._frames_to_grab or self._current_frame > self._frames_to_grab[
             0]:
         self._done = True
示例#29
0
    def process_frame(self, tex):
        print('process_frame w/shader')
        with self._fbo:

            self.program['u_tex1'] = gloo.Texture2D(tex,
                                                    interpolation='linear')
            self.program.bind(gloo.VertexBuffer(data))

            self.program.draw('triangle_strip')

            self.sc = _screenshot((0, 0, self.size[0], self.size[1]),
                                  alpha=False)

        self._time = time() - self._t0
示例#30
0
 def on_draw(self, event):
     if args.outputfile:
         faces = [[0, 0], [90, 0], [180, 0], [270, 0], [0, 90], [0, 270]]
         self.model = numpy.dot(rotate(faces[args.faces - 1][0], (0, 0, 1)),
                                rotate(faces[args.faces - 1][0], (0, 1, 0)))
         self.shader_nodes['u_model'] = self.model
         self.shader_traces['u_model'] = self.model
     gloo.clear()
     self.program = self.shader_nodes
     self.program.draw('points')
     self.program = self.shader_traces
     self.program.draw('lines')
     if args.outputfile:
         self.im = _screenshot((0, 0, self.size[0], self.size[1]))
         app.quit()
示例#31
0
def make_frame(t):
    global fnum
    # paramter t is time in seconds. so for 20 fps every second
    # has 20 calls. this is not the frame number
    # so multiply by some factor to get a faster movie
    surface.set_data(z = Z(fnum)) # Update the mathematical surface
    fnum += .25
    # optionally change color ...
    surface.color = (np.clip(1.-fnum/20,0,1),1,np.clip(fnum/20,0,1),1)
    canvas.bgcolor=(0,np.clip(.3-(fnum/20),0,1),.3,1)
    # optionally call event loop. this will make the video
    ## follow the user interactions .... 
    app.process_events()
    # draw and capture
    canvas.on_draw(None) # Update the image on Vispy's canvas
    return _screenshot((0,0,canvas.size[0],canvas.size[1]))[:,:,:3]
def _update_process_check(canvas, val, draw=True):
    """Update, process, and check result"""
    if draw:
        canvas.update()
        canvas.app.process_events()
        canvas.app.process_events()
        sleep(0.03)  # give it time to swap (Qt?)
    canvas._backend._vispy_set_current()
    print('           check %s' % val)
    # check screenshot to see if it's all one color
    ss = _screenshot()
    try:
        assert_allclose(ss.shape[:2], _win_size[::-1])
    except Exception:
        print('!!!!!!!!!! FAIL  bad size %s' % list(ss.shape[:2]))
        raise
    goal = val * np.ones(ss.shape)
    try:
        assert_allclose(ss, goal, atol=1)  # can be off by 1 due to rounding
    except Exception:
        print('!!!!!!!!!! FAIL  %s' % np.unique(ss))
        raise
示例#33
0
def _update_process_check(canvas, val, draw=True):
    """Update, process, and check result"""
    if draw:
        canvas.update()
        canvas.app.process_events()
        canvas.app.process_events()
        sleep(0.03)  # give it time to swap (Qt?)
    canvas._backend._vispy_set_current()
    print('           check %s' % val)
    # check screenshot to see if it's all one color
    ss = _screenshot()
    try:
        assert_allclose(ss.shape[:2], _win_size[::-1])
    except Exception:
        print('!!!!!!!!!! FAIL  bad size %s' % list(ss.shape[:2]))
        raise
    goal = val * np.ones(ss.shape)
    try:
        # Get rid of the alpha value before testing
        # It can be off by 1 due to rounding
        assert_allclose(ss[:, :, :3], goal[:, :, :3], atol=1)
    except Exception:
        print('!!!!!!!!!! FAIL  %s' % np.unique(ss))
        raise
示例#34
0
文件: test_app.py 项目: vanossj/vispy
def test_application():
    """Test application running"""
    app = use_app()
    print(app)  # __repr__ without app
    app.create()
    wrong = 'glfw' if app.backend_name.lower() != 'glfw' else 'pyqt4'
    assert_raises(RuntimeError, use_app, wrong)
    app.process_events()
    print(app)  # test __repr__

    assert_raises(ValueError, Canvas, keys='foo')
    assert_raises(TypeError, Canvas, keys=dict(escape=1))
    assert_raises(ValueError, Canvas, keys=dict(escape='foo'))  # not an attr

    pos = [0, 0] if app.backend_module.capability['position'] else None
    size = (100, 100)
    # Use "with" statement so failures don't leave open window
    # (and test context manager behavior)
    title = 'default'
    with Canvas(title=title, size=size, app=app, show=True,
                position=pos) as canvas:
        context = canvas.context
        assert_true(canvas.create_native() is None)  # should be done already
        assert_is(canvas.app, app)
        assert_true(canvas.native)
        assert_equal('swap_buffers', canvas.events.draw.callback_refs[-1])

        canvas.measure_fps(0.001)
        sleep(0.002)
        canvas.update()
        app.process_events()
        assert_true(canvas.fps > 0)

        # Other methods
        print(canvas)  # __repr__
        assert_equal(canvas.title, title)
        canvas.title = 'you'
        with use_log_level('warning', record=True, print_msg=False) as l:
            if app.backend_module.capability['position']:
                # todo: disable more tests based on capability
                canvas.position = pos
            canvas.size = size
        if 'ipynb_vnc' in canvas.app.backend_name.lower():
            assert_true(len(l) >= 1)
        else:
            assert_true(len(l) == 0)
        canvas.connect(on_mouse_move)
        assert_raises(ValueError, canvas.connect, _on_mouse_move)
        if sys.platform != 'darwin':  # XXX knownfail, prob. needs warmup
            canvas.show(False)
            canvas.show()
        app.process_events()
        assert_raises(ValueError, canvas.connect, on_nonexist)
        # deprecation of "paint"
        with use_log_level('info', record=True, print_msg=False) as log:
            olderr = sys.stderr
            try:
                fid = StringIO()
                sys.stderr = fid

                @canvas.events.paint.connect
                def fake(event):
                    pass
            finally:
                sys.stderr = olderr
        assert_equal(len(log), 1)
        assert_in('deprecated', log[0])

        # screenshots
        gl.glViewport(0, 0, *size)
        ss = _screenshot()
        assert_array_equal(ss.shape, size + (4,))
        assert_equal(len(canvas._backend._vispy_get_geometry()), 4)
        if sys.platform != 'win32':  # XXX knownfail for windows
            assert_array_equal(canvas.size, size)
        assert_equal(len(canvas.position), 2)  # XXX knawnfail, doesn't "take"

        # GLOO: should have an OpenGL context already, so these should work
        vert = "void main (void) {gl_Position = pos;}"
        frag = "void main (void) {gl_FragColor = pos;}"
        program = Program(vert, frag)
        assert_raises(RuntimeError, program.glir.flush, context.shared.parser)
        
        vert = "uniform vec4 pos;\nvoid main (void) {gl_Position = pos;}"
        frag = "uniform vec4 pos;\nvoid main (void) {gl_FragColor = pos;}"
        program = Program(vert, frag)
        #uniform = program.uniforms[0]
        program['pos'] = [1, 2, 3, 4]
        
        vert = "attribute vec4 pos;\nvoid main (void) {gl_Position = pos;}"
        frag = "void main (void) {}"
        program = Program(vert, frag)
        #attribute = program.attributes[0]
        program["pos"] = [1, 2, 3, 4]
        
        # use a real program
        program._glir.clear()
        vert = ("uniform mat4 u_model;"
                "attribute vec2 a_position; attribute vec4 a_color;"
                "varying vec4 v_color;"
                "void main (void) {v_color = a_color;"
                "gl_Position = u_model * vec4(a_position, 0.0, 1.0);"
                "v_color = a_color;}")
        frag = "void main() {gl_FragColor = vec4(0, 0, 0, 1);}"
        n, p = 250, 50
        T = np.random.uniform(0, 2 * np.pi, n)
        position = np.zeros((n, 2), dtype=np.float32)
        position[:, 0] = np.cos(T)
        position[:, 1] = np.sin(T)
        color = np.ones((n, 4), dtype=np.float32) * (1, 1, 1, 1)
        data = np.zeros(n * p, [('a_position', np.float32, 2),
                                ('a_color', np.float32, 4)])
        data['a_position'] = np.repeat(position, p, axis=0)
        data['a_color'] = np.repeat(color, p, axis=0)

        program = Program(vert, frag)
        program.bind(VertexBuffer(data))
        program['u_model'] = np.eye(4, dtype=np.float32)
        # different codepath if no call to activate()
        program.draw(gl.GL_POINTS)
        subset = IndexBuffer(np.arange(10, dtype=np.uint32))
        program.draw(gl.GL_POINTS, subset)

        # bad programs
        frag_bad = ("varying vec4 v_colors")  # no semicolon
        program = Program(vert, frag_bad)
        assert_raises(RuntimeError, program.glir.flush, context.shared.parser)
        frag_bad = None  # no fragment code. no main is not always enough
        assert_raises(ValueError, Program, vert, frag_bad)

        # Timer
        timer = Timer(interval=0.001, connect=on_mouse_move, iterations=2,
                      start=True, app=app)
        timer.start()
        timer.interval = 0.002
        assert_equal(timer.interval, 0.002)
        assert_true(timer.running)
        sleep(.003)
        assert_true(timer.elapsed >= 0.002)
        timer.stop()
        assert_true(not timer.running)
        assert_true(timer.native)
        timer.disconnect()

        # test that callbacks take reasonable inputs
        _test_callbacks(canvas)

        # cleanup
        canvas.swap_buffers()
        canvas.update()
        app.process_events()
        # put this in even though __exit__ will call it to make sure we don't
        # have problems calling it multiple times
        canvas.close()  # done by context
示例#35
0
文件: test_app.py 项目: Zulko/vispy
def test_application():
    """Test application running"""
    app = use_app()
    print(app)  # __repr__ without app
    app.create()
    wrong = 'glut' if app.backend_name.lower() != 'glut' else 'pyglet'
    assert_raises(RuntimeError, use_app, wrong)
    app.process_events()
    print(app)  # test __repr__

    assert_raises(ValueError, Canvas, keys='foo')
    assert_raises(TypeError, Canvas, keys=dict(escape=1))
    assert_raises(ValueError, Canvas, keys=dict(escape='foo'))  # not an attr

    pos = [0, 0] if app.backend_module.capability['position'] else None
    size = (100, 100)
    # Use "with" statement so failures don't leave open window
    # (and test context manager behavior)
    title = 'default'
    with Canvas(title=title, size=size, app=app, show=True,
                position=pos) as canvas:
        assert_true(canvas.create_native() is None)  # should be done already
        assert_is(canvas.app, app)
        assert_true(canvas.native)
        assert_equal('swap_buffers', canvas.events.draw.callback_refs[-1])

        canvas.measure_fps(0.001)
        sleep(0.002)
        canvas.update()
        app.process_events()
        assert_true(canvas.fps > 0)

        # Other methods
        print(canvas)  # __repr__
        assert_equal(canvas.title, title)
        canvas.title = 'you'
        with use_log_level('warning', record=True, print_msg=False) as l:
            if app.backend_module.capability['position']:
                # todo: disable more tests based on capability
                canvas.position = pos
            canvas.size = size
        if 'ipynb_vnc' in canvas.app.backend_name.lower():
            assert_true(len(l) >= 1)
        else:
            assert_true(len(l) == 0)
        canvas.connect(on_mouse_move)
        assert_raises(ValueError, canvas.connect, _on_mouse_move)
        if sys.platform != 'darwin':  # XXX knownfail, prob. needs warmup
            canvas.show(False)
            canvas.show()
        app.process_events()
        assert_raises(ValueError, canvas.connect, on_nonexist)
        # deprecation of "paint"
        with use_log_level('info', record=True, print_msg=False) as log:
            olderr = sys.stderr
            try:
                with open(os.devnull, 'w') as fid:
                    sys.stderr = fid

                    @canvas.events.paint.connect
                    def fake(event):
                        pass
            finally:
                sys.stderr = olderr
        assert_equal(len(log), 1)
        assert_in('deprecated', log[0])

        # screenshots
        gl.glViewport(0, 0, *size)
        ss = _screenshot()
        assert_array_equal(ss.shape, size + (4,))
        assert_equal(len(canvas._backend._vispy_get_geometry()), 4)
        if (app.backend_name.lower() != 'glut' and  # XXX knownfail for Almar
                sys.platform != 'win32'):  # XXX knownfail for windows
            assert_array_equal(canvas.size, size)
        assert_equal(len(canvas.position), 2)  # XXX knawnfail, doesn't "take"

        # GLOO: should have an OpenGL context already, so these should work
        vert = VertexShader("void main (void) {gl_Position = pos;}")
        frag = FragmentShader("void main (void) {gl_FragColor = pos;}")
        program = Program(vert, frag)
        assert_raises(RuntimeError, program.activate)

        vert = VertexShader("uniform vec4 pos;"
                            "void main (void) {gl_Position = pos;}")
        frag = FragmentShader("uniform vec4 pos;"
                              "void main (void) {gl_FragColor = pos;}")
        program = Program(vert, frag)
        #uniform = program.uniforms[0]
        program['pos'] = [1, 2, 3, 4]
        program.activate()  # should print
        #uniform.upload(program)
        program.detach(vert)
        program.detach(frag)
        assert_raises(RuntimeError, program.detach, vert)
        assert_raises(RuntimeError, program.detach, frag)

        vert = VertexShader("attribute vec4 pos;"
                            "void main (void) {gl_Position = pos;}")
        frag = FragmentShader("void main (void) {}")
        program = Program(vert, frag)
        #attribute = program.attributes[0]
        program["pos"] = [1, 2, 3, 4]
        program.activate()
        #attribute.upload(program)
        # cannot get element count
        #assert_raises(RuntimeError, program.draw, 'POINTS')

        # use a real program
        vert = ("uniform mat4 u_model;"
                "attribute vec2 a_position; attribute vec4 a_color;"
                "varying vec4 v_color;"
                "void main (void) {v_color = a_color;"
                "gl_Position = u_model * vec4(a_position, 0.0, 1.0);"
                "v_color = a_color;}")
        frag = "void main() {gl_FragColor = vec4(0, 0, 0, 1);}"
        n, p = 250, 50
        T = np.random.uniform(0, 2 * np.pi, n)
        position = np.zeros((n, 2), dtype=np.float32)
        position[:, 0] = np.cos(T)
        position[:, 1] = np.sin(T)
        color = np.ones((n, 4), dtype=np.float32) * (1, 1, 1, 1)
        data = np.zeros(n * p, [('a_position', np.float32, 2),
                                ('a_color', np.float32, 4)])
        data['a_position'] = np.repeat(position, p, axis=0)
        data['a_color'] = np.repeat(color, p, axis=0)

        program = Program(vert, frag)
        program.bind(VertexBuffer(data))
        program['u_model'] = np.eye(4, dtype=np.float32)
        # different codepath if no call to activate()
        program.draw(gl.GL_POINTS)
        subset = IndexBuffer(np.arange(10, dtype=np.uint32))
        program.draw(gl.GL_POINTS, subset)

        # bad programs
        frag_bad = ("varying vec4 v_colors")  # no semicolon
        program = Program(vert, frag_bad)
        assert_raises(RuntimeError, program.activate)
        frag_bad = None  # no fragment code. no main is not always enough
        program = Program(vert, frag_bad)
        assert_raises(ValueError, program.activate)

        # Timer
        timer = Timer(interval=0.001, connect=on_mouse_move, iterations=2,
                      start=True, app=app)
        timer.start()
        timer.interval = 0.002
        assert_equal(timer.interval, 0.002)
        assert_true(timer.running)
        sleep(.003)
        assert_true(timer.elapsed >= 0.002)
        timer.stop()
        assert_true(not timer.running)
        assert_true(timer.native)
        timer.disconnect()

        # test that callbacks take reasonable inputs
        _test_callbacks(canvas)

        # cleanup
        canvas.swap_buffers()
        canvas.update()
        app.process_events()
        # put this in even though __exit__ will call it to make sure we don't
        # have problems calling it multiple times
        canvas.close()  # done by context
示例#36
0
文件: test_app.py 项目: kif/vispy
def _test_application(backend):
    """Test application running"""
    app = Application()
    assert_raises(ValueError, app.use, "foo")
    app.use(backend)
    wrong = "Glut" if app.backend_name != "Glut" else "Pyglet"
    assert_raises(RuntimeError, app.use, wrong)
    app.process_events()
    if backend is not None:
        # "in" b/c "qt" in "PySide (qt)"
        assert_in(backend, app.backend_name)
    print(app)  # test __repr__

    # Canvas
    pos = [0, 0]
    size = (100, 100)
    # Use "with" statement so failures don't leave open window
    # (and test context manager behavior)
    title = "default" if backend is None else backend
    with Canvas(title=title, size=size, app=app, show=True, position=pos) as canvas:
        assert_is(canvas.app, app)
        assert_true(canvas.native)
        assert_equal("swap_buffers", canvas.events.paint.callback_refs[-1])
        print(canvas)  # __repr__
        assert_array_equal(canvas.size, size)
        assert_equal(canvas.title, title)
        canvas.title = "you"
        canvas.position = pos
        canvas.size = size
        canvas.connect(on_mouse_move)
        assert_raises(ValueError, canvas.connect, _on_mouse_move)
        if sys.platform != "darwin":  # XXX knownfail, prob. needs warmup
            canvas.show(False)
            canvas.show()
        app.process_events()
        assert_raises(ValueError, canvas.connect, on_nonexist)

        # screenshots
        gl.glViewport(0, 0, *size)
        ss = _screenshot()
        assert_array_equal(ss.shape, size + (3,))
        assert_equal(len(canvas._backend._vispy_get_geometry()), 4)
        assert_array_equal(canvas.size, size)
        assert_equal(len(canvas.position), 2)  # XXX knawnfail, doesn't "take"

        # GLOO: should have an OpenGL context already, so these should work
        vert = VertexShader("void main (void) {gl_Position = pos;}")
        frag = FragmentShader("void main (void) {gl_FragColor = pos;}")
        program = Program(vert, frag)
        assert_raises(RuntimeError, program.activate)

        vert = VertexShader("uniform vec4 pos;" "void main (void) {gl_Position = pos;}")
        frag = FragmentShader("uniform vec4 pos;" "void main (void) {gl_FragColor = pos;}")
        program = Program(vert, frag)
        # uniform = program.uniforms[0]
        program["pos"] = [1, 2, 3, 4]
        program.activate()  # should print
        # uniform.upload(program)
        program.detach(vert)
        program.detach(frag)
        assert_raises(RuntimeError, program.detach, vert)
        assert_raises(RuntimeError, program.detach, frag)

        vert = VertexShader("attribute vec4 pos;" "void main (void) {gl_Position = pos;}")
        frag = FragmentShader("void main (void) {}")
        program = Program(vert, frag)
        # attribute = program.attributes[0]
        program["pos"] = [1, 2, 3, 4]
        program.activate()
        # attribute.upload(program)
        # cannot get element count
        # assert_raises(RuntimeError, program.draw, 'POINTS')

        # use a real program
        vert = (
            "uniform mat4 u_model;"
            "attribute vec2 a_position; attribute vec4 a_color;"
            "varying vec4 v_color;"
            "void main (void) {v_color = a_color;"
            "gl_Position = u_model * vec4(a_position, 0.0, 1.0);"
            "v_color = a_color;}"
        )
        frag = "void main() {gl_FragColor = vec4(0, 0, 0, 1);}"
        n, p = 250, 50
        T = np.random.uniform(0, 2 * np.pi, n)
        position = np.zeros((n, 2), dtype=np.float32)
        position[:, 0] = np.cos(T)
        position[:, 1] = np.sin(T)
        color = np.ones((n, 4), dtype=np.float32) * (1, 1, 1, 1)
        data = np.zeros(n * p, [("a_position", np.float32, 2), ("a_color", np.float32, 4)])
        data["a_position"] = np.repeat(position, p, axis=0)
        data["a_color"] = np.repeat(color, p, axis=0)

        program = Program(vert, frag)
        program.bind(VertexBuffer(data))
        program["u_model"] = np.eye(4, dtype=np.float32)
        # different codepath if no call to activate()
        program.draw(gl.GL_POINTS)
        subset = IndexBuffer(np.arange(10, dtype=np.uint32))
        program.draw(gl.GL_POINTS, subset)

        # bad programs
        frag_bad = "varying vec4 v_colors"  # no semicolon
        program = Program(vert, frag_bad)
        assert_raises(RuntimeError, program.activate)
        frag_bad = None  # no fragment code. no main is not always enough
        program = Program(vert, frag_bad)
        assert_raises(ValueError, program.activate)

        # Timer
        timer = Timer(interval=0.001, connect=on_mouse_move, iterations=2, start=True, app=app)
        timer.start()
        timer.interval = 0.002
        assert_equal(timer.interval, 0.002)
        assert_true(timer.running)
        timer.stop()
        assert_true(not timer.running)
        assert_true(timer.native)
        timer.disconnect()

        # test that callbacks take reasonable inputs
        _test_callbacks(canvas)

        # cleanup
        canvas.swap_buffers()
        canvas.update()
        app.process_events()
示例#37
0
 def movie_animate(self, t):
     self.program['u_time'] = t
     self.update()
     gloo.clear()
     self.program.draw('triangle_strip')
     return _screenshot((0, 0, self.size[0], self.size[1]))[:, :, :3]
示例#38
0
 def on_key_press(self, event):
     # Hold <Ctrl> to enter drag mode.
     if keys.CONTROL in event.modifiers:
         # TODO: I cannot get the mouse position within the key_press event ...
         # so it is not yet implemented. The purpose of this event handler
         # is simply trying to highlight the visual node when <Ctrl> is pressed
         # but mouse is not moved (just nicer interactivity), so not very
         # high priority now.
         pass
     # Press <Space> to reset camera.
     if event.text == ' ':
         self.camera.fov = self.fov
         self.camera.azimuth = self.azimuth
         self.camera.elevation = self.elevation
         self.camera.set_range()
         self.camera.scale_factor = self.scale_factor
         self.camera.scale_factor /= self.zoom_factor
         for child in self.view.children:
             if type(child) == XYZAxis:
                 child._update_axis()
     # Press <s> to save a screenshot.
     if event.text == 's':
         screenshot = _screenshot()
         io.write_png(self.title + '.png', screenshot)
     # Press <d> to toggle drag mode.
     if event.text == 'd':
         if not self.drag_mode:
             self.drag_mode = True
             self.camera.viewbox.events.mouse_move.disconnect(
                 self.camera.viewbox_mouse_event)
         else:
             self.drag_mode = False
             self._exit_drag_mode()
             self.camera.viewbox.events.mouse_move.connect(
                 self.camera.viewbox_mouse_event)
     # Press <a> to get the parameters of all visual nodes.
     if event.text == 'a':
         print("===== All useful parameters ====")
         # Canvas size.
         print("Canvas size = {}".format(self.size))
         # Collect camera parameters.
         print("Camera:")
         camera_state = self.camera.get_state()
         for key, value in camera_state.items():
             print(" - {} = {}".format(key, value))
         print(" - {} = {}".format('zoom factor', self.zoom_factor))
         # Collect slice parameters.
         print("Slices:")
         pos_dict = {'x': [], 'y': [], 'z': []}
         for node in self.view.scene.children:
             if type(node) == AxisAlignedImage:
                 pos = node.pos
                 if node.seismic_coord_system and node.axis in ['y', 'z']:
                     pos = node.limit[1] - pos  # revert y and z axis
                 pos_dict[node.axis].append(pos)
         for axis, pos in pos_dict.items():
             print(" - {}: {}".format(axis, pos))
         # Collect the axis legend parameters.
         for node in self.view.children:
             if type(node) == XYZAxis:
                 print("XYZAxis loc = {}".format(node.loc))
示例#39
0
 def animation(self, t):
     self.on_draw(DrawEvent('draw'))
     return _screenshot((0, 0, self.size[0], self.size[1]))[:, :, :3]
    def draw(self):
        for i in range(4):
            if self._bufXglsl[i]:
                fragment = fragment_template % self._bufXglsl[i]
                self._bufXglsl[i] = None
                frag_handle = gl.glCreateShader(gl.GL_FRAGMENT_SHADER)
                gl.glShaderSource(frag_handle, fragment)
                gl.glCompileShader(frag_handle)
                status = gl.glGetShaderParameter(frag_handle,
                                        gl.GL_COMPILE_STATUS)
                if not status:
                    errors = gl.glGetShaderInfoLog(frag_handle)
                    errors = self.process_errors(errors)
                    print('Shader failed to compile:', file=sys.stderr)
                    print(errors, file=sys.stderr)
                    exit(1)
                else:
                    self._BufX[i].set_shaders(vertex, fragment)
                gl.glDeleteShader(frag_handle)
            
        if self._glsl:
            fragment = fragment_template % self._glsl
            self._glsl = None

            # Check to see if the shader will compile successfully before we
            # set it. We do this here because the ShaderWatcher runs in a
            # different thread and so can't access the GL context.

            frag_handle = gl.glCreateShader(gl.GL_FRAGMENT_SHADER)
            gl.glShaderSource(frag_handle, fragment)
            gl.glCompileShader(frag_handle)
            status = gl.glGetShaderParameter(frag_handle,
                    gl.GL_COMPILE_STATUS)
            if not status:
                errors = gl.glGetShaderInfoLog(frag_handle)
                errors = self.process_errors(errors)
                print('Shader failed to compile:', file=sys.stderr)
                print(errors, file=sys.stderr)
                exit(1)
                
                # Switch to error shader

                self._glsl = error_shader
                self.update()
            else:
                self.program.set_shaders(vertex, fragment)
            gl.glDeleteShader(frag_handle)

        if self._interactive:
            for i in range(4):
                with self._fboX[self._doubleFboid][i]:
                    gloo.set_clear_color((0.0, 0.0, 0.0, 0.0))
                    gloo.clear(color=True, depth=True)
                    gloo.set_viewport(0, 0, *self.physical_size)
                    self._BufX[i].draw()
            
            self.program.draw()

            if self._ffmpeg_pipe is not None:
                img = _screenshot()
                self.write_video_frame(img)

            self._render_frame_index += 1
            if self._render_frame_count is not None \
                and self._render_frame_index \
                >= self._render_frame_count:
                app.quit()
                return

            self._doubleFbo = not self._doubleFbo
            self._doubleFboid=(0 if self._doubleFbo else 1)
            self.advance_time()
            self.program['iFrame'] = self._render_frame_index
            self.set_Buf_uniform('iFrame' , self._render_frame_index)
            self.set_channel_input()
            self.set_Buf_channel_input()
        else:
            for i in range(4):
                with self._fboX[self._doubleFboid][i]:
                    gloo.set_clear_color((0.0, 0.0, 0.0, 0.0))
                    gloo.clear(color=True, depth=True)
                    gloo.set_viewport(0, 0, *self.physical_size)
                    self._BufX[i].draw()
                    
            with self._fbo:
                rs = list(self._render_size)

                if self._tile_coord[0] + rs[0] > self._output_size[0]:
                    rs[0] = self._output_size[0] - self._tile_coord[0]

                if self._tile_coord[1] + rs[1] > self._output_size[1]:
                    rs[1] = self._output_size[1] - self._tile_coord[1]

                gloo.set_viewport(0, 0, *rs)
                self.program['iOffset'] = self._tile_coord
                self.program.draw()
                self._doubleFbo = not self._doubleFbo
                self._doubleFboid=(0 if self._doubleFbo else 1)
                self.program['iFrame'] = self._render_frame_index
                self.set_Buf_uniform('iFrame' , self._render_frame_index)
                self.set_channel_input()
                self.set_Buf_channel_input()
                img = _screenshot()
                row = self._output_size[1] - self._tile_coord[1] - rs[1]
                col = self._tile_coord[0]
                self._img[row:row + rs[1], col:col + rs[0], :] = img
示例#41
0
	def on_draw(self, event):
		# Clear screen
		gloo.clear('black')
		
		frame = black

		if not self.get_lerp('MUTE'):
			# Read frame depending on controller status
			source = self.sources[self.get_lerp('SOURCE')]
			frame = source.read(self.get_lerp('RATE'))
			frame = np.flipud(frame) # Why does this need to be flipped?

		# Zoom
		z = self.get_lerp('ZOOM') # Temp. variable to use same random value for both axes
		x = self.get_lerp('X')
		y = self.get_lerp('Y')

		ox = (W_HD - W) * z
		oy = (H_HD - H) * z

		a = int(oy + (oy * y))
		b = int((H_HD-oy) + (oy * y))
		c = int(ox + (ox * x))
		d = int((W_HD-ox) + (ox * x))

		frame = frame[a:b,c:d,:]
		frame = cv2.resize(frame, (W, H))
		
		self.im_cap_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
		frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
		frame_rgbgray = cv2.cvtColor(self.im_cap_gray, cv2.COLOR_GRAY2RGB)
		
		# Difference image
		if self.get_lerp('DIFF'):
			# Monochrome
			if not self.get_lerp('SAT'):
				frame_diff = cv2.absdiff(self.im_last, frame_rgbgray)
				self.im_last = frame_rgbgray
				self.im_cap = frame_diff
			else:
				frame_diff = cv2.absdiff(self.im_last, frame_rgb)
				self.im_last = frame_rgb
				self.im_cap = frame_diff
		else:
			# Monochrome
			if not self.get_lerp('SAT'):
				self.im_cap = frame_rgbgray
			else:
				self.im_cap = frame_rgb

		# Generate when button is pressed and LUT, zoom, or pan is changed
		# BUG: This can NOT be done by callback functions in the MIDI handler, as the 
		# callbacks will be queued by mido and then successively released instead of
		# going bad
		
		self.new_C1 = self.get_lerp('LUT_C1')
		self.new_C2 = self.get_lerp('LUT_C2')
		self.new_zoom = self.get_lerp('ZOOM')
		self.new_x = self.get_lerp('X')
		self.new_y = self.get_lerp('Y')
		
		if self.new_C1 != self.last_C1: 
			self.regenerate_lut()
			self.last_C1 = self.new_C1
			if self.get_lerp('GENERATE'):
				self.q_map.put(self.im_lut_for_map)

		if self.new_C2 != self.last_C2: 
			self.regenerate_lut()
			self.last_C2 = self.new_C2
			if self.get_lerp('GENERATE'):
				self.q_map.put(self.im_lut_for_map)

		if self.new_zoom != self.last_zoom: 
			self.regenerate_lut()
			self.last_zoom = self.new_zoom
			if self.get_lerp('GENERATE'):
				self.q_map.put(self.im_lut_for_map)

		if self.new_x != self.last_x: 
			self.regenerate_lut()
			self.last_x = self.new_x
			if self.get_lerp('GENERATE'):
				self.q_map.put(self.im_lut_for_map)

		if self.new_y != self.last_y: 
			self.regenerate_lut()
			self.last_y = self.new_y
			if self.get_lerp('GENERATE'):
				self.q_map.put(self.im_lut_for_map)

		# Put map comparison map parts together
		self.im_map[:,:W2,:] = self.im_map_right
		self.im_map[:,W2:,:] = np.flipud(np.fliplr(self.im_cap[:,LEFT_WH2_IN_WH:RIGHT_WH2_IN_WH])) # Why does this need to be flipped?
			
		# Write options
		for k,o in self.opt.items():
			if o[4]: self.program[o[4]] = self.get_lerp(k)

		# Write textures
		self.program['u_time'] = (time.time() - self.start_time)
		self.program['t_video'][...] = self.im_cap
		self.program['t_map'][...] = self.im_map
		
		# Draw
		self.program.draw('triangle_strip')

		# Feedback
		# BUG: Needs to be "treated" once by openCV (-> CPU?) to not produce weird memory leaks
		# BUG: Weird scan lines in full screen mode when screen resolution > canvas resolution
		# BUG: "Flows" to bottom when activated before full screened/moved to second screen
		# WORKAROUND: Start feedback once before moving window, move/full screen while feedback runs
		screenshot = cv2.cvtColor(_screenshot(), cv2.COLOR_RGBA2RGB)
		self.program['t_feedback'][...] = np.fliplr(screenshot)

		# Communicate with control window
		control[0] = self.im_cap
		control[1] = self.im_lut
		control[2] = np.flipud(screenshot) # Why does this need to be flipped?
		control[3] = np.flipud(np.fliplr(self.im_map)) # Why does this need to be flipped?

		self.time = (time.time() * self.get_lerp('SPEED')) - self.start_time
示例#42
0
 def animation(self, t):
     self.update()
     return _screenshot((0,0,self.canvas.size[0],self.canvas.size[1]))[:,:,:3]
示例#43
0
def make_frame(t):
    surface.set_data(z=Z(t))  # Update the mathematical surface
    canvas.on_draw(None)  # Update the image on Vispy's canvas
    return _screenshot((0, 0, size_pixels[0], size_pixels[1]))
示例#44
0
 def animation(self, t):
     """ Added for animation with MoviePy """
     self.program['u_clock'] = 2*t
     gloo.clear('black')
     self.program.draw('points')
     return  _screenshot((0, 0, self.size[0], self.size[1]))[:,:,:3]