File: moderngl_logo.py

package info (click to toggle)
python-moderngl-window 3.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 69,096 kB
  • sloc: python: 12,076; makefile: 21
file content (72 lines) | stat: -rw-r--r-- 1,887 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import moderngl
import numpy as np

import moderngl_window as mglw


class ModernglLogo(mglw.WindowConfig):
    title = "ModernGL Logo"

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.prog = self.ctx.program(
            vertex_shader="""
                #version 330

                in vec2 vert;
                in vec4 vert_color;

                uniform vec2 scale;
                uniform float rotation;

                out vec4 frag_color;

                void main() {
                    frag_color = vert_color;
                    float r = rotation * (0.5 + gl_InstanceID * 0.05);
                    mat2 rot = mat2(cos(r), sin(r), -sin(r), cos(r));
                    gl_Position = vec4((rot * vert) * scale, 0.0, 1.0);
                }
            """,
            fragment_shader="""
                #version 330

                in vec4 frag_color;
                out vec4 color;

                void main() {
                    color = vec4(frag_color);
                }
            """,
        )

        self.scale = self.prog["scale"]
        self.rotation = self.prog["rotation"]

        self.scale.value = (0.5, self.aspect_ratio * 0.5)

        # fmt: off
        vertices = np.array([
            1.0, 0.0,
            1.0, 0.0, 0.0, 0.5,

            -0.5, 0.86,
            0.0, 1.0, 0.0, 0.5,

            -0.5, -0.86,
            0.0, 0.0, 1.0, 0.5,
        ], dtype="f4")
        # fmt: on

        self.vbo = self.ctx.buffer(vertices.astype("f4").tobytes())
        self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, "vert", "vert_color")

    def on_render(self, time: float, frametime: float):
        self.ctx.clear(1.0, 1.0, 1.0)
        self.ctx.enable(moderngl.BLEND)
        self.rotation.value = time
        self.vao.render(instances=10)


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