{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%gui qt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Show a textured quad\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n\nfrom vispy import gloo, app\nfrom vispy.gloo import Program\n\nvertex = \"\"\"\n attribute vec2 position;\n attribute vec2 texcoord;\n varying vec2 v_texcoord;\n void main()\n {\n gl_Position = vec4(position, 0.0, 1.0);\n v_texcoord = texcoord;\n } \"\"\"\n\nfragment = \"\"\"\n uniform sampler2D texture;\n varying vec2 v_texcoord;\n void main()\n {\n gl_FragColor = texture2D(texture, v_texcoord);\n } \"\"\"\n\n\ndef checkerboard(grid_num=8, grid_size=32):\n row_even = grid_num // 2 * [0, 1]\n row_odd = grid_num // 2 * [1, 0]\n Z = np.row_stack(grid_num // 2 * (row_even, row_odd)).astype(np.uint8)\n return 255 * Z.repeat(grid_size, axis=0).repeat(grid_size, axis=1)\n\n\nclass Canvas(app.Canvas):\n def __init__(self):\n app.Canvas.__init__(self, size=(512, 512), title='Textured quad',\n keys='interactive')\n\n # Build program & data\n self.program = Program(vertex, fragment, count=4)\n self.program['position'] = [(-1, -1), (-1, +1),\n (+1, -1), (+1, +1)]\n self.program['texcoord'] = [(0, 0), (1, 0), (0, 1), (1, 1)]\n self.program['texture'] = checkerboard()\n\n gloo.set_viewport(0, 0, *self.physical_size)\n\n self.show()\n\n def on_draw(self, event):\n gloo.set_clear_color('white')\n gloo.clear(color=True)\n self.program.draw('triangle_strip')\n\n def on_resize(self, event):\n gloo.set_viewport(0, 0, *event.physical_size)\n\nif __name__ == '__main__':\n c = Canvas()\n app.run()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.20" } }, "nbformat": 4, "nbformat_minor": 0 }