Ejemplo n.º 1
0
def run_example(example, size, title):
    if title is None:
        title = '%s - %s - %s' % (example.__name__, 'ModernGL', 'GLWindow')

    if size == 'fullscreen':
        wnd = GLWindow.create_window(fullscreen=True, title=title)

    else:
        wnd = GLWindow.create_window(size, title=title)

    app = example(WindowData(wnd))

    while wnd.update():
        app.render()
Ejemplo n.º 2
0
def main():
    '''
        Sample program to test GLWindow.
    '''

    print('GLWindow:', GLWindow.__version__)
    print('Python:', sys.version)
    print('Platform:', sys.platform)

    wnd = GLWindow.create_window((480, 480), title='GLWindow Sample')
    wnd.vsync = False

    ctx = ModernGL.create_context()

    prog = ctx.program([
        ctx.vertex_shader('''
            #version 330
            in vec2 vert;
            in vec4 vert_color;
            out vec4 frag_color;
            uniform vec2 scale;
            uniform float rotation;
            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);
            }
        '''),
        ctx.fragment_shader('''
            #version 330
            in vec4 frag_color;
            out vec4 color;
            void main() {
                color = vec4(frag_color);
            }
        '''),
    ])

    scale = prog.uniforms['scale']
    rotation = prog.uniforms['rotation']

    vbo = ctx.buffer(struct.pack(
        '18f',
        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,
    ))

    vao = ctx.simple_vertex_array(prog, vbo, ['vert', 'vert_color'])

    while wnd.update():
        wnd.clear(0.95, 0.95, 0.95)

        width, height = wnd.size
        scale.value = (height / width * 0.75, 0.75)
        ctx.viewport = wnd.viewport
        ctx.enable(ModernGL.BLEND)
        rotation.value = wnd.time
        vao.render(instances=10)
Ejemplo n.º 3
0
import struct

import GLWindow
import ModernGL

from PIL import Image, ImageDraw, ImageFont

# Window & Context

wnd = GLWindow.create_window()
ctx = ModernGL.create_context()

# Using Pillow to create a Texture

font = ImageFont.truetype('../data/OpenSans-Regular.ttf', 120)

msg = 'Hello World!'
msg_size = font.getsize(msg)

im = Image.new('RGBA', msg_size, (0, 0, 0, 0))

draw = ImageDraw.Draw(im)
draw.text((1, 0), msg, font=font, fill=(0, 0, 0, 255))
del draw

tex = ctx.texture(im.size, 4, im.tobytes())
tex.use()

# Shaders & Program

prog = ctx.program([
import struct

import GLWindow
import ModernGL
from pyrr import Matrix44

wnd = GLWindow.create_window()
ctx = ModernGL.create_context()

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

        uniform mat4 Mvp;

        in vec3 in_vert;
        in vec3 in_color;

        out vec3 v_color;

        void main() {
            v_color = in_color;
            gl_Position = Mvp * vec4(in_vert, 1.0);
        }
    '''),
    ctx.fragment_shader('''
        #version 330

        in vec3 v_color;
        out vec4 f_color;
Ejemplo n.º 5
0
import struct
import numpy

import GLWindow
import ModernGL

# This example is not working with NPOT Textures

pixels = numpy.round(numpy.random.rand(512, 512)).astype('float32')
grid = numpy.dstack(numpy.mgrid[0:512, 0:512][::-1] / 512).astype('float32')

wnd = GLWindow.create_window(512, 512)
ctx = ModernGL.create_context()

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

        in vec2 vert;
        out vec2 text;

        void main() {
            text = vert;
            gl_Position = vec4((vert * 2.0 - 1.0) * 1, 0.0, 1.0);
        }
    '''),
    ctx.fragment_shader('''
        #version 330

        uniform sampler2D Texture;
import struct
import numpy

import GLWindow
import ModernGL

# This example is not working with NPOT Textures

width, height = 640, 460

pixels = numpy.round(numpy.random.rand(width, height)).astype('float32')
grid = numpy.dstack(numpy.mgrid[0:height, 0:width][::-1]).astype('int32')

wnd = GLWindow.create_window(width, height)
ctx = ModernGL.create_context()

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

        in vec2 vert;
        out vec2 text;

        void main() {
            text = vert;
            gl_Position = vec4((vert * 2.0 - 1.0) * 1, 0.0, 1.0);
        }
    '''),
    ctx.fragment_shader('''
        #version 330