{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%gui qt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Use a Geometry Shader\n\nSimple geometry shader: Takes one point as input emits one triangle as output.\n\nNOTE: This example is currently not processed in CI.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\nfrom vispy import gloo\nfrom vispy import app\n\n# geometry shaders require full OpenGL namespace provided by PyOpenGL\ngloo.gl.use_gl('gl+')\n\n\nposition = np.random.normal(loc=0, scale=0.3, size=(1000, 2)).astype('float32')\n\n\nVERT_SHADER = \"\"\"\n#version 330\n\nin vec2 a_position;\n\nvoid main (void) {\n gl_Position = vec4(a_position, 0, 1);\n gl_PointSize = 3.0;\n}\n\"\"\"\n\n\nGEOM_SHADER = \"\"\"\n#version 330\n\nlayout (points) in;\nlayout (triangle_strip, max_vertices=3) out;\n\nvoid main(void) {\n vec4 p = gl_in[0].gl_Position;\n \n gl_Position = p;\n EmitVertex();\n gl_Position = p + vec4(0.06, 0.03, 0, 0);\n EmitVertex();\n gl_Position = p + vec4(0.03, 0.06, 0, 0);\n EmitVertex();\n EndPrimitive();\n}\n\"\"\"\n\n\nFRAG_SHADER = \"\"\"\n#version 330\n\nout vec4 frag_color;\n\nvoid main()\n{\n frag_color = vec4(0,0,0,0.5);\n}\n\"\"\"\n\n\nclass Canvas(app.Canvas):\n def __init__(self):\n app.Canvas.__init__(self, keys='interactive', size=(400, 400))\n self.program = gloo.Program()\n self.program.set_shaders(vert=VERT_SHADER, geom=GEOM_SHADER,\n frag=FRAG_SHADER)\n self.program['a_position'] = gloo.VertexBuffer(position)\n gloo.set_viewport(0, 0, self.physical_size[0], self.physical_size[1])\n self.context.set_clear_color('white')\n self.context.set_state('translucent', cull_face=False,\n depth_test=False)\n self.show()\n\n def on_resize(self, event):\n gloo.set_viewport(0, 0, event.physical_size[0], event.physical_size[1])\n\n def on_draw(self, event):\n self.context.clear()\n self.program.draw('points')\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.20" } }, "nbformat": 4, "nbformat_minor": 0 }