{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%gui qt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Use multiple textures\n\nWe create two textures. One that shows a red, green and blue band in\nthe horizontal direction and one that does the same in the vertical\ndirection. In the fragment shader the colors from both textures are\nadded.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n\nfrom vispy import gloo\nfrom vispy import app\n\n\n# Images to be displayed\nW, H = 30, 30\nim1 = np.zeros((W, H, 3), np.float32)\nim2 = np.zeros((W, H, 3), np.float32)\nim1[:10, :, 0] = 1.0\nim1[10:20, :, 1] = 1.0\nim1[20:, :, 2] = 1.0\nim2[:, :10, 0] = 1.0\nim2[:, 10:20, 1] = 1.0\nim1[:, 20:, 2] = 1.0\n\n# A simple texture quad\ndata = np.zeros(4, dtype=[('a_position', np.float32, 2),\n ('a_texcoord', np.float32, 2)])\ndata['a_position'] = np.array([[-1, -1], [+1, -1], [-1, +1], [+1, +1]])\ndata['a_texcoord'] = np.array([[1, 0], [1, 1.2], [0, 0], [0, 1.2]])\n\n\nVERT_SHADER = \"\"\"\nattribute vec2 a_position;\nattribute vec2 a_texcoord;\nvarying vec2 v_texcoord;\n\nvoid main (void)\n{\n v_texcoord = a_texcoord;\n gl_Position = vec4(a_position, 0.0, 1.0);\n}\n\"\"\"\n\nFRAG_SHADER = \"\"\"\nuniform sampler2D u_tex1;\nuniform sampler2D u_tex2;\nvarying vec2 v_texcoord;\n\nvoid main()\n{\n vec3 clr1 = texture2D(u_tex1, v_texcoord).rgb;\n vec3 clr2 = texture2D(u_tex2, v_texcoord).rgb;\n gl_FragColor.rgb = clr1 + clr2;\n gl_FragColor.a = 1.0;\n}\n\"\"\"\n\n\nclass Canvas(app.Canvas):\n\n def __init__(self):\n app.Canvas.__init__(self, size=(500, 500), keys='interactive')\n\n self.program = gloo.Program(VERT_SHADER, FRAG_SHADER)\n self.program['u_tex1'] = gloo.Texture2D(im1, interpolation='linear')\n self.program['u_tex2'] = gloo.Texture2D(im2, interpolation='linear')\n self.program.bind(gloo.VertexBuffer(data))\n\n gloo.set_clear_color('white')\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 gloo.clear(color=True, depth=True)\n self.program.draw('triangle_strip')\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 }