{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%gui qt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Animate a Shape\n\nExample demonstrating showing a quad using\na Texture2D and VertexBuffer and a timer to control the drawing.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import time\nimport numpy as np\n\nfrom vispy import gloo\nfrom vispy import app\n\n\n# Create a texture\nim1 = np.zeros((100, 100, 3), 'float32')\nim1[:50, :, 0] = 1.0\nim1[:, :50, 1] = 1.0\nim1[50:, 50:, 2] = 1.0\n\n# Create vertices and texture coords, combined in one array for high performance\nvertex_data = np.zeros(4, dtype=[('a_position', np.float32, 3),\n ('a_texcoord', np.float32, 2)])\nvertex_data['a_position'] = np.array([[-0.8, -0.8, 0.0], [+0.7, -0.7, 0.0],\n [-0.7, +0.7, 0.0], [+0.8, +0.8, 0.0, ]])\nvertex_data['a_texcoord'] = np.array([[0.0, 0.0], [0.0, 1.0],\n [1.0, 0.0], [1.0, 1.0]])\n\n# Create indices and an ElementBuffer for it\nindices = np.array([0, 1, 2, 1, 2, 3], np.uint16)\nindices_buffer = gloo.IndexBuffer(indices)\nclient_indices_buffer = gloo.IndexBuffer(indices)\n\n\nVERT_SHADER = \"\"\" // simple vertex shader\n\nattribute vec3 a_position;\nattribute vec2 a_texcoord;\nuniform float sizeFactor;\n\nvoid main (void) {\n // Pass tex coords\n gl_TexCoord[0] = vec4(a_texcoord.x, a_texcoord.y, 0.0, 0.0);\n // Calculate position\n gl_Position = sizeFactor*vec4(a_position.x, a_position.y, a_position.z,\n 1.0/sizeFactor);\n}\n\"\"\"\n\nFRAG_SHADER = \"\"\" // simple fragment shader\nuniform sampler2D texture1;\n\nvoid main()\n{\n gl_FragColor = texture2D(texture1, gl_TexCoord[0].st);\n}\n\n\"\"\"\n\n\nclass Canvas(app.Canvas):\n\n def __init__(self):\n app.Canvas.__init__(self, keys='interactive')\n\n # Create program\n self._program = gloo.Program(VERT_SHADER, FRAG_SHADER)\n\n # Create vertex buffer\n self._vbo = gloo.VertexBuffer(vertex_data)\n\n # Set uniforms, samplers, attributes\n # We create one VBO with all vertex data (array of structures)\n # and create two views from it for the attributes.\n self._program['texture1'] = gloo.Texture2D(im1)\n self._program.bind(self._vbo) # This does:\n # self._program['a_position'] = self._vbo['a_position']\n # self._program['a_texcoords'] = self._vbo['a_texcoords']\n\n gloo.set_clear_color('white')\n\n self._timer = app.Timer('auto', connect=self.update, start=True)\n\n self.show()\n\n def on_resize(self, event):\n width, height = event.physical_size\n gloo.set_viewport(0, 0, width, height)\n\n def on_draw(self, event):\n\n # Clear\n gloo.clear()\n\n # Draw\n self._program['sizeFactor'] = 0.5 + np.sin(time.time() * 3) * 0.2\n\n # Draw (pick one!)\n # self._program.draw('triangle_strip')\n self._program.draw('triangles', indices_buffer)\n # self._program.draw('triangles', client_indices_buffer) # Not\n # recommended\n\n\nif __name__ == '__main__':\n canvas = 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.19" } }, "nbformat": 4, "nbformat_minor": 0 }