def new_frame(self): # todo: consider moving to init if not self._font_texture: self._create_device_objects() io = imgui.get_io() w, h = glfw.get_window_size(self.window) dw, dh = glfw.get_framebuffer_size(self.window) io.display_size = w, h io.display_fb_scale = float(dw) / w, float(dh) / h io.delta_time = 1.0 / 60 if glfw.get_window_attrib(self.window, glfw.FOCUSED): io.mouse_pos = glfw.get_cursor_pos(self.window) else: io.mouse_pos = -1, -1 # todo: py3k compat for i in xrange(3): io.mouse_down[i] = glfw.get_mouse_button(self.window, i) imgui.new_frame()
def process_inputs(self): io = imgui.get_io() window_size = glfw.get_window_size(self.window) fb_size = glfw.get_framebuffer_size(self.window) io.display_size = window_size io.display_fb_scale = compute_fb_scale(window_size, fb_size) io.delta_time = 1.0 / 60 if glfw.get_window_attrib(self.window, glfw.FOCUSED): io.mouse_pos = glfw.get_cursor_pos(self.window) else: io.mouse_pos = -1, -1 io.mouse_down[0] = glfw.get_mouse_button(self.window, 0) io.mouse_down[1] = glfw.get_mouse_button(self.window, 1) io.mouse_down[2] = glfw.get_mouse_button(self.window, 2) current_time = glfw.get_time() if self._gui_time: self.io.delta_time = current_time - self._gui_time else: self.io.delta_time = 1. / 60. self._gui_time = current_time
def process_inputs(self): # todo: consider moving to init io = imgui.get_io() w, h = glfw.get_window_size(self.window) dw, dh = glfw.get_framebuffer_size(self.window) io.display_size = w, h io.display_fb_scale = float(dw) / w, float(dh) / h io.delta_time = 1.0 / 60 if glfw.get_window_attrib(self.window, glfw.FOCUSED): io.mouse_pos = glfw.get_cursor_pos(self.window) else: io.mouse_pos = -1, -1 io.mouse_down[0] = glfw.get_mouse_button(self.window, 0) io.mouse_down[1] = glfw.get_mouse_button(self.window, 1) io.mouse_down[2] = glfw.get_mouse_button(self.window, 2) current_time = glfw.get_time() if self._gui_time: self.io.delta_time = current_time - self._gui_time else: self.io.delta_time = 1. / 60. self._gui_time = current_time
def _poll_mouse(self, window): hover_state = glfw.get_window_attrib(window, glfw.HOVERED) self._hover = True if hover_state == 1 else False if self._hover: x, y = glfw.get_cursor_pos(window) self._dx = self._x - x self._dy = self._y - y self._x = x self._y = y for btn, btn_state in self._buttons.items(): cur_state = glfw.get_mouse_button(window, btn) pressed = cur_state == glfw.PRESS released = cur_state == glfw.RELEASE if pressed and not btn_state.pressed and not btn_state.held: btn_state.pressed = True btn_state.released = False btn_state.held = True elif pressed and btn_state.held: btn_state.pressed = False btn_state.released = False btn_state.held = True elif released and btn_state.held: btn_state.pressed = False btn_state.released = True btn_state.held = False else: btn_state.pressed = False btn_state.released = False btn_state.held = False
def key_callback(_, key, __, 方向, ___): if 方向 != 1: return d = { 'D': glfw.DECORATED, 'F': glfw.FLOATING, } if chr(key) in d: flag = d[chr(key)] t = glfw.get_window_attrib(window, flag) glfw.set_window_attrib(window, flag, not t) # esc if key == 256: glfw.set_window_should_close(window, True)
def on_resize(window, w, h): nonlocal window_size nonlocal content_scale is_minimized = bool(glfw.get_window_attrib(window, glfw.ICONIFIED)) if is_minimized: return # Always clear buffers on resize to make sure that there are no overlapping # artifacts from previous frames. gl_utils.glClear(GL_COLOR_BUFFER_BIT) gl_utils.glClearColor(0, 0, 0, 1) active_window = glfw.get_current_context() glfw.make_context_current(window) content_scale = gl_utils.get_content_scale(window) framebuffer_scale = gl_utils.get_framebuffer_scale(window) g_pool.gui.scale = content_scale window_size = w, h g_pool.camera_render_size = w - int( icon_bar_width * g_pool.gui.scale), h g_pool.gui.update_window(w, h) g_pool.gui.collect_menus() for g in g_pool.graphs: g.scale = content_scale g.adjust_window_size(w, h) adjust_gl_view(w, h) glfw.make_context_current(active_window) # Minimum window size required, otherwise parts of the UI can cause openGL # issues with permanent effects. Depends on the content scale, which can # potentially be dynamically modified, so we re-adjust the size limits every # time here. min_size = int(2 * icon_bar_width * g_pool.gui.scale / framebuffer_scale) glfw.set_window_size_limits( window, min_size, min_size, glfw.DONT_CARE, glfw.DONT_CARE, ) # Needed, to update the window buffer while resizing consume_events_and_render_buffer()
def is_window_visible(window): visible = glfw.get_window_attrib(window, glfw.VISIBLE) iconified = glfw.get_window_attrib(window, glfw.ICONIFIED) return visible and not iconified
def main(): if not glfw.init(): raise RuntimeError('Failed to initialize GLFW') version = glfw.get_version_string().decode('ASCII') print('GLFW', version) monitors = glfw.get_monitors() for i, monitor in enumerate(monitors): name = glfw.get_monitor_name(monitor) primary = (glfw.get_monitor_pos(monitor) == glfw.get_monitor_pos(glfw.get_primary_monitor())) print('Monitor #{}: {}{}'.format(i, name.decode('utf8'), ' (primary)' if primary else '')) width_mm, height_mm = glfw.get_monitor_physical_size(monitor) diag_mm = math.sqrt(width_mm*width_mm + height_mm*height_mm) print('Diagonal: {:.1f}"'.format(diag_mm / 25.4)) mode = glfw.get_video_mode(monitor) print('Video mode: {}x{} {}Hz {}'.format(mode.size.width, mode.size.height, mode.refresh_rate, mode.bits)) xscale, yscale = glfw.get_monitor_content_scale(monitor) print('Scale: {}|{}'.format(xscale, yscale)) print('Virtual position:', glfw.get_monitor_pos(monitor)) print('Work ares:', glfw.get_monitor_workarea(monitor)) for mode in glfw.get_video_modes(monitor): print('Supported: {}x{} {}Hz {}'.format(mode.size.width, mode.size.height, mode.refresh_rate, mode.bits)) print(mode) print() glfw.window_hint(glfw.RESIZABLE, True) glfw.window_hint(glfw.STENCIL_BITS, 8) glfw.window_hint(glfw.CLIENT_API, glfw.OPENGL_API) glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 6) glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, True) glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) monitor = glfw.get_primary_monitor() mode = glfw.get_video_mode(monitor) window = glfw.create_window(640, 480, 'Title', None, None) # window = glfw.create_window(mode.size.width, mode.size.height, 'Title', monitor, None) if window is None: glfw.terminate() raise RuntimeError('Failed to create a window') # glfw.set_window_monitor(window, monitor, 0, 0, mode.size.width, mode.size.height, mode.refresh_rate) width, height = glfw.get_window_size(window) print('Window size: {}x{}'.format(width, height)) print('Frame size:', glfw.get_window_frame_size(window)) width, height = glfw.get_framebuffer_size(window) print('Framebuffer size: {}x{}'.format(width, height)) print('Client API:', glfw.get_window_attrib(window, glfw.CLIENT_API)) version_major = glfw.get_window_attrib(window, glfw.CONTEXT_VERSION_MAJOR) version_minor = glfw.get_window_attrib(window, glfw.CONTEXT_VERSION_MINOR) revision = glfw.get_window_attrib(window, glfw.CONTEXT_REVISION) print('Version: {}.{} rev{}'.format(version_major, version_minor, revision)) glfw.make_context_current(window) renderer = Renderer(window) while not glfw.window_should_close(window): GL.glClear(GL.GL_COLOR_BUFFER_BIT) renderer.render() glfw.swap_buffers(window) glfw.poll_events() glfw.terminate()
def get_config(self) -> WindowConfig: w, h = glfw.get_window_size(self.window) x, y = glfw.get_window_pos(self.window) is_maximized = True if glfw.get_window_attrib( self.window, glfw.MAXIMIZED) else False return WindowConfig(x, y, w, h, is_maximized)
glfw.window_hint(glfw.AUTO_ICONIFY, 0) win = glfw.create_window(width=width, height=height, title='', monitor=monitor, share=None) glfw.make_context_current(win) glfw.swap_interval(True) # vsync? glfw.set_input_mode(win, glfw.CURSOR, glfw.CURSOR_HIDDEN) glfw.set_key_callback(win, on_key) # set up moderngl context major = glfw.get_window_attrib(win, glfw.CONTEXT_VERSION_MAJOR) minor = glfw.get_window_attrib(win, glfw.CONTEXT_VERSION_MINOR) ctx = mgl.create_context(require=int('%i%i0' % (major, minor))) ctx.viewport = (0, 0, width, height) ctx.disable(mgl.DEPTH_TEST) # set up square # we want 100px by 100px square on edge of screen # origin is top left corner prog = ctx.program(vertex_shader=vert_string, fragment_shader=frag_string) proj = glm.ortho(0, width, height, 0) verts = [ *(proj * glm.vec4(0, height // 2 - 50, 0, 1)).to_list()[:2], *(proj * glm.vec4(0, height // 2 + 50, 0, 1)).to_list()[:2],
from OpenGLContext.arrays import * import numpy as np import ctypes glfw.init() glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3); glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 2); glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, GL_TRUE); glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE); window = glfw.create_window(256, 240, "Hello World", None, None) assert (glfw.get_window_attrib(window, glfw.CONTEXT_VERSION_MAJOR) >= 3) print (glfw.get_window_attrib(window, glfw.CONTEXT_VERSION_MAJOR), glfw.get_window_attrib(window, glfw.CONTEXT_VERSION_MINOR)) vao_id = GLuint(0) # I hate python gl bindings pyglet.gl.glGenVertexArrays(1, ctypes.byref(vao_id)) pyglet.gl.glBindVertexArray(vao_id.value) vertexShaderSrc = """#version 150 in vec2 position; in vec3 color; out vec3 fColor;