def init(self): assert not self._initialized glfw.SetMouseButtonCallback(self._window, self._on_mouse_button) self._double_clicks.set_double_click_callback( self._on_mouse_double_click) glfw.SetScrollCallback(self._window, self._on_scroll) glfw.SetCursorPosCallback(self._window, self._on_mouse_move) glfw.SetKeyCallback(self._window, self._on_keyboard) glfw.SetDropCallback(self._window, self._on_drop) glfw.SetWindowSizeCallback(self._window, self._on_resize) glfw.SetWindowCloseCallback(self._window, lambda window: self._fps_limiter.stop()) yield from self._gl_executor.init_gl_context() yield from self._renderer.init() self._initialized = True
def add_cursor_pos_callback(self, callback): if not hasattr(self, '_cursor_pos_callbacks'): self._cursor_pos_callbacks = [] glfw.SetCursorPosCallback(self._window, self.__cursor_pos_callback__) self._cursor_pos_callbacks.append(callback)
def __init__(self, window_width, window_height, samples=1, window_title="", monitor=1, show_at_center=True, offscreen=False): self.window_title = window_title assert glfw.Init(), "Glfw Init failed!" glfw.WindowHint(glfw.SAMPLES, samples) if offscreen: glfw.WindowHint(glfw.VISIBLE, False) mon = glfw.GetMonitors()[monitor] if monitor != None else None self.windowID = glfw.CreateWindow(window_width, window_height, self.window_title, mon) assert self.windowID, "Could not create Window!" glfw.MakeContextCurrent(self.windowID) if not glInitBindlessTextureNV(): raise RuntimeError("Bindless Textures not supported") self.framebuf_width, self.framebuf_height = glfw.GetFramebufferSize( self.windowID) self.framebuffer_size_callback = [] def framebuffer_size_callback(window, w, h): self.framebuf_width, self.framebuf_height = w, h for callback in self.framebuffer_size_callback: callback(w, h) glfw.SetFramebufferSizeCallback(self.windowID, framebuffer_size_callback) self.key_callback = [] def key_callback(window, key, scancode, action, mode): if action == glfw.PRESS: if key == glfw.KEY_ESCAPE: glfw.SetWindowShouldClose(window, True) for callback in self.key_callback: callback(key, scancode, action, mode) glfw.SetKeyCallback(self.windowID, key_callback) self.mouse_callback = [] def mouse_callback(window, xpos, ypos): for callback in self.mouse_callback: callback(xpos, ypos) glfw.SetCursorPosCallback(self.windowID, mouse_callback) self.mouse_button_callback = [] def mouse_button_callback(window, button, action, mods): for callback in self.mouse_button_callback: callback(button, action, mods) glfw.SetMouseButtonCallback(self.windowID, mouse_button_callback) self.scroll_callback = [] def scroll_callback(window, xoffset, yoffset): for callback in self.scroll_callback: callback(xoffset, yoffset) glfw.SetScrollCallback(self.windowID, scroll_callback) self.previous_second = glfw.GetTime() self.frame_count = 0.0 if show_at_center: monitors = glfw.GetMonitors() assert monitor >= 0 and monitor < len( monitors), "Invalid monitor selected." vidMode = glfw.GetVideoMode(monitors[monitor]) glfw.SetWindowPos( self.windowID, vidMode.width / 2 - self.framebuf_width / 2, vidMode.height / 2 - self.framebuf_height / 2, )
#glfw.SetCharCallback(window, character_callback) def charmods_callback(window, codepoint, mods): print codepoint, mods glfw.SetCharModsCallback(window, charmods_callback) def cursor_pos_callback(window, xpos, ypos): pass #print xpos, ypos glfw.SetCursorPosCallback(window, cursor_pos_callback) #glfw.SetInputMode(window, glfw.CURSOR, glfw.CURSOR_DISABLED) #glfwSetCursor(window, glfw.CURSOR_NORMAL); def cursor_enter_callback(window, entered): pass glfw.SetCursorEnterCallback(window, cursor_enter_callback) from scipy import misc import numpy as np from PIL import Image
def mpos_callback(self, value): self._mpos_callback = value if self._win: glfw.SetCursorPosCallback(self._win, self._mpos_callback)
def __init__(self, width, height, title): glfw.WindowHint(glfw.CONTEXT_VERSION_MAJOR, 4) glfw.WindowHint(glfw.CONTEXT_VERSION_MINOR, 1) glfw.WindowHint(glfw.OPENGL_FORWARD_COMPAT, 1) glfw.WindowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) glfw.WindowHint(glfw.RESIZABLE, True) glfw.WindowHint(glfw.RED_BITS, 8) glfw.WindowHint(glfw.GREEN_BITS, 8) glfw.WindowHint(glfw.BLUE_BITS, 8) # glfw.WindowHint(glfw.ALPHA_BITS, 8) glfw.WindowHint(glfw.DEPTH_BITS, 24) glfw.WindowHint(glfw.STENCIL_BITS, 8) self.window = glfw.CreateWindow(width, height, title) self.framebufferSize = (width, height) f = 240 pos = np.array([3, 3, 3]) dir = np.array([-1, -1, -1]) up = np.array([0, 0, 1]) near = 0.2 far = 100 self.playerCamera = PlayerCamera(f, self.framebufferSize, pos, dir, up, near, far) self.currentCamera = self.playerCamera self.cameraLocked = True self.mainRender = None self.cairoSavePath = None def key_callback(window, key, scancode, action, mods): if (key == glfw.KEY_ESCAPE and action == glfw.PRESS): glfw.SetWindowShouldClose(window, True) if (action == glfw.PRESS and key == glfw.KEY_L): self.cameraLocked = not self.cameraLocked print 'cameraLocked:', self.cameraLocked glfw.SetInputMode(window, glfw.CURSOR, glfw.CURSOR_NORMAL if self.cameraLocked else glfw.CURSOR_DISABLED) # if not self.cameraLocked: # Ensure that locking/unlocking doesn't move the view: windowWidth, windowHeight = glfw.GetWindowSize(window) glfw.SetCursorPos(window, windowWidth / 2, windowHeight / 2) self.currentCamera.lastCursor = np.array(glfw.GetCursorPos(window), np.float32) self.currentCamera.lastUpdateTime = glfw.GetTime() if (action == glfw.PRESS and key == glfw.KEY_R): self.mainRender.renderCairo(self.playerCamera, self.cairoSavePath) pass # # print( # "keybrd: key=%s scancode=%s action=%s mods=%s" % # (key, scancode, action, mods)) def char_callback(window, char): pass # print("unichr: char=%s" % char) def scroll_callback(window, off_x, off_y): pass # print("scroll: x=%s y=%s" % (off_x, off_y)) def mouse_button_callback(window, button, action, mods): pass # print("button: button=%s action=%s mods=%s" % (button, action, mods)) def cursor_enter_callback(window, status): pass # print("cursor: status=%s" % status) def cursor_pos_callback(window, pos_x, pos_y): pass # print("curpos: x=%s y=%s" % (pos_x, pos_y)) def window_size_callback(window, wsz_w, wsz_h): pass # print("window: w=%s h=%s" % (wsz_w, wsz_h)) def window_pos_callback(window, pos_x, pos_y): pass # print("window: x=%s y=%s" % (pos_x, pos_y)) def window_close_callback(window): pass # print("should: %s" % self.should_close) def window_refresh_callback(window): pass # print("redraw") def window_focus_callback(window, status): pass # print("active: status=%s" % status) def window_iconify_callback(window, status): pass # print("hidden: status=%s" % status) def framebuffer_size_callback(window, fbs_x, fbs_y): print("buffer: x=%s y=%s" % (fbs_x, fbs_y)) self.framebufferSize = (fbs_x, fbs_y) self.playerCamera.framebufferSize = (fbs_x, fbs_y) glViewport(0, 0, self.currentCamera.framebufferSize[0], self.currentCamera.framebufferSize[1]) pass glfw.SetKeyCallback(self.window, key_callback) glfw.SetCharCallback(self.window, char_callback) glfw.SetScrollCallback(self.window, scroll_callback) glfw.SetMouseButtonCallback(self.window, mouse_button_callback) glfw.SetCursorEnterCallback(self.window, cursor_enter_callback) glfw.SetCursorPosCallback(self.window, cursor_pos_callback) glfw.SetWindowSizeCallback(self.window, window_size_callback) glfw.SetWindowPosCallback(self.window, window_pos_callback) glfw.SetWindowCloseCallback(self.window, window_close_callback) glfw.SetWindowRefreshCallback(self.window, window_refresh_callback) glfw.SetWindowFocusCallback(self.window, window_focus_callback) glfw.SetWindowIconifyCallback(self.window, window_iconify_callback) glfw.SetFramebufferSizeCallback(self.window, framebuffer_size_callback)