
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/gloo/textured_quad.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_gallery_gloo_textured_quad.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_gloo_textured_quad.py:


Show a textured quad
====================

.. GENERATED FROM PYTHON SOURCE LINES 10-69




.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    /build/python-vispy-wTQcL3/python-vispy-0.16.1/examples/gloo/textured_quad.py:38: DeprecationWarning: `row_stack` alias is deprecated. Use `np.vstack` directly.
      Z = np.row_stack(grid_num // 2 * (row_even, row_odd)).astype(np.uint8)






|

.. code-block:: Python


    import numpy as np

    from vispy import gloo, app
    from vispy.gloo import Program

    vertex = """
        attribute vec2 position;
        attribute vec2 texcoord;
        varying vec2 v_texcoord;
        void main()
        {
            gl_Position = vec4(position, 0.0, 1.0);
            v_texcoord = texcoord;
        } """

    fragment = """
        uniform sampler2D texture;
        varying vec2 v_texcoord;
        void main()
        {
            gl_FragColor = texture2D(texture, v_texcoord);
        } """


    def checkerboard(grid_num=8, grid_size=32):
        row_even = grid_num // 2 * [0, 1]
        row_odd = grid_num // 2 * [1, 0]
        Z = np.row_stack(grid_num // 2 * (row_even, row_odd)).astype(np.uint8)
        return 255 * Z.repeat(grid_size, axis=0).repeat(grid_size, axis=1)


    class Canvas(app.Canvas):
        def __init__(self):
            app.Canvas.__init__(self, size=(512, 512), title='Textured quad',
                                keys='interactive')

            # Build program & data
            self.program = Program(vertex, fragment, count=4)
            self.program['position'] = [(-1, -1), (-1, +1),
                                        (+1, -1), (+1, +1)]
            self.program['texcoord'] = [(0, 0), (1, 0), (0, 1), (1, 1)]
            self.program['texture'] = checkerboard()

            gloo.set_viewport(0, 0, *self.physical_size)

            self.show()

        def on_draw(self, event):
            gloo.set_clear_color('white')
            gloo.clear(color=True)
            self.program.draw('triangle_strip')

        def on_resize(self, event):
            gloo.set_viewport(0, 0, *event.physical_size)

    if __name__ == '__main__':
        c = Canvas()
        app.run()


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 0.108 seconds)


.. _sphx_glr_download_gallery_gloo_textured_quad.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: textured_quad.ipynb <textured_quad.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: textured_quad.py <textured_quad.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: textured_quad.zip <textured_quad.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
