
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/gloo/textured_cube.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_cube.py>`
        to download the full example code.

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

.. _sphx_glr_gallery_gloo_textured_cube.py:


Show a rotating textured cube
=============================

.. GENERATED FROM PYTHON SOURCE LINES 13-112




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

 .. code-block:: none

    /build/reproducible-path/python-vispy-0.16.1/examples/gloo/textured_cube.py:53: 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, VertexBuffer, IndexBuffer
    from vispy.util.transforms import perspective, translate, rotate
    from vispy.geometry import create_cube


    vertex = """
    uniform mat4 model;
    uniform mat4 view;
    uniform mat4 projection;
    uniform sampler2D texture;

    attribute vec3 position;
    attribute vec2 texcoord;
    attribute vec3 normal;
    attribute vec4 color;

    varying vec2 v_texcoord;
    void main()
    {
        gl_Position = projection * view * model * vec4(position,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 cube',
                                keys='interactive')
            self.timer = app.Timer('auto', self.on_timer)

            # Build cube data
            V, I, _ = create_cube()
            vertices = VertexBuffer(V)
            self.indices = IndexBuffer(I)

            # Build program
            self.program = Program(vertex, fragment)
            self.program.bind(vertices)

            # Build view, model, projection & normal
            view = translate((0, 0, -5))
            model = np.eye(4, dtype=np.float32)
            self.program['model'] = model
            self.program['view'] = view
            self.program['texture'] = checkerboard()

            self.activate_zoom()

            self.phi, self.theta = 0, 0

            # OpenGL initalization
            gloo.set_state(clear_color=(0.30, 0.30, 0.35, 1.00), depth_test=True)
            self.timer.start()

            self.show()

        def on_draw(self, event):
            gloo.clear(color=True, depth=True)
            self.program.draw('triangles', self.indices)

        def on_resize(self, event):
            self.activate_zoom()

        def activate_zoom(self):
            gloo.set_viewport(0, 0, *self.physical_size)
            projection = perspective(45.0, self.size[0] / float(self.size[1]),
                                     2.0, 10.0)
            self.program['projection'] = projection

        def on_timer(self, event):
            self.theta += .5
            self.phi += .5
            self.program['model'] = np.dot(rotate(self.theta, (0, 0, 1)),
                                           rotate(self.phi, (0, 1, 0)))
            self.update()

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


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

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


.. _sphx_glr_download_gallery_gloo_textured_cube.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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