File: tesselation.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 (95 lines) | stat: -rw-r--r-- 2,738 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python3
'''Simple example of using tessellation to render a cubic Bézier curve'''

import numpy as np

import moderngl
from _example import Example


class Tessellation(Example):
    title = "Tessellation"
    gl_version = (4, 0)

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.prog = self.ctx.program(
            vertex_shader='''
            #version 400 core

            in vec2 in_pos;

            void main() { gl_Position = vec4(in_pos, 0.0, 1.0); }
            ''',
            tess_control_shader='''
            #version 400 core

            layout(vertices = 4) out;

            void main() {
            // set tessellation levels, TODO compute dynamically
            gl_TessLevelOuter[0] = 1;
            gl_TessLevelOuter[1] = 32;

            // pass through vertex positions
            gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;
            }
            ''',
            tess_evaluation_shader='''
            #version 400 core

            layout(isolines, fractional_even_spacing, ccw) in;

            // compute a point on a bezier curve with the points p0, p1, p2, p3
            // the parameter u is in [0, 1] and determines the position on the curve
            vec3 bezier(float u, vec3 p0, vec3 p1, vec3 p2, vec3 p3) {
            float B0 = (1.0 - u) * (1.0 - u) * (1.0 - u);
            float B1 = 3.0 * (1.0 - u) * (1.0 - u) * u;
            float B2 = 3.0 * (1.0 - u) * u * u;
            float B3 = u * u * u;

            return B0 * p0 + B1 * p1 + B2 * p2 + B3 * p3;
            }

            void main() {
            float u = gl_TessCoord.x;

            vec3 p0 = vec3(gl_in[0].gl_Position);
            vec3 p1 = vec3(gl_in[1].gl_Position);
            vec3 p2 = vec3(gl_in[2].gl_Position);
            vec3 p3 = vec3(gl_in[3].gl_Position);

            gl_Position = vec4(bezier(u, p0, p1, p2, p3), 1.0);
            }
            ''',
            fragment_shader='''
            #version 400 core

            out vec4 frag_color;

            void main() { frag_color = vec4(1.0); }
            '''
        )

        # four vertices define a cubic Bézier curve; has to match the shaders
        self.ctx.patch_vertices = 4

        self.ctx.line_width = 5.0
        vertices = np.array([
            [-1.0, 0.0],
            [-0.5, 1.0],
            [0.5, -1.0],
            [1.0, 0.0],
        ])

        vbo = self.ctx.buffer(vertices.astype('f4'))
        self.vao = self.ctx.simple_vertex_array(self.prog, vbo, 'in_pos')

    def render(self, time, frame_time):
        self.ctx.clear(0.2, 0.4, 0.7)
        self.vao.render(mode=moderngl.PATCHES)


if __name__ == '__main__':
    Tessellation.run()