Example #1
0
    def __init__(self, sim):
        super().__init__(sim)
        self.key_pressed = None
        self.custom_text = None
        self.prog_info = None

        glfw.set_window_size(self.window, 840, 680)
Example #2
0
    def _set_logical_size(self):
        # There is unclarity about the window size in "screen pixels".
        # It appears that on Windows and X11 its the same as the
        # framebuffer size, and on macOS it's logical pixels.
        # See https://github.com/glfw/glfw/issues/845
        # Here, we simply do a quick test so we can compensate.

        # The target logical size
        lsize = self._logical_size
        pixel_ratio = glfw.get_window_content_scale(self._window)[0]
        # The current screen size and physical size, and its ratio
        ssize = glfw.get_window_size(self._window)
        psize = glfw.get_framebuffer_size(self._window)
        # Apply
        if is_wayland:
            # Not sure why, but on Wayland things work differently
            screen_ratio = ssize[0] / lsize[0]
            glfw.set_window_size(
                self._window,
                int(lsize[0] / screen_ratio),
                int(lsize[1] / screen_ratio),
            )
        else:
            screen_ratio = ssize[0] / psize[0]
            glfw.set_window_size(
                self._window,
                int(lsize[0] * pixel_ratio / screen_ratio),
                int(lsize[1] * pixel_ratio / screen_ratio),
            )
Example #3
0
    def __init__(self, parent, monitor):
        self.parent = parent
        self.monitor = monitor

        self.running = True
        self.modman = ModuleManager()

        self.currentSheet = None
        self.sheetObjects = {}
        self.subsheets = {}

        self.miscdata = {}  # Special cases are ~fun~! (The f stands for "FUUUCK why did I do this?!")

        self.videomode = glfw.get_video_mode(monitor)
        self.window = glfw.create_window(100, 100, "Hello World", monitor, None)
        #self.window = glfw.create_window(100, 100, "Hello World", None, None)
        if not self.window:
            raise Exception("Creating window failed")
        glfw.set_window_size(self.window, self.videomode[0][0], self.videomode[0][1])

        glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
        glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
        glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)

        glfw.set_framebuffer_size_callback(self.window, self.framebufferSizeCallback)

        self.deltatime = 0
        self.time = glfw.get_time()


        self.beatlow = False
        self.beatmid = False
        self.beathigh = False
        self.bpm = 0
Example #4
0
    def render(self, mode: RenderMode = RenderMode(), render_step: int = 1):
        if self._curr_step % render_step == 0:
            # Call base class
            super().render(mode)

            # Print to console
            if mode.text:
                print(
                    f"step: {self._curr_step:4d}  |  r_t: {self._curr_rew: 1.3f}  |  a_t: {self._curr_act}  |  s_t+1: {self.state}"
                )

            # Forward to MuJoCo viewer
            if mode.video:
                if self.viewer is None:
                    # Create viewer if not existent (see 'human' mode of OpenAI Gym's MujocoEnv)
                    self.viewer = mujoco_py.MjViewer(self.sim)

                    # Adjust window size and position to custom values
                    import glfw

                    glfw.make_context_current(self.viewer.window)
                    glfw.set_window_size(self.viewer.window, 1280, 720)
                    glfw.set_window_pos(self.viewer.window, 50, 50)

                    self.configure_viewer()
                self.viewer.render()
Example #5
0
    def loadImgFromArray(self, img=None):
        self.img = img.copy()
        self.img_height, self.img_width, self.channels = self.img.shape

        self.numLevels = 1 + math.floor(
            math.log2(max(self.img_width, self.img_height)))

        if self.visualize:
            glfw.set_window_size(self.window, self.img_width, self.img_height)
        #self.updateTexture()
        gazePosition = self.gazePosition
        if gazePosition[0] < 0:
            gazePosition = (self.img_height / 2, self.img_width / 2)

        x, step = np.linspace(0,
                              self.img_height - 1,
                              num=self.img_height,
                              retstep=True,
                              dtype=np.float32)
        y, step = np.linspace(0,
                              self.img_width - 1,
                              num=self.img_width,
                              retstep=True,
                              dtype=np.float32)

        self.ix, self.iy = np.meshgrid(y, x, sparse=False, indexing='xy')

        self.updateGaze(gazePosition)
        self.updateTexture()
Example #6
0
    def loadImgFromArray(self, img=None):
        self.img = img.copy()
        self.img_width, self.img_height = self.img.size

        self.numLevels = 1 + math.floor(
            math.log2(max(self.img_width, self.img_height)))

        if self.visualize:
            glfw.set_window_size(self.window, self.img_width, self.img_height)
        #self.updateTexture()
        if self.gazePosition[0] < 0:
            self.gazePosition = (self.img_height / 2, self.img_width / 2)
            #self.updateGaze(gazePosition)

        x, step = np.linspace(0,
                              self.img_height - 1,
                              num=self.img_height,
                              retstep=True,
                              dtype=np.float32)
        y, step = np.linspace(0,
                              self.img_width - 1,
                              num=self.img_width,
                              retstep=True,
                              dtype=np.float32)

        self.ix, self.iy = np.meshgrid(y, x, sparse=False, indexing='xy')

        self.dotPitch = computeDotPitch(pix2deg=self.pix2deg,
                                        viewDist=self.viewDist,
                                        imgWidth=self.img_width)

        self.updateGaze(gazePosition)
        self.updateTexture()
Example #7
0
    def loadImgFromFile(self, imgFilename='images/Yarbus_scaled.jpg'):
        self.img = cv2.imread(imgFilename)
        self.img_height, self.img_width, self.channels = self.img.shape

        self.numLevels = 1 + math.floor(
            math.log2(max(self.img_width, self.img_height)))

        if self.visualize:
            glfw.set_window_size(self.window, self.img_width, self.img_height)

        if self.gazePosition[0] < 0:
            gazePosition = (self.img_height / 2, self.img_width / 2)
        else:
            gazePosition = self.gazePosition

        x, step = np.linspace(0,
                              self.img_height - 1,
                              num=self.img_height,
                              retstep=True,
                              dtype=np.float32)
        y, step = np.linspace(0,
                              self.img_width - 1,
                              num=self.img_width,
                              retstep=True,
                              dtype=np.float32)

        self.ix, self.iy = np.meshgrid(y, x, sparse=False, indexing='xy')

        #self.dotPitch = computeDotPitch(pix2deg=self.pix2deg, viewDist=self.viewDist, imgWidth=self.img_width)

        self.updateGaze(gazePosition)
        self.updateTexture()
Example #8
0
 def morph_2D_mesh(self, H, W, points_coord, points_text_coord, triangles):
     glfw.set_window_size(self.window, W, H)
     self.__draw_2d_mesh(H, W, points_coord, points_text_coord, triangles)
     image_buffer = glReadPixels(0, 0, W, H, OpenGL.GL.GL_RGB,
                                 OpenGL.GL.GL_UNSIGNED_BYTE)
     image = numpy.frombuffer(image_buffer,
                              dtype=numpy.uint8).reshape(H, W, 3)
     return image[:H, :W]
Example #9
0
 def get_image(self, image):
     width, height = image.shape[1], image.shape[0]
     glfw.set_window_size(self.window, width, height)
     self.__draw_image(image)
     image_buffer = glReadPixels(0, 0, width, height, OpenGL.GL.GL_RGB,
                                 OpenGL.GL.GL_UNSIGNED_BYTE)
     image = numpy.frombuffer(image_buffer,
                              dtype=numpy.uint8).reshape(height, width, 3)
     return image
Example #10
0
 def loadImgFromArray(self, img=None):
     self.img = img.copy()
     self.img_width, self.img_height = self.img.size
     if self.visualize:
         glfw.set_window_size(self.window, self.img_width, self.img_height)
     self.updateTexture()
     if self.gazePosition[0] < 0:
         self.gazePosition = (self.img_height / 2, self.img_width / 2)
         self.updateGaze(gazeRadius, gazePosition)
Example #11
0
  def activate(self, width, height):
    """Called when entering the `make_current` context manager.

    Args:
      width: Integer specifying the new framebuffer width in pixels.
      height: Integer specifying the new framebuffer height in pixels.
    """
    self._previous_context = glfw.get_current_context()
    glfw.make_context_current(self._context)
    if (width, height) != glfw.get_window_size(self._context):
      glfw.set_window_size(self._context, width, height)
Example #12
0
    def on_cursor_pos(self, window, x, y):
        pos = ivec2(x, y)
        delta = pos - self.prev_pos
        self.prev_pos = pos

        if self.is_drag_window:
            win_pos = ivec2(*glfw.get_window_pos(window))
            win_pos += delta
            glfw.set_window_pos(window, *win_pos)
        if self.is_scale_window:
            win_size = ivec2(*glfw.get_window_size(window))
            win_size += delta
            glfw.set_window_size(window, *win_size)
Example #13
0
    def loadImgFromFile(self, imgFilename='images/Yarbus_scaled.jpg'):
        self.img = Image.open(imgFilename)
        self.img_width, self.img_height = self.img.size
        if self.visualize:
            glfw.set_window_size(self.window, self.img_width, self.img_height)

        self.updateTexture()

        if self.gazePosition[0] < 0:
            gazePosition = (self.img_height / 2, self.img_width / 2)
        else:
            gazePosition = self.gazePosition
        self.updateGaze(self.gazeRadius, gazePosition)
Example #14
0
    def on_scroll(self, window, x, y):
        self._isresizing = True

        w, h = glfw.get_window_size(window)
        a = 1.1 if y > 0.0 else 0.9
        w, h = w * a, h * a
        w, h = int(clamp(w, 128, 4096)), int(clamp(h, 128, 4096))
        old_w, old_h = glfw.get_window_size(window)
        glfw.set_window_size(window, w, h)
        x, y = glfw.get_window_pos(window)
        glfw.set_window_pos(window, x - (w - old_w) // 2, y - (h - old_h) // 2)

        self._isresizing = False
Example #15
0
        def set_window_size():
            # Get default window size
            f_width, f_height = window_size_default

            # Get current display scale factor
            content_scale = gl_utils.get_content_scale(main_window)
            framebuffer_scale = gl_utils.get_framebuffer_scale(main_window)
            display_scale_factor = content_scale / framebuffer_scale

            # Scale the capture frame size by display scale factor
            f_width *= display_scale_factor
            f_height *= display_scale_factor

            # Set the newly calculated size (scaled capture frame size + scaled icon bar width)
            glfw.set_window_size(main_window, int(f_width), int(f_height))
Example #16
0
    def _get_viewer(self, mode) -> mujoco_py.MjViewer:
        self.viewer = self._viewers.get(mode)
        if self.viewer is not None:
            return self.viewer

        if mode == 'human':
            self.viewer = mujoco_py.MjViewer(self.sim)
            # we turn off the overlay and make the window smaller.
            self.viewer._hide_overlay = True
            import glfw
            glfw.set_window_size(self.viewer.window, self.default_window_width, self.default_window_height)
        else:
            self.viewer = mujoco_py.MjRenderContextOffscreen(self.sim, -1)
        self.viewer_setup()
        self._viewers[mode] = self.viewer
        return self.viewer
def display_image(image, zoom=None, size=None, title=None):  # HWC
    # Import OpenGL and glfw.
    import OpenGL.GL as gl
    import glfw

    # Zoom image if requested.
    image = np.asarray(image)
    if size is not None:
        assert zoom is None
        zoom = max(1, size // image.shape[0])
    if zoom is not None:
        image = image.repeat(zoom, axis=0).repeat(zoom, axis=1)
    height, width, channels = image.shape

    # Initialize window.
    if title is None:
        title = 'Debug window'
    global _glfw_window
    if _glfw_window is None:
        glfw.init()
        _glfw_window = glfw.create_window(width, height, title, None, None)
        glfw.make_context_current(_glfw_window)
        glfw.show_window(_glfw_window)
        glfw.swap_interval(0)
    else:
        glfw.make_context_current(_glfw_window)
        glfw.set_window_title(_glfw_window, title)
        glfw.set_window_size(_glfw_window, width, height)

    # Update window.
    glfw.poll_events()
    gl.glClearColor(0, 0, 0, 1)
    gl.glClear(gl.GL_COLOR_BUFFER_BIT)
    gl.glWindowPos2f(0, 0)
    gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1)
    gl_format = {3: gl.GL_RGB, 2: gl.GL_RG, 1: gl.GL_LUMINANCE}[channels]
    gl_dtype = {
        'uint8': gl.GL_UNSIGNED_BYTE,
        'float32': gl.GL_FLOAT
    }[image.dtype.name]
    gl.glDrawPixels(width, height, gl_format, gl_dtype, image[::-1])
    glfw.swap_buffers(_glfw_window)
    if glfw.window_should_close(_glfw_window):
        return False
    return True
Example #18
0
        def set_window_size():
            # Get current capture frame size
            f_width, f_height = g_pool.capture.frame_size

            # Get current display scale factor
            content_scale = gl_utils.get_content_scale(main_window)
            framebuffer_scale = gl_utils.get_framebuffer_scale(main_window)
            display_scale_factor = content_scale / framebuffer_scale

            # Scale the capture frame size by display scale factor
            f_width *= display_scale_factor
            f_height *= display_scale_factor

            # Increas the width to account for the added scaled icon bar width
            f_width += icon_bar_width * display_scale_factor

            # Set the newly calculated size (scaled capture frame size + scaled icon bar width)
            glfw.set_window_size(main_window, int(f_width), int(f_height))
Example #19
0
    def render(self, mode='human', width=None, height=None):
        """
        returns images of modality <modeL

        :param mode: One of ['human', 'rgb', 'rgbd', 'depth']
        :param kwargs: width, height (in pixels) of the image.
        :return: image(, depth). image is between [0, 1), depth is distance.
        """
        width = width or self.width
        height = height or self.height
        viewer = self._get_viewer(mode, cam_id=self.cam_id)

        viewer.render(width, height)

        if mode in ['rgb', 'rgb_array']:
            data = viewer.read_pixels(width, height, depth=False)
            # original image is upside-down, so flip it
            return data[::-1, :, :]
        elif mode == 'rgbd':
            rgb, d = viewer.read_pixels(width, height, depth=True)
            # original image is upside-down, so flip it
            return rgb[::-1, :, :], d[::-1, :]
        elif mode == 'depth':
            _, d = viewer.read_pixels(width, height, depth=True)
            # original image is upside-down, so flip it
            return d[::-1, :]
        elif mode == 'grey':
            data = viewer.read_pixels(width, height, depth=False)
            # original image is upside-down, so flip it
            return data[::-1, :, :].mean(axis=-1).astype(np.uint8)
        elif mode == 'notebook':
            from PIL import Image
            from IPython.display import display

            data = viewer.read_pixels(width, height, depth=False)
            img = Image.fromarray(data[::-1])
            display(img)
            return data[::-1]
        elif mode == 'human':
            if width and height:
                import glfw
                glfw.set_window_size(viewer.window, width, height)
            viewer.render()
Example #20
0
    def _get_viewer(self, mode, cam_id) -> mujoco_py.MjViewer:
        mode_cam_id = mode, cam_id

        self.viewer = self._viewers.get(mode_cam_id)
        if self.viewer is not None:
            if sys.platform == 'darwin':
                # info: to fix the black image of death.
                self.viewer._set_mujoco_buffers()
            return self.viewer

        if mode == 'human':
            self.viewer = mujoco_py.MjViewer(self.sim)
            # we turn off the overlay and make the window smaller.
            self.viewer._hide_overlay = True
            import glfw
            glfw.set_window_size(self.viewer.window, self.width, self.height)
        else:
            self.viewer = mujoco_py.MjRenderContextOffscreen(self.sim, -1)

        self.viewer_setup()
        self._viewers[mode_cam_id] = self.viewer
        return self.viewer
Example #21
0
    def render(self, mode='human', width=None, height=None):
        """
        returns images of modality <modeL

        :param mode: One of ['human', 'rgb', 'rgbd', 'depth']
        :param kwargs: width, height (in pixels) of the image.
        :return: image(, depth). image is between [0, 1), depth is distance.
        """
        width = width or self.default_window_width
        height = height or self.default_window_height
        viewer = self._get_viewer(mode)

        self._before_render()

        if mode in ['rgb', 'rgb_array']:
            viewer.render(width, height)
            data = viewer.read_pixels(width, height, depth=False)
            # original image is upside-down, so flip it
            return data[::-1, :, :]
        elif mode == 'rgbd':
            viewer.render(width, height)
            rgb, d = viewer.read_pixels(width, height, depth=True)
            # original image is upside-down, so flip it
            return rgb[::-1, :, :], d[::-1, :]
        elif mode == 'depth':
            viewer.render(width, height)
            _, d = viewer.read_pixels(width, height, depth=True)
            # original image is upside-down, so flip it
            return d[::-1, :]
        elif mode == 'grey':
            viewer.render(width, height)
            data = viewer.read_pixels(width, height, depth=False)
            # original image is upside-down, so flip it
            return data[::-1, :, :].mean(axis=-1).astype(np.uint8)
        elif mode == 'human':
            if width and height:
                import glfw
                glfw.set_window_size(viewer.window, width, height)
            viewer.render()
Example #22
0
File: eye.py Project: N-M-T/pupil
        def set_window_size():
            # Get current capture frame size
            f_width, f_height = g_pool.capture.frame_size
            # Eye camera resolutions are too small to be used as default window sizes.
            # We use double their size instead.
            frame_scale_factor = 2
            f_width *= frame_scale_factor
            f_height *= frame_scale_factor

            # Get current display scale factor
            content_scale = gl_utils.get_content_scale(main_window)
            framebuffer_scale = gl_utils.get_framebuffer_scale(main_window)
            display_scale_factor = content_scale / framebuffer_scale

            # Scale the capture frame size by display scale factor
            f_width *= display_scale_factor
            f_height *= display_scale_factor

            # Increas the width to account for the added scaled icon bar width
            f_width += icon_bar_width * display_scale_factor

            # Set the newly calculated size (scaled capture frame size + scaled icon bar width)
            glfw.set_window_size(main_window, int(f_width), int(f_height))
Example #23
0
    def draw(self, return_mat=False):
        if self.client:
            img = self.client.new_frame_image()
        
        if img and img.texture_size().width and img.texture_size().height:
            if self.first_frame_flg:
                # set window size and show option
                glfw.set_window_size(self.window, int(img.texture_size().width), int(img.texture_size().height))
                if not self.show:
                    glfw.hide_window(self.window) # hide window
                    
                # OpenGL init
                glMatrixMode(GL_PROJECTION)
                glOrtho(0, int(img.texture_size().width), int(img.texture_size().height), 0, 1, -1)
                glMatrixMode(GL_MODELVIEW)
                glLoadIdentity()
                glDisable(GL_DEPTH_TEST)
                glClearColor(0.0, 0.0, 0.0, 0.0)
                glEnable(GL_TEXTURE_RECTANGLE)
                
                self.first_frame_flg = False

                print(f"server: \"{self.server.app_name}/{self.server.name}\", window: \"{self.window_name}\", size: \"{img.texture_size().width} × {img.texture_size().height}\"")
            
            glfw.make_context_current(self.window)
            
            # Clear screen
            glClearColor(0.0, 0.0, 0.0, 0.0)
            glClear(GL_COLOR_BUFFER_BIT)
            
            #  Bind the texture
            glEnable(GL_TEXTURE_RECTANGLE)
            glActiveTexture(GL_TEXTURE0)
            glBindTexture(GL_TEXTURE_RECTANGLE, img.texture_name())
            
            #  Configure texturing as we want it
            glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_S, GL_CLAMP)
            glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_T, GL_CLAMP)
            glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
            glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
            glEnable(GL_BLEND)
            glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
            glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
            
            glColor4f(1.0, 1.0, 1.0, 1.0)
            
            texCoords = np.array([
                0.0, img.texture_size().height, 
                img.texture_size().width, img.texture_size().height, 
                img.texture_size().width, 0.0, 
                0.0, 0.0], dtype=GLfloat)

            verts = np.array([
                0.0, 0.0, 
                img.texture_size().width, 0.0, 
                img.texture_size().width, img.texture_size().height, 
                0.0, img.texture_size().height], dtype=GLfloat)
            
            glEnableClientState( GL_TEXTURE_COORD_ARRAY )
            glTexCoordPointer(2, GL_FLOAT, 0, texCoords)
            glEnableClientState(GL_VERTEX_ARRAY)
            glVertexPointer(2, GL_FLOAT, 0, verts)
            glDrawArrays( GL_TRIANGLE_FAN, 0, 4 )
            
            if return_mat:
                x_scale, y_scale = glfw.get_window_content_scale(self.window)
                frame = np.zeros((int(img.texture_size().height * x_scale), int(img.texture_size().width * y_scale), 4), np.uint8)
                glPixelStorei(GL_PACK_ALIGNMENT, 1)
                glPixelStorei(GL_PACK_ROW_LENGTH, (int(img.texture_size().width * x_scale)))
               
                glReadPixels(0, 0, int(img.texture_size().width * x_scale), int(img.texture_size().height * y_scale), GL_BGRA, GL_UNSIGNED_BYTE, frame.data)
                frame = cv2.resize(frame, (int(img.texture_size().width), int(img.texture_size().height)))
                frame = cv2.flip(frame, 0)
                return frame
            
        glfw.swap_buffers(self.window)
        glfw.poll_events()
        
        # unbind our sender texture
        glBindTexture(GL_TEXTURE_RECTANGLE, 0)
Example #24
0
 def size(self, value: Tuple[int, int]):
     glfw.set_window_size(self._window, value[0], value[1])
Example #25
0
 def _vispy_set_size(self, w, h):
     if self._id is None:
         return
     # Set size of the widget or window
     glfw.set_window_size(self._id, w, h)
Example #26
0
 def change_size(self, width, height):
     glfw.set_window_size(self.window, width, height)
Example #27
0
 def set_window_size(self, widht, hight):
     if self.minimumSize_guard(widht, hight):
         glfw.set_window_size(self.Window, widht, hight)
Example #28
0
 def _before_make_current(self, width, height):
     previous_context = glfw.get_current_context()
     glfw.make_context_current(self._context)
     if (width, height) != glfw.get_window_size(self._context):
         glfw.set_window_size(self._context, width, height)
     return previous_context
Example #29
0
 def _set_initial_window_state(self):
     glfw.set_window_pos(self._window,
                         *self._general_settings.window_position)
     glfw.set_window_size(self._window, *self._general_settings.window_size)