How to use the moderngl.pack function in moderngl

To help you get started, we’ve selected a few moderngl examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github moderngl / moderngl / tests / test_pack_half_floats.py View on Github external
def fmt(x):
    x = moderngl.pack([x], 'f2')
    t = ('0' * 16 + bin(int.from_bytes(x, 'little'))[2:])[-16:]
    return t[0 : 1] + ' ' + t[1 : 6] + ' ' + t[6 : 16]
github moderngl / moderngl / examples / texture_1d_array.py View on Github external
255, 127, 0,
            255, 255, 0,
            0, 255, 0,
            0, 0, 255,

            0, 0, 0,
            40, 40, 40,
            80, 80, 80,
            120, 120, 120,
            160, 160, 160,
        ], 'u1')

        self.texture = self.ctx.texture(5, 3, pixels, array=2)
        self.sampler = self.ctx.sampler(self.texture)

        vertex_data = moderngl.pack([
            -0.4, 0.4, 0.0, 0.0,
            -0.7, -0.4, 1.0, 0.0,
            -0.1, -0.4, 1.0, 0.0,
            0.4, 0.4, 0.0, 1.0,
            0.1, -0.4, 1.0, 1.0,
            0.7, -0.4, 1.0, 1.0,
        ])

        self.vbo = self.ctx.buffer(vertex_data)
        self.vao = self.ctx.vertex_array(self.prog, [
            self.vbo.bind('in_vert', 'in_text'),
        ])

        self.vao.scope = self.ctx.scope(samplers=[self.sampler.assign(0)])
github moderngl / moderngl / examples / texture_1d_array.py View on Github external
''',
            fragment_shader='''
                #version 330

                uniform sampler1DArray Texture;

                in vec2 v_text;
                out vec4 f_color;

                void main() {
                    f_color = texture(Texture, v_text);
                }
            ''',
        )

        pixels = moderngl.pack([
            255, 0, 0,
            255, 127, 0,
            255, 255, 0,
            0, 255, 0,
            0, 0, 255,

            0, 0, 0,
            40, 40, 40,
            80, 80, 80,
            120, 120, 120,
            160, 160, 160,
        ], 'u1')

        self.texture = self.ctx.texture(5, 3, pixels, array=2)
        self.sampler = self.ctx.sampler(self.texture)
github moderngl / moderngl / examples / headless / headless_hello_world.py View on Github external
void main() {
            gl_Position = vec4(in_vert, 0.0, 1.0);
        }
    ''',
    fragment_shader='''
        #version 330

        out vec4 f_color;

        void main() {
            f_color = vec4(0.3, 0.5, 1.0, 1.0);
        }
    ''',
)

vertex_data = moderngl.pack([
    0.0, 0.8,
    -0.6, -0.8,
    0.6, -0.8,
])

vbo = ctx.buffer(vertex_data)
vao = ctx.vertex_array(prog, [
    vbo.bind('in_vert'),
])

vao.scope = ctx.scope(framebuffer=fbo)

fbo.clear((1.0, 1.0, 1.0))
vao.render()

img = Image.frombytes('RGB', size, fbo.read(), 'raw', 'RGB', 0, -1)
github moderngl / moderngl / examples / alpha_blending_reverse_subtract.py View on Github external
one = np.ones(50)
        zero = np.zeros(50)
        it = np.linspace(-np.pi, np.pi, 50)

        vertex_data = moderngl.pack(np.concatenate([
            [np.cos(it) * 0.3 - 0.2, np.sin(it) * 0.5 - 0.3, one, zero, zero],
            [np.cos(it) * 0.3 + 0.2, np.sin(it) * 0.5 - 0.3, zero, one, zero],
            [np.cos(it) * 0.3 + 0.0, np.sin(it) * 0.5 + 0.3, zero, zero, one],
        ], 1).T.flatten())

        self.vbo = self.ctx.buffer(vertex_data)
        self.vao = self.ctx.vertex_array(self.prog, [
            self.vbo.bind('in_vert', 'in_color'),
        ])
        self.vao.indirect_buffer = self.ctx.buffer(moderngl.pack([
            50, 1, 0, 0,
            50, 1, 50, 0,
            50, 1, 100, 0,
        ], 'i'))

        self.vao.scope = self.ctx.scope(self.ctx.BLEND)
        self.vao.scope.blending = self.ctx.blending(
            blend_functions=[
                (self.ctx.SRC_ALPHA, self.ctx.ONE_MINUS_SRC_ALPHA),
            ],
            blend_equations=[
                self.ctx.FUNC_REVERSE_SUBTRACT,
            ],
        )
        self.vao.mode = self.ctx.TRIANGLE_FAN
        self.vao.instances = 3
github moderngl / moderngl / examples / texture_1d.py View on Github external
''',
            fragment_shader='''
                #version 330

                uniform sampler1D Texture;

                in float v_text;
                out vec4 f_color;

                void main() {
                    f_color = texture(Texture, v_text);
                }
            ''',
        )

        pixels = moderngl.pack([
            255, 0, 0,
            255, 127, 0,
            255, 255, 0,
            0, 255, 0,
            0, 0, 255,
        ], 'u1')

        self.texture = self.ctx.texture(5, 3, pixels)
        self.sampler = self.ctx.sampler(self.texture)

        vertex_data = moderngl.pack([
            0.0, 0.4, 0.0,
            -0.3, -0.4, 1.0,
            0.3, -0.4, 1.0,
        ])
github moderngl / moderngl / examples / empty_vertex_array.py View on Github external
v_color = in_color;
                }
            ''',
            fragment_shader='''
                #version 330

                in vec3 v_color;
                out vec4 f_color;

                void main() {
                    f_color = vec4(v_color, 1.0);
                }
            ''',
        )

        vertex_data = moderngl.pack([
            0.0, 0.8, 1.0, 0.0, 0.0,
            -0.6, -0.8, 0.0, 1.0, 0.0,
            0.6, -0.8, 0.0, 0.0, 1.0,
        ])

        self.vbo = self.ctx.buffer(vertex_data)
        self.vao = self.ctx.vertex_array()

        self.vao.mode = self.ctx.TRIANGLES
        self.vao.program = self.prog
        self.vao.bind(self.vbo.bind('in_vert', 'in_color'))
        # self.vao.bind(self.vbo.bind(0, 1, layout='2f 3f'))
        # self.vao.program = self.prog
        self.vao.vertices = 3
        self.vao.instances = 1
github moderngl / moderngl / examples / custom / tkinter_example.py View on Github external
void main() {
            gl_Position = vec4(in_vert, 0.0, 1.0);
        }
    ''',
    fragment_shader='''
        #version 330

        out vec4 f_color;

        void main() {
            f_color = vec4(0.3, 0.5, 1.0, 1.0);
        }
    ''',
)

vertex_data = moderngl.pack([
    0.0, 0.8,
    -0.6, -0.8,
    0.6, -0.8,
])

vbo = ctx.buffer(vertex_data)
vao = ctx.vertex_array(prog, [
    vbo.bind('in_vert'),
])

vao.scope = ctx.scope(framebuffer=fbo)

fbo.clear((1.0, 1.0, 1.0))
vao.render()

class Window(Frame):
github moderngl / moderngl / examples / alpha_blending_reverse_subtract.py View on Github external
#version 400

                in vec3 v_color;
                out vec4 f_color;

                void main() {
                    f_color = vec4(v_color, 0.3);
                }
            ''',
        )

        one = np.ones(50)
        zero = np.zeros(50)
        it = np.linspace(-np.pi, np.pi, 50)

        vertex_data = moderngl.pack(np.concatenate([
            [np.cos(it) * 0.3 - 0.2, np.sin(it) * 0.5 - 0.3, one, zero, zero],
            [np.cos(it) * 0.3 + 0.2, np.sin(it) * 0.5 - 0.3, zero, one, zero],
            [np.cos(it) * 0.3 + 0.0, np.sin(it) * 0.5 + 0.3, zero, zero, one],
        ], 1).T.flatten())

        self.vbo = self.ctx.buffer(vertex_data)
        self.vao = self.ctx.vertex_array(self.prog, [
            self.vbo.bind('in_vert', 'in_color'),
        ])
        self.vao.indirect_buffer = self.ctx.buffer(moderngl.pack([
            50, 1, 0, 0,
            50, 1, 50, 0,
            50, 1, 100, 0,
        ], 'i'))

        self.vao.scope = self.ctx.scope(self.ctx.BLEND)