File: texture_array.py

package info (click to toggle)
python-moderngl-window 3.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 69,096 kB
  • sloc: python: 12,076; makefile: 21
file content (48 lines) | stat: -rw-r--r-- 1,487 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from pathlib import Path

import glm
import moderngl
from base import CameraWindow

from moderngl_window import geometry


class TextureArrayExample(CameraWindow):
    """
    Cycles different texture layers in an array texture
    rendered on a cube.
    """

    title = "Texture Array"
    resource_dir = (Path(__file__).parent / "resources").resolve()

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.wnd.mouse_exclusivity = True
        self.num_layers = 10
        self.cube = geometry.cube(size=(2, 2, 2))
        self.texture = self.load_texture_array(
            "textures/array.png", layers=self.num_layers, mipmap=True, anisotrpy=8.0
        )
        self.prog = self.load_program("programs/cube_texture_array.glsl")
        self.prog["texture0"].value = 0
        self.prog["num_layers"].value = 10

    def on_render(self, time: float, frametime: float):
        self.ctx.enable_only(moderngl.CULL_FACE | moderngl.DEPTH_TEST)

        rotation = glm.mat4(glm.quat(glm.vec3(time, time, time)))
        translation = glm.translate(glm.vec3(0.0, 0.0, -3.5))
        modelview = translation * rotation

        self.prog["m_proj"].write(self.camera.projection.matrix)
        self.prog["m_model"].write(modelview)
        self.prog["m_camera"].write(self.camera.matrix)
        self.prog["time"].value = time

        self.texture.use(location=0)
        self.cube.render(self.prog)


if __name__ == "__main__":
    TextureArrayExample.run()