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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
from pathlib import Path
import imgui
import moderngl
from pyrr import Matrix44
import moderngl_window as mglw
from moderngl_window import geometry
from moderngl_window.integrations.imgui import ModernglWindowRenderer
import PIL
class WindowEvents(mglw.WindowConfig):
gl_version = (3, 3)
title = "imgui Integration"
resource_dir = (Path(__file__).parent / '../examples/resources').resolve()
aspect_ratio = None
def __init__(self, **kwargs):
super().__init__(**kwargs)
imgui.create_context()
self.wnd.ctx.error
self.imgui = ModernglWindowRenderer(self.wnd)
self.cube = geometry.cube(size=(2, 2, 2))
self.prog = self.load_program('programs/cube_simple.glsl')
self.prog['color'].value = (1.0, 1.0, 1.0, 1.0)
self.prog['m_camera'].write(Matrix44.identity(dtype='f4'))
self.prog['m_proj'].write(Matrix44.perspective_projection(75, 1.0, 1, 100, dtype='f4'))
self.fbo = self.ctx.framebuffer(
color_attachments=self.ctx.texture((512, 512), 4),
depth_attachment=self.ctx.depth_texture((512, 512)),
)
# Ensure imgui knows about this texture
# This is the color layer in the framebuffer
self.imgui.register_texture(self.fbo.color_attachments[0])
def render(self, time: float, frametime: float):
# Rotate/move cube
rotation = Matrix44.from_eulers((time, time, time), dtype='f4')
translation = Matrix44.from_translation((0.0, 0.0, -3.5), dtype='f4')
model = translation * rotation
# Render cube to offscreen texture / fbo
self.fbo.use()
self.fbo.clear()
self.ctx.enable(moderngl.DEPTH_TEST | moderngl.CULL_FACE)
self.prog['m_model'].write(model)
self.cube.render(self.prog)
# Render UI to screen
self.wnd.use()
self.render_ui()
def render_ui(self):
"""Render the UI"""
imgui.new_frame()
if imgui.begin_main_menu_bar():
if imgui.begin_menu("File", True):
clicked_quit, selected_quit = imgui.menu_item(
"Quit", 'Cmd+Q', False, True
)
if clicked_quit:
exit(1)
imgui.end_menu()
imgui.end_main_menu_bar()
imgui.show_test_window()
imgui.begin("Custom window", True)
imgui.text("Bar")
imgui.text_colored("Eggs", 0.2, 1., 0.)
imgui.end()
# Create window with the framebuffer image
imgui.begin("Custom window with Image", True)
# Create an image control by passing in the OpenGL texture ID (glo)
# and pass in the image size as well.
# The texture needs to he registered using register_texture for this to work
imgui.image(self.fbo.color_attachments[0].glo, *self.fbo.size )
imgui.end()
imgui.render()
self.imgui.render(imgui.get_draw_data())
def resize(self, width: int, height: int):
self.imgui.resize(width, height)
def key_event(self, key, action, modifiers):
self.imgui.key_event(key, action, modifiers)
def mouse_position_event(self, x, y, dx, dy):
self.imgui.mouse_position_event(x, y, dx, dy)
def mouse_drag_event(self, x, y, dx, dy):
self.imgui.mouse_drag_event(x, y, dx, dy)
def mouse_scroll_event(self, x_offset, y_offset):
self.imgui.mouse_scroll_event(x_offset, y_offset)
def mouse_press_event(self, x, y, button):
self.imgui.mouse_press_event(x, y, button)
def mouse_release_event(self, x: int, y: int, button: int):
self.imgui.mouse_release_event(x, y, button)
def unicode_char_entered(self, char):
self.imgui.unicode_char_entered(char)
if __name__ == '__main__':
mglw.run_window_config(WindowEvents)
|