How to use the vispy.gloo.Texture2D function in vispy

To help you get started, we’ve selected a few vispy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github vispy / vispy / examples / demo / gloo / glsl_sandbox_cube.py View on Github external
def __init__(self, **kwargs):
        app.Canvas.__init__(self, size=(400, 400), **kwargs)

        self.program = gloo.Program(VERT_CODE, FRAG_CODE)

        # Set attributes
        self.program['a_position'] = gloo.VertexBuffer(positions)
        self.program['a_texcoord'] = gloo.VertexBuffer(texcoords)

        self.program['u_texture'] = gloo.Texture2D(load_crate())

        # Handle transformations
        self.init_transforms()

        self.apply_zoom()

        gloo.set_clear_color((1, 1, 1, 1))
        gloo.set_state(depth_test=True)

        self._timer = app.Timer('auto', connect=self.update_transforms)
        self._timer.start()

        self.show()
github vispy / vispy / vispy / scene / viewbox.py View on Github external
render_fragment = """
            uniform sampler2D u_texture;
            varying vec2 v_texcoord;
            void main()
            {
                vec4 v = texture2D(u_texture, v_texcoord);
                gl_FragColor = vec4(v.rgb, 1.0);
            }
        """

        # todo: don't do this on every draw
        if True:
            # Create program
            self._myprogram = gloo.Program(render_vertex, render_fragment)
            # Create texture
            self._tex = gloo.Texture2D(shape=(10, 10, 4), dtype=np.uint8)
            self._tex.interpolation = gl.GL_LINEAR
            self._myprogram['u_texture'] = self._tex
            # Create texcoords and vertices
            texcoord = np.array([[0, 0], [1, 0], [0, 1], [1, 1]],
                                dtype=np.float32)
            position = np.zeros((4, 3), np.float32)
            self._myprogram['a_texcoord'] = gloo.VertexBuffer(texcoord)
            self._myprogram['a_position'] = self._vert = \
                gloo.VertexBuffer(position)

        # Get fbo, ensure it exists
        fbo = getattr(self, '_fbo', None)
        if True:  # fbo is None:
            self._fbo = 4
            self._fbo = fbo = gloo.FrameBuffer(self._tex,
                                               depth=gloo.DepthBuffer((10,
github vispy / vispy / examples / basics / gloo / post_processing.py View on Github external
self.indices = IndexBuffer(indices)

        # Build program
        # --------------------------------------
        view = translate((0, 0, -7))
        self.phi, self.theta = 60, 20
        model = rotate(self.theta, (0, 0, 1)).dot(rotate(self.phi, (0, 1, 0)))

        self.cube = Program(cube_vertex, cube_fragment)
        self.cube.bind(vertices)
        self.cube["texture"] = checkerboard()
        self.cube["texture"].interpolation = 'linear'
        self.cube['model'] = model
        self.cube['view'] = view

        color = Texture2D((512, 512, 3), interpolation='linear')
        self.framebuffer = FrameBuffer(color, RenderBuffer((512, 512)))

        self.quad = Program(quad_vertex, quad_fragment, count=4)
        self.quad['texcoord'] = [(0, 0), (0, 1), (1, 0), (1, 1)]
        self.quad['position'] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)]
        self.quad['texture'] = color

        # OpenGL and Timer initalization
        # --------------------------------------
        set_state(clear_color=(.3, .3, .35, 1), depth_test=True)
        self.timer = app.Timer('auto', connect=self.on_timer, start=True)
        self._set_projection(self.physical_size)

        self.show()
github sjdv1982 / seamless / OLD / examples / fireworks / fireworks-ORIGINAL.py View on Github external
def __init__(self):
        app.Canvas.__init__(self, keys='interactive', size=(800, 600))

        # Create program
        self._program = gloo.Program(VERT_SHADER, FRAG_SHADER)
        self._program.bind(gloo.VertexBuffer(data))
        self._program['s_texture'] = gloo.Texture2D(im1)

        # Create first explosion
        self._new_explosion()

        # Enable blending
        gloo.set_state(blend=True, clear_color='black',
                       blend_func=('src_alpha', 'one'))

        gloo.set_viewport(0, 0, self.physical_size[0], self.physical_size[1])

        self._timer = app.Timer('auto', connect=self.update, start=True)

        self.show()
github vispy / vispy / vispy / visuals / line.py View on Github external
self._opts.update(kwds)
        typ = [('pos', np.float32, self._opts['pos'].shape[-1])]
        if isinstance(self._opts['color'], np.ndarray):
            typ.append(('color', np.float32, self._opts['color'].shape[-1]))
        self._data = np.empty(self._opts['pos'].shape[:-1], typ)
        self._data['pos'] = self._opts['pos']
        if isinstance(self._opts['color'], np.ndarray):
            self._data['color'] = self._opts['color']
        
        self.vbo = VertexBuffer(data=self._data)
        #tex = np.zeros((len(self._data), 1, 3), dtype=np.float32)
        #tex[...,:2] = self._data['pos'][:,np.newaxis]
        
        # recast float to RGBA bytes
        d = self._data['pos'].astype(np.float32).view(np.ubyte).reshape(len(self._data), 2, 4)
        self.ptex = Texture2D(d)
        self.ptex.set_filter(gl.GL_NEAREST, gl.GL_NEAREST)
        
        self.indexes = VertexBuffer(data=np.arange(len(self._data)*2, dtype=np.float32))
github vispy / vispy / examples / basics / gloo / hello_fbo.py View on Github external
def __init__(self):
        app.Canvas.__init__(self, keys='interactive', size=(560, 420))

        # Create texture to render to
        shape = self.physical_size[1], self.physical_size[0]
        self._rendertex = gloo.Texture2D((shape + (3,)))

        # Create FBO, attach the color buffer and depth buffer
        self._fbo = gloo.FrameBuffer(self._rendertex, gloo.RenderBuffer(shape))

        # Create program to render a shape
        self._program1 = gloo.Program(VERT_SHADER1, FRAG_SHADER1)
        self._program1['u_color'] = 0.9, 1.0, 0.4, 1
        self._program1['a_position'] = gloo.VertexBuffer(vPosition)

        # Create program to render FBO result
        self._program2 = gloo.Program(VERT_SHADER2, FRAG_SHADER2)
        self._program2['a_position'] = gloo.VertexBuffer(vPosition)
        self._program2['a_texcoord'] = gloo.VertexBuffer(vTexcoord)
        self._program2['u_texture1'] = self._rendertex

        self.show()
github napari / napari / napari / _vispy / visuals / image.py View on Github external
def __init__(self, data=None, method='auto', grid=(1, 1),
                 cmap='viridis', clim='auto',
                 interpolation='nearest', **kwargs):
        self._data = None

        # load 'float packed rgba8' interpolation kernel
        # to load float interpolation kernel use
        # `load_spatial_filters(packed=False)`
        kernel, self._interpolation_names = load_spatial_filters()

        self._kerneltex = Texture2D(kernel, interpolation='nearest')
        # The unpacking can be debugged by changing "spatial-filters.frag"
        # to have the "unpack" function just return the .r component. That
        # combined with using the below as the _kerneltex allows debugging
        # of the pipeline
        # self._kerneltex = Texture2D(kernel, interpolation='linear',
        #                             internalformat='r32f')

        # create interpolation shader functions for available
        # interpolations
        fun = [Function(_interpolation_template % n)
               for n in self._interpolation_names]
        self._interpolation_names = [n.lower()
                                     for n in self._interpolation_names]

        self._interpolation_fun = dict(zip(self._interpolation_names, fun))
        self._interpolation_names.sort()
github vispy / vispy / examples / spinning-cube2.py View on Github external
def __init__(self, **kwargs):
        app.Canvas.__init__(self, **kwargs)
        self.geometry = 0, 0, 400, 400
        
        self.program = gloo.Program(VERT_CODE, FRAG_CODE)
        
        # Set attributes
        self.program['a_position'] = gloo.VertexBuffer(positions)
        self.program['a_texcoord'] = gloo.VertexBuffer(texcoords)
        
        self.program['u_texture'] = gloo.Texture2D(dataio.crate())
        
        # Handle transformations
        self.init_transforms()
        
        self.timer = app.Timer(1.0/60)
        self.timer.connect(self.update_transforms)
        self.timer.start()