Ejemplo n.º 1
0
    def __init__(self, window_width, window_height, samples, window_title):
        self.window_title = window_title
        assert glfw.Init(), 'Glfw Init failed!'
        glfw.WindowHint(glfw.SAMPLES, samples)
        self.windowID = glfw.CreateWindow(window_width, window_height,
                                          self.window_title, None)
        assert self.windowID, 'Could not create Window!'
        glfw.MakeContextCurrent(self.windowID)
        assert glInitBindlessTextureNV(), 'Bindless Textures not supported!'
        self.framebuf_width, self.framebuf_height = glfw.GetFramebufferSize(
            self.windowID)

        def framebuffer_size_callback(window, w, h):
            self.framebuf_width, self.framebuf_height = w, h

        glfw.SetFramebufferSizeCallback(self.windowID,
                                        framebuffer_size_callback)

        def key_callback(window, key, scancode, action, mode):
            if action == glfw.PRESS:
                if key == glfw.KEY_ESCAPE:
                    glfw.SetWindowShouldClose(window, True)

        glfw.SetKeyCallback(self.windowID, key_callback)

        self.previous_second = glfw.GetTime()
        self.frame_count = 0.0

        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE)
        glDisable(GL_STENCIL_TEST)
        glDisable(GL_BLEND)
        glDisable(GL_CULL_FACE)
        glClearColor(0.0, 0.0, 0.0, 1.0)
        glEnable(GL_LINE_SMOOTH)
        glEnable(GL_POLYGON_SMOOTH)
Ejemplo n.º 2
0
    def test_window_framebuffer_resize_callback(self):
        w_called = [False]

        def w_callback(window, width, height):
            w_called[0] = True

        f_called = [False]

        def f_callback(window, width, height):
            f_called[0] = True

        glfw.SetWindowSizeCallback(self.window, w_callback)
        glfw.SetFramebufferSizeCallback(self.window, f_callback)
        glfw.SetWindowSize(self.window, 800, 600)
        glfw.PollEvents()
        glfw.SetWindowSizeCallback(self.window, None)
        glfw.SetFramebufferSizeCallback(self.window, None)
        self.assertTrue(w_called[0])
        self.assertTrue(f_called[0])
Ejemplo n.º 3
0
 def setup_loop(self, scene, camera):
     scene.set_camera(camera)
     scene.update_minmax()
     camera.compute_matrices()
     print("Starting rendering...")
     callbacks = EventCollection(scene, camera)
     # register key callbacks defined in
     # yt.visualization.volume_rendering.input_events
     callbacks.add_key_callback("close_window", "escape")
     callbacks.add_key_callback("zoomin", "w")
     callbacks.add_key_callback("zoomout", "s")
     callbacks.add_key_callback("closeup", "z")
     callbacks.add_key_callback("cmap_cycle", "c")
     callbacks.add_key_callback("reset", "r")
     callbacks.add_key_callback("camera_orto", "o")
     callbacks.add_key_callback("camera_proj", "p")
     callbacks.add_key_callback("shader_max", "1")
     callbacks.add_key_callback("shader_proj", "2")
     callbacks.add_key_callback("shader_test", "3")
     callbacks.add_key_callback("print_limits", "g")
     callbacks.add_key_callback("print_help", "h")
     callbacks.add_key_callback("debug_buffer", "d")
     callbacks.add_key_callback("cmap_max_up", "right_bracket")
     callbacks.add_key_callback("cmap_max_down", "left_bracket")
     callbacks.add_key_callback("cmap_min_up", "semicolon")
     callbacks.add_key_callback("cmap_min_down", "apostrophe")
     callbacks.add_key_callback("cmap_toggle_log", "l")
     mouse_callbacks = MouseRotation()
     callbacks.add_mouse_callback(
         mouse_callbacks.start_rotation, glfw.MOUSE_BUTTON_LEFT
     )
     callbacks.add_mouse_callback(
         mouse_callbacks.stop_rotation, glfw.MOUSE_BUTTON_LEFT, action="release"
     )
     callbacks.add_framebuffer_callback("framebuffer_size_callback")
     callbacks.add_render_callback(mouse_callbacks.do_rotation)
     glfw.SetFramebufferSizeCallback(self.window, callbacks.framebuffer_call)
     glfw.SetKeyCallback(self.window, callbacks.key_call)
     glfw.SetMouseButtonCallback(self.window, callbacks.mouse_call)
     callbacks.draw = True
     return callbacks
Ejemplo n.º 4
0
 def add_framebuffer_size_callback(self, callback):
     if not hasattr(self, '_framebuffer_size_callbacks'):
         self._framebuffer_size_callbacks = []
         glfw.SetFramebufferSizeCallback(self._window,
                                         self.__framebuffer_size_callback__)
     self._framebuffer_size_callbacks.append(callback)
Ejemplo n.º 5
0
bridge = CvBridge()

window_width, window_height = 640, 480
samples = 16
window_title = 'OpenCV+OpenGL'
assert glfw.Init(), 'Glfw Init failed!'
glfw.WindowHint(glfw.SAMPLES, samples)
window = glfw.CreateWindow(window_width, window_height, window_title, None)
assert window, 'Could not create Window!'
glfw.MakeContextCurrent(window)
assert glInitBindlessTextureNV(), 'Bindless Textures not supported!'
framebuf_width, framebuf_height = glfw.GetFramebufferSize(window)
def framebuffer_size_callback(window, w, h):
    global framebuf_width, framebuf_height
    framebuf_width, framebuf_height = w, h
glfw.SetFramebufferSizeCallback(window, framebuffer_size_callback)
def key_callback(window, key, scancode, action, mode):
    if action == glfw.PRESS:
        if key == glfw.KEY_ESCAPE:
            glfw.SetWindowShouldClose(window, True)
glfw.SetKeyCallback(window, key_callback)

previous_second = glfw.GetTime(); frame_count = 0.0
def update_fps_counter(window):
    global previous_second, frame_count
    current_second = glfw.GetTime()
    elapsed_seconds = current_second - previous_second
    if elapsed_seconds > 1.0:
        previous_second = current_second
        fps = float(frame_count) / float(elapsed_seconds)
        glfw.SetWindowTitle(window, '%s @ FPS: %.2f' % (window_title, fps))
Ejemplo n.º 6
0
    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,
            )
Ejemplo n.º 7
0
    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)