Skip to main content
`Ctrl`+`K`

Note

Go to the end to download the full example code.

# Show a textured quad

    /build/python-vispy-wTQcL3/python-vispy-0.16.1/examples/gloo/textured_quad.py:38: DeprecationWarning: `row_stack` alias is deprecated. Use `np.vstack` directly.
      Z = np.row_stack(grid_num // 2 * (row_even, row_odd)).astype(np.uint8)

    import numpy as np

    from vispy import gloo, app
    from vispy.gloo import Program

    vertex = """
        attribute vec2 position;
        attribute vec2 texcoord;
        varying vec2 v_texcoord;
        void main()
        {
            gl_Position = vec4(position, 0.0, 1.0);
            v_texcoord = texcoord;
        } """

    fragment = """
        uniform sampler2D texture;
        varying vec2 v_texcoord;
        void main()
        {
            gl_FragColor = texture2D(texture, v_texcoord);
        } """

    def checkerboard(grid_num=8, grid_size=32):
        row_even = grid_num // 2 * [0, 1]
        row_odd = grid_num // 2 * [1, 0]
        Z = np.row_stack(grid_num // 2 * (row_even, row_odd)).astype(np.uint8)
        return 255 * Z.repeat(grid_size, axis=0).repeat(grid_size, axis=1)

    class Canvas(app.Canvas):
        def __init__(self):
            app.Canvas.__init__(self, size=(512, 512), title='Textured quad',
                                keys='interactive')

            # Build program & data
            self.program = Program(vertex, fragment, count=4)
            self.program['position'] = [(-1, -1), (-1, +1),
                                        (+1, -1), (+1, +1)]
            self.program['texcoord'] = [(0, 0), (1, 0), (0, 1), (1, 1)]
            self.program['texture'] = checkerboard()

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

            self.show()

        def on_draw(self, event):
            gloo.set_clear_color('white')
            gloo.clear(color=True)
            self.program.draw('triangle_strip')

        def on_resize(self, event):
            gloo.set_viewport(0, 0, *event.physical_size)

    if __name__ == '__main__':
        c = Canvas()
        app.run()

Total running time of the script: (0 minutes 0.108 seconds)

Gallery generated by Sphinx-Gallery