File: creating_separate_window.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 (79 lines) | stat: -rw-r--r-- 1,613 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
73
74
75
76
77
78
79
# Example of creating a window without
# enclosing code in modengl_window.WindowConfig class
# Description: This example shows how to create a window as a separate object.
# Note: Only one window can be used.

import moderngl as gl
import moderngl_window as glw
import struct


# Create window
window_cls = glw.get_local_window_cls('pyglet')
window = window_cls(
    size=(1024, 512), fullscreen=False, title='Separate window',
    resizable=False, vsync=True, gl_version=(3, 3))
ctx = window.ctx
glw.activate_context(window, ctx=ctx)
window.clear()
window.swap_buffers()

# Window prog
prog = ctx.program(
    vertex_shader='''
    #version 330

    in vec3 in_vert;
    in vec4 in_color;

    uniform mat4 prj;

    out vec4 o_color;

    void main() {
        gl_Position = prj*vec4(in_vert.xyz, 1.0);
        o_color = in_color;
    }
    ''',
    fragment_shader='''
    #version 330

    in vec4 o_color;

    out vec4 f_color;

    void main() {
        f_color = o_color;
    }
    ''')

# Preparing
content = [
    .3, .3, -.5, 1, 0, 0, 1,
    .3, -.3, -.5, 1, 0, 0, 1,
    -.3, .3, -.4, 1, 0, 0, 1,

    .3, .4, -.5, 0, 0, 1, 1,
    .3, -.2, -.5, 0, 0, 1, 1,
    -.3, .4, -.45, 0, 0, 1, 1
]
buf = ctx.buffer(struct.pack(f'{len(content)}f', *content))
vao = ctx.vertex_array(prog, buf, 'in_vert', 'in_color')

f = 1
n = 0.1
prj = (
    1, 0, 0, 0,
    0, 1, 0, 0,
    0, 0, -(f+n)/(f-n), -1,
    0, 0, -(2*f*n)/(f-n), 0
)
vao.program['prj'] = prj

# Render loop
ctx.enable(gl.DEPTH_TEST)
while not window.is_closing:
    window.fbo.use()
    window.fbo.clear()
    vao.render()
    window.swap_buffers()