{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%gui qt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Show a rotating textured cube\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, VertexBuffer, IndexBuffer\nfrom vispy.util.transforms import perspective, translate, rotate\nfrom vispy.geometry import create_cube\n\n\nvertex = \"\"\"\nuniform mat4 model;\nuniform mat4 view;\nuniform mat4 projection;\nuniform sampler2D texture;\n\nattribute vec3 position;\nattribute vec2 texcoord;\nattribute vec3 normal;\nattribute vec4 color;\n\nvarying vec2 v_texcoord;\nvoid main()\n{\n gl_Position = projection * view * model * vec4(position,1.0);\n v_texcoord = texcoord;\n}\n\"\"\"\n\nfragment = \"\"\"\nuniform sampler2D texture;\nvarying vec2 v_texcoord;\nvoid main()\n{\n gl_FragColor = texture2D(texture, v_texcoord);\n}\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 cube',\n keys='interactive')\n self.timer = app.Timer('auto', self.on_timer)\n\n # Build cube data\n V, I, _ = create_cube()\n vertices = VertexBuffer(V)\n self.indices = IndexBuffer(I)\n\n # Build program\n self.program = Program(vertex, fragment)\n self.program.bind(vertices)\n\n # Build view, model, projection & normal\n view = translate((0, 0, -5))\n model = np.eye(4, dtype=np.float32)\n self.program['model'] = model\n self.program['view'] = view\n self.program['texture'] = checkerboard()\n\n self.activate_zoom()\n\n self.phi, self.theta = 0, 0\n\n # OpenGL initalization\n gloo.set_state(clear_color=(0.30, 0.30, 0.35, 1.00), depth_test=True)\n self.timer.start()\n\n self.show()\n\n def on_draw(self, event):\n gloo.clear(color=True, depth=True)\n self.program.draw('triangles', self.indices)\n\n def on_resize(self, event):\n self.activate_zoom()\n\n def activate_zoom(self):\n gloo.set_viewport(0, 0, *self.physical_size)\n projection = perspective(45.0, self.size[0] / float(self.size[1]),\n 2.0, 10.0)\n self.program['projection'] = projection\n\n def on_timer(self, event):\n self.theta += .5\n self.phi += .5\n self.program['model'] = np.dot(rotate(self.theta, (0, 0, 1)),\n rotate(self.phi, (0, 1, 0)))\n self.update()\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.19" } }, "nbformat": 4, "nbformat_minor": 0 }