Python vispy.app.Canvas() Examples

The following are 11 code examples of vispy.app.Canvas(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module vispy.app , or try the search function .
Example #1
Source File: renderer.py    From Pix2Pose with MIT License 6 votes vote down vote up
def __init__(self, size, cam):

        app.Canvas.__init__(self, show=False, size=size)
        self.shape = (size[1], size[0])
        self.yz_flip = np.eye(4, dtype=np.float32)
        self.yz_flip[1, 1], self.yz_flip[2, 2] = -1, -1

        self.set_cam(cam)

        # Set up shader programs
        self.program_col = gloo.Program(_vertex_code_colored, _fragment_code_colored)
        self.program_tex = gloo.Program(_vertex_code_textured, _fragment_code_textured)

        # Texture where we render the color/depth and its FBO
        self.col_tex = gloo.Texture2D(shape=self.shape + (3,))
        self.fbo = gloo.FrameBuffer(self.col_tex, gloo.RenderBuffer(self.shape))
        self.fbo.activate()
        gloo.set_state(depth_test=True, blend=False, cull_face=True)
        gl.glEnable(gl.GL_LINE_SMOOTH)
        gloo.set_clear_color((0.0, 0.0, 0.0))
        gloo.set_viewport(0, 0, *self.size) 
Example #2
Source File: renderer_xyz.py    From Pix2Pose with MIT License 6 votes vote down vote up
def __init__(self, size, cam):

        app.Canvas.__init__(self, show=False, size=size)
        self.shape = (size[1], size[0])
        self.yz_flip = np.eye(4, dtype=np.float32)
        self.yz_flip[1, 1], self.yz_flip[2, 2] = -1, -1

        self.set_cam(cam)

        # Set up shader programs
        self.program_col = gloo.Program(_vertex_code_colored, _fragment_code_colored)
        self.program_tex = gloo.Program(_vertex_code_textured, _fragment_code_textured)

        # Texture where we render the color/depth and its FBO
        self.col_tex = gloo.Texture2D(shape=self.shape + (3,))
        self.fbo = gloo.FrameBuffer(self.col_tex, gloo.RenderBuffer(self.shape))
        self.fbo.activate()
        gloo.set_state(depth_test=True, blend=False, cull_face=True)
        gl.glEnable(gl.GL_LINE_SMOOTH)
        gloo.set_clear_color((0.0, 0.0, 0.0))
        gloo.set_viewport(0, 0, *self.size) 
Example #3
Source File: renderer.py    From ssd-6d with MIT License 6 votes vote down vote up
def __init__(self, size, cam):

        app.Canvas.__init__(self, show=False, size=size)
        self.shape = (size[1], size[0])
        self.yz_flip = np.eye(4, dtype=np.float32)
        self.yz_flip[1, 1], self.yz_flip[2, 2] = -1, -1

        self.set_cam(cam)

        # Set up shader programs
        self.program_col = gloo.Program(_vertex_code_colored, _fragment_code_colored)
        self.program_tex = gloo.Program(_vertex_code_textured, _fragment_code_textured)

        # Texture where we render the color/depth and its FBO
        self.col_tex = gloo.Texture2D(shape=self.shape + (3,))
        self.fbo = gloo.FrameBuffer(self.col_tex, gloo.RenderBuffer(self.shape))
        self.fbo.activate()
        gloo.set_state(depth_test=True, blend=False, cull_face=True)
        gl.glEnable(gl.GL_LINE_SMOOTH)
        gloo.set_clear_color((0.0, 0.0, 0.0))
        gloo.set_viewport(0, 0, *self.size) 
Example #4
Source File: vispy_base_layer.py    From napari with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def get_max_texture_sizes():
    """Get maximum texture sizes for 2D and 3D rendering.

    Returns
    -------
    MAX_TEXTURE_SIZE_2D : int or None
        Max texture size allowed by the vispy canvas during 2D rendering.
    MAX_TEXTURE_SIZE_3D : int or None
        Max texture size allowed by the vispy canvas during 2D rendering.
    """
    # A canvas must be created to access gl values
    c = Canvas(show=False)
    try:
        MAX_TEXTURE_SIZE_2D = gl.glGetParameter(gl.GL_MAX_TEXTURE_SIZE)
    finally:
        c.close()
    if MAX_TEXTURE_SIZE_2D == ():
        MAX_TEXTURE_SIZE_2D = None
    # vispy doesn't expose GL_MAX_3D_TEXTURE_SIZE so hard coding
    # MAX_TEXTURE_SIZE_3D = gl.glGetParameter(gl.GL_MAX_3D_TEXTURE_SIZE)
    # if MAX_TEXTURE_SIZE_3D == ():
    #    MAX_TEXTURE_SIZE_3D = None
    MAX_TEXTURE_SIZE_3D = 2048

    return MAX_TEXTURE_SIZE_2D, MAX_TEXTURE_SIZE_3D 
Example #5
Source File: waterfall.py    From OpenNFB with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self):
        app.Canvas.__init__(self,
                            keys='interactive')

        self.program = gloo.Program(VERT_SHADER, FRAG_SHADER)
        self.program['index'] = [(x,) for x in range(num_bins)] * num_lines
        self.program['line'] = [(x,) * num_bins for x in range(num_lines)]
        self.program['map_offset'] = 0

        self.program['num_bins'] = num_bins
        self.program['num_lines'] = num_lines

        heightmap = np.random.random(size=(num_lines, num_bins)).astype('float32')

        self.program['heightmap'] = gloo.Texture2D(data=heightmap, internalformat='r32f')
        #print (dir(self.program['heightmap']))

        self.program['colormap'] = Colormap(['r', 'g', 'b']).map(np.linspace(0, 1, 64)).astype('float32')

        gloo.set_viewport(0, 0, *self.physical_size)

        gloo.set_state(clear_color='black', blend=True,
                       blend_func=('src_alpha', 'one_minus_src_alpha'))

        self.show() 
Example #6
Source File: renderer.py    From patch_linemod with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self, vertices, faces, size, K, R, t, clip_near, clip_far,
                 bg_color=(0.0, 0.0, 0.0, 0.0), ambient_weight=0.1,
                 render_rgb=True, render_depth=True):
        """
        mode is from ['rgb', 'depth', 'rgb+depth']
        """
        app.Canvas.__init__(self, show=False, size=size)

        #gloo.gl.use_gl('gl2 debug')

        self.size = size
        self.shape = (self.size[1], self.size[0])
        self.bg_color = bg_color
        self.ambient_weight = ambient_weight
        self.render_rgb = render_rgb
        self.render_depth = render_depth

        self.rgb = np.array([])
        self.depth = np.array([])

        # Model matrix
        self.mat_model = np.eye(4, dtype=np.float32) # From object space to world space

        # View matrix (transforming also the coordinate system from OpenCV to
        # OpenGL camera space)
        self.mat_view = np.eye(4, dtype=np.float32) # From world space to eye space
        self.mat_view[:3, :3], self.mat_view[:3, 3] = R, t.squeeze()
        yz_flip = np.eye(4, dtype=np.float32)
        yz_flip[1, 1], yz_flip[2, 2] = -1, -1
        self.mat_view = yz_flip.dot(self.mat_view) # OpenCV to OpenGL camera system
        self.mat_view = self.mat_view.T # OpenGL expects column-wise matrix format

        # Projection matrix
        self.mat_proj = _compute_calib_proj(K, 0, 0, size[0], size[1], clip_near, clip_far)

        # Create buffers
        self.vertex_buffer = gloo.VertexBuffer(vertices)
        self.index_buffer = gloo.IndexBuffer(faces.flatten().astype(np.uint32))

        # We manually draw the hidden canvas
        self.update() 
Example #7
Source File: base.py    From p5 with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, setup_method, draw_method,
                 handlers=dict(), frame_rate=60):
        app.Canvas.__init__(
            self,
            title=builtins.title,
            size=(builtins.width, builtins.height),
            keys='interactive',
            resizable=True,
        )

        self.setup_method = setup_method
        self.draw_method = draw_method

        self.looping = None
        self.redraw = None
        self.setup_done = False
        self.timer = app.Timer(1.0 / frame_rate, connect=self.on_timer)

        self.handlers = dict()
        for handler_name in handler_names:
            self.handlers[handler_name] = handlers.get(handler_name, _dummy)

        self.handler_queue = []

        self._save_fname = 'screen'
        self._save_fname_num = 0
        self._save_flag = False

        p5.renderer.initialize_renderer()
        p5.renderer.clear() 
Example #8
Source File: waterfall.py    From OpenNFB with GNU General Public License v3.0 5 votes vote down vote up
def init(self):
        self.canvas = Canvas()
        self.widget = self.canvas.native

        self.gr_block.set_history(bins)
        self.win = np.blackman(bins) 
Example #9
Source File: vispy_test.py    From VNect-tensorflow with Apache License 2.0 5 votes vote down vote up
def __init__(self):
        app.Canvas.__init__(self, size=(512, 512),
                            title='Rotating cube (GL version)',
                            keys='interactive') 
Example #10
Source File: renderer.py    From eccv18-rgb_pose_refinement with MIT License 5 votes vote down vote up
def __init__(self, size, cam):

        app.Canvas.__init__(self, show=False, size=size)
        self.shape = (size[1], size[0])
        self.yz_flip = np.eye(4, dtype=np.float32)
        self.yz_flip[1, 1], self.yz_flip[2, 2] = -1, -1

        self.set_cam(cam)

        # Set up shader programs
        self.program_col = gloo.Program(_vertex_code_colored, _fragment_code_colored)
        self.program_bbox = gloo.Program(_vertex_code_colored, _fragment_code_bbox)
        self.program_tex = gloo.Program(_vertex_code_textured, _fragment_code_textured)
        self.program_bg = gloo.Program(_vertex_code_background, _fragment_code_background)

        # Texture where we render the color/depth and its FBO
        self.col_tex = gloo.Texture2D(shape=self.shape + (3,))
        self.fbo = gloo.FrameBuffer(self.col_tex, gloo.RenderBuffer(self.shape))
        self.fbo.activate()
        gloo.set_state(depth_test=True, blend=False, cull_face=True)
        gl.glEnable(gl.GL_LINE_SMOOTH)
        gloo.set_clear_color((0.0, 0.0, 0.0))
        gloo.set_viewport(0, 0, *self.size)

        # Set up background render quad in NDC
        quad = [[-1, -1], [1, -1], [1, 1], [-1, 1]]
        tex = [[0, 1], [1, 1], [1, 0], [0, 0]]
        vertices_type = [('a_position', np.float32, 2), ('a_texcoord', np.float32, 2)]
        collated = np.asarray(list(zip(quad, tex)), vertices_type)
        self.bg_vbuffer = gloo.VertexBuffer(collated)
        self.bg_ibuffer = gloo.IndexBuffer([0, 1, 2, 0, 2, 3]) 
Example #11
Source File: canvas.py    From harvesters_gui with Apache License 2.0 4 votes vote down vote up
def __init__(
            self, *,
            image_acquirer=None,
            width=640, height=480,
            display_rate=30.,
            background_color='gray',
            vsync=True
    ):
        """
        As far as we know, Vispy refreshes the canvas every 1/30 sec at the
        fastest no matter which faster number is specified. If we set any
        value which is greater than 30, then Vispy's callback is randomly
        called.
        """

        #
        app.Canvas.__init__(
            self, size=(width, height), vsync=vsync, autoswap=True
        )

        #
        self._ia = image_acquirer

        #
        self._background_color = background_color
        self._has_filled_texture = False
        self._width, self._height = width, height

        #
        self._is_dragging = False

        # If it's True, the canvas keeps image acquisition but do not
        # draw images on the canvas:
        self._pause_drawing = False

        #
        self._origin = [0, 0]

        #
        self._display_rate = display_rate
        self._timer = app.Timer(
            1. / self._display_rate, connect=self.update, start=True
        )

        #
        self._buffers = []