# -*- coding: utf-8 -*- # ----------------------------------------------------------------------------- # Copyright (c) 2014, Nicolas P. Rougier. All rights reserved. # Distributed under the terms of the new BSD License. # ----------------------------------------------------------------------------- """ Show a textured quad ==================== """ 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()