Example #1
0
    def __init__(self, title, width, height):

        settings.WINDOW['class'] = 'moderngl_window.context.glfw.Window'
        settings.WINDOW['gl_version'] = (4, 6)
        settings.WINDOW['size'] = (width, height)
        settings.WINDOW['aspect_ratio'] = width / height
        settings.WINDOW['title'] = title
        settings.WINDOW['resizable'] = True
        settings.WINDOW['vsync'] = True

        path = os.path.abspath(__file__)
        dirPath = os.path.dirname(os.path.dirname(path))
        resources.register_dir(os.path.normpath(dirPath))

        self.wnd = moderngl_window.create_window_from_settings()
        self.ctx = self.wnd.ctx
        self.wnd.key_event_func = self.key_event
        self.wnd.mouse_press_event_func = self.mouse_press_event
        self.wnd.mouse_release_event_func = self.mouse_release_event
        self.wnd.position = (1640 - self.wnd.size[0]) // 2, (
            1140 - self.wnd.size[1]) // 2
        self.height = height
        self.width = width
        self.frames = 0
        self.oldTime = 0

        self.wnd.set_icon('resources/icon.png')
        self.camera = camera.Camera(self.wnd.keys)
Example #2
0
def main():
    global window
    # Configure to use pyglet window
    settings.WINDOW['class'] = 'moderngl_window.context.pyglet.Window'
    window = moderngl_window.create_window_from_settings()

    # Map callback functions
    window.resize_func = resize
    window.iconify_func = iconify
    window.key_event_func = key_event
    window.mouse_position_event_func = mouse_position_event
    window.mouse_drag_event_func = mouse_drag_event
    window.mouse_scroll_event_func = mouse_scroll_event
    window.mouse_press_event_func = mouse_press_event
    window.mouse_release_event_func = mouse_release_event
    window.unicode_char_entered_func = unicode_char_entered

    timer = Timer()
    timer.start()

    while not window.is_closing:
        window.use()
        window.clear()

        time, frame_time = timer.next_frame()

        window.ctx.clear(
            (math.sin(time) + 1.0) / 2,
            (math.sin(time + 2) + 1.0) / 2,
            (math.sin(time + 3) + 1.0) / 2,
        )

        window.swap_buffers()

    window.destroy()
Example #3
0
    def setUpClass(cls):
        """Create a headless window and activate the context"""
        settings.WINDOW['class'] = 'moderngl_window.context.headless.Window'
        settings.WINDOW['size'] = cls.window_size
        settings.WINDOW['aspect_ratio'] = cls.aspect_ratio
        settings.WINDOW['gl_version'] = cls.gl_version

        cls.window = mglw.create_window_from_settings()
        mglw.activate_context(window=cls.window)
Example #4
0
def main():
    # settings.WINDOW['class'] = 'moderngl_window.context.headless.Window'
    window = moderngl_window.create_window_from_settings()

    while not window.is_closing:
        window.use()
        window.clear()
        # Render stuff here
        window.swap_buffers()

    window.destroy()
    def __init__(self):
        # Configure to use pyglet window
        settings.WINDOW['class'] = 'moderngl_window.context.pyglet.Window'
        self.wnd = moderngl_window.create_window_from_settings()
        self.ctx = self.wnd.ctx

        # register event methods
        self.wnd.resize_func = self.resize
        self.wnd.iconify_func = self.iconify
        self.wnd.key_event_func = self.key_event
        self.wnd.mouse_position_event_func = self.mouse_position_event
        self.wnd.mouse_drag_event_func = self.mouse_drag_event
        self.wnd.mouse_scroll_event_func = self.mouse_scroll_event
        self.wnd.mouse_press_event_func = self.mouse_press_event
        self.wnd.mouse_release_event_func = self.mouse_release_event
        self.wnd.unicode_char_entered_func = self.unicode_char_entered
 def __init__(self, gl_version, size, fullscreen, resizable, title, cursor):
     config = {
         #"class": "moderngl_window.context.tk.Window",
         "gl_version": gl_version,
         "size": size,
         "aspect_ratio": size[1] / size[0],
         "fullscreen": fullscreen,
         "resizable": resizable,
         "title": title,
         "vsync": False,
         "cursor": cursor,
         "samples": 0
     }
     settings.WINDOW = dict(
         list(settings.WINDOW.items()) + list(config.items()))
     self.window = mglw.create_window_from_settings()
     self.ctx = self.window.ctx
Example #7
0
    def __init__(self, source, texture_directory, screenshot_directory, **kws):
        super().__init__(**kws)
        self.source = source
        self.targets = []

        if not hasattr(self, 'background'):
            self.background = vec3(0.1, 0.1, 0.1)

        settings.apply_from_dict(dict(SCREENSHOT_PATH=screenshot_directory))
        settings.WINDOW['class'] = 'moderngl_window.context.pyglet.Window'
        self.wnd = moderngl_window.create_window_from_settings()

        self.ctx = self.wnd.ctx
        self.ctx.enable(moderngl.DEPTH_TEST | moderngl.CULL_FACE | moderngl.BLEND)

        #gl2.glEnable(GL.GL_BLEND);
        #gl2.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);

        if not hasattr(self, 'programs'):
            self.programs = [MainShader(), EdgeShader()]
            #self.programs = [MainShader(), WireShader()]

        self.shaderkeys = {}
        for i, program in enumerate(self.programs):
            print(f'Building Shader {i+1}:{program}')
            program.build(self.ctx).init()
            key = getattr(self.wnd.keys, f'F{i + 1}')
            self.shaderkeys[key] = self.toggleshader(program)

        self.materials = LazyMaterials(self.ctx, texture_directory)

        self.camera = Camera(self.wnd.keys, **kws)

        self.wnd.mouse_drag_event_func = self.camera.mouse_drag_event
        self.wnd.mouse_scroll_event_func = self.camera.mouse_scroll_event
        self.wnd.mouse_press_event_func = self.camera.mouse_press_event
        self.wnd.key_event_func = self.key_event
Example #8
0
    def mode(self, window_class, msaa=1, vsync=True, strict=False, icon=None):
        debug_prefix = "[MMVShaderMGLWindowHandlers.mode]"

        logging.info(
            f"{debug_prefix} \"i\" Set window mode [window_class={window_class}] [msaa={msaa}] [vsync={vsync}] [strict={strict}] [icon={icon}]"
        )

        # Get function arguments
        self.headless = window_class == "headless"
        self.strict = strict
        self.vsync = vsync
        self.msaa = msaa

        # Headless we disable vsync because we're rendering only..?
        # And also force aspect ratio just in case (strict option)
        if self.headless:
            self.strict = True
            self.vsync = False

        # Assign the function arguments
        settings.WINDOW[
            "class"] = f"moderngl_window.context.{window_class}.Window"
        settings.WINDOW[
            "aspect_ratio"] = self.mmv_shader_mgl.width / self.mmv_shader_mgl.height
        settings.WINDOW["vsync"] = self.vsync
        settings.WINDOW["title"] = "MMVShaderMGL Real Time Window"
        settings.WINDOW["size"] = (self.mmv_shader_mgl.width,
                                   self.mmv_shader_mgl.height)

        # Create the window
        self.window = moderngl_window.create_window_from_settings()

        # Make sure we render strictly into the resolution we asked
        if strict:
            self.window.fbo.viewport = (0, 0, self.mmv_shader_mgl.width,
                                        self.mmv_shader_mgl.height)
            # self.window.set_default_viewport()

        # Set the icon
        if icon is not None:
            # Absolute path
            icon = Path(icon).resolve()
            resources.register_dir(icon.parent)
            self.window.set_icon(icon_path=icon.name)

        # The context we'll use is the one from the window
        self.gl_context = self.window.ctx
        self.mmv_shader_mgl.gl_context = self.gl_context
        self.window_should_close = False

        # Functions of the window if not headless
        if not self.headless:
            self.window.resize_func = self.window_resize
            self.window.key_event_func = self.key_event
            self.window.mouse_position_event_func = self.mouse_position_event
            self.window.mouse_drag_event_func = self.mouse_drag_event
            self.window.mouse_scroll_event_func = self.mouse_scroll_event
            self.window.mouse_press_event_func = self.mouse_press_event
            self.window.mouse_release_event_func = self.mouse_release_event
            self.window.unicode_char_entered_func = self.unicode_char_entered
            self.window.close_func = self.close
            imgui.create_context()
            self.imgui = ModernglWindowRenderer(self.window)
    def __init__(self, byte_array):
        # Configure to use pyglet window
        settings.WINDOW['class'] = 'moderngl_window.context.pyglet.Window'
        settings.WINDOW['size'] = (720, 720)
        settings.WINDOW['aspect_ratio'] = 1

        self.wnd = moderngl_window.create_window_from_settings()

        self.ctx = self.wnd.ctx
        self.ctx.enable(moderngl.DEPTH_TEST)

        # register event methods
        self.wnd.resize_func = self.resize
        self.wnd.iconify_func = self.iconify
        self.wnd.key_event_func = self.key_event
        self.wnd.mouse_position_event_func = self.mouse_position_event
        self.wnd.mouse_drag_event_func = self.mouse_drag_event
        self.wnd.mouse_scroll_event_func = self.mouse_scroll_event
        self.wnd.mouse_press_event_func = self.mouse_press_event
        self.wnd.mouse_release_event_func = self.mouse_release_event
        self.wnd.unicode_char_entered_func = self.unicode_char_entered
        self.wnd.close_func = self.close

        with open("shaders/tri_vert_shader.glsl") as f:
            vertex_shader = f.read()

        with open("shaders/tri_frag_shader.glsl") as f:
            fragment_shader = f.read()

        self.prog = self.ctx.program(
            vertex_shader=vertex_shader,
            fragment_shader=fragment_shader,
        )

        self.clear_color = (1.0, 1.0, 1.0)

        eye = Vector3(5.0, 0.0, 5.0)
        center = Vector3(0.0, 0.0, 0.0)
        up = Vector3(0.0, 0.0, 1.0)

        self.prog['camera_pos'].value = eye.to_tuple()

        self.look = Matrix4.look_at(center, eye, up)
        self.perspective_matrix = Matrix4.perspective_projection(
            0.1, 1000, 1, math.pi / 3)
        self.orthoganal_matrix = Matrix4.orthographic_projection(
            5, 5, -5, -5, 0.1, 1000)

        self.proj_matrix = self.look @ self.perspective_matrix
        self.prog['projection_matrix'].value = self.proj_matrix.to_tuple()

        self.prog['time'].value = 0

        self.prog['change_matrix'].value = Matrix3.random(-1, 1).to_tuple()
        self.prog['change_bias'].value = Vector3.random(-5, 5).to_tuple()
        self.prog['matrix_change_start_stop_time'].value = (0, 5)
        self.prog['bias_change_start_stop_time'].value = (5, 10)
        self.prog['activation_function_change_start_stop_time'].value = (10,
                                                                         15)

        self.shader_args = ("3f4 3f4 3f4 3f4 2f4 3f4 3f4 1f4 3f4 u1 /v",
                            "from_vert", "to_vert", "tangent_translate_from",
                            "tangent_translate_to",
                            "point_transform_start_stop_time", "normal",
                            "light_direction", "width_scale", "in_color",
                            "type")

        self.vbo = self.ctx.buffer(byte_array)
        self.vao = self.ctx.vertex_array(self.prog, [
            (self.vbo, *self.shader_args),
        ])
Example #10
0
import math
import numpy as np

import sdl2
import moderngl
import moderngl_window
from moderngl_window.conf import settings

# (math.sin(time) + 1.0) / 2,
# (math.sin(time + 2) + 1.0) / 2,
# (math.sin(time + 3) + 1.0) / 2,

settings.WINDOW['class'] = 'moderngl_window.context.sdl2.Window'
settings.WINDOW['gl_version'] = (4, 5)

window = moderngl_window.create_window_from_settings()
ctx = window.ctx

vertices = np.array(
    [
        # x, y, red, green, blue
        0.0,
        1.0,
        1.0,
        0.1,
        0.3,
        -1.0,
        -1.0,
        0.5,
        0.5,
        0.3,