File: pygame_hello_world.py

package info (click to toggle)
python-moderngl 5.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,700 kB
  • sloc: python: 15,758; cpp: 14,665; makefile: 14
file content (59 lines) | stat: -rw-r--r-- 1,434 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
import pygame
import moderngl

pygame.init()
pygame.display.set_mode((400, 400), flags=pygame.OPENGL | pygame.DOUBLEBUF)

# ctx = moderngl.create_context()
ctx = moderngl.get_context()

prog = ctx.program(
    vertex_shader='''
        #version 330 core
        uniform float time;
        vec2 vertex[3] = vec2[](
            vec2(1.0, 0.0),
            vec2(-0.5, -0.86),
            vec2(-0.5, 0.86)
        );
        vec3 color[3] = vec3[](
            vec3(0.0, 0.0, 1.0),
            vec3(0.0, 1.0, 0.0),
            vec3(1.0, 0.0, 0.0)
        );
        out vec3 v_color;
        void main() {
            v_color = color[gl_VertexID];
            float r = float(gl_InstanceID - 4) * 0.15 * (0.8 + sin(time) * 0.2);
            mat2 rot = mat2(cos(r), sin(r), -sin(r), cos(r));
            gl_Position = vec4(rot * vertex[gl_VertexID] * 0.95, 0.0, 1.0);
        }
    ''',
    fragment_shader='''
        #version 330 core
        in vec3 v_color;
        out vec4 f_color;
        void main() {
            f_color = vec4(pow(v_color, vec3(1.0 / 2.2)), 1.0);
        }
    ''',
)

vao = ctx.vertex_array(prog, [])
vao.instances = 9
vao.vertices = 3

clock = pygame.Clock()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

    prog["time"].value += 1.0 / 60.0
    ctx.clear()
    vao.render()

    pygame.display.flip()
    clock.tick(60)