Esempio n. 1
0
 def __init__(self,
              text,
              color='black',
              bold=False,
              italic=False,
              face='OpenSans',
              font_size=12,
              pos=[0, 0, 0],
              rotation=0.0,
              anchor_x='center',
              anchor_y='center',
              font_manager=None):
     super(TimeTextVisual,
           self).__init__(text, color, bold, italic, face, font_size, pos,
                          rotation, anchor_x, anchor_y, font_manager)
     self.unfreeze()  # super class froze things. Need to unfreeze.
     visuals.Visual.__init__(self, vertex_shader, fragment_shader)
     self.timer = app.Timer(interval='auto',
                            connect=self.update_time,
                            start=False)
     self._time = 0.0
     self._first_time = ptime.time()
     self._last_time = ptime.time()
     self.text = text
     self.freeze()
     self.shared_program['time'] = self._time
     self.shared_program['text_scale'] = 1
     self.timer.start()
Esempio n. 2
0
 def input_event(self, ev):
     self._last_event = ev
     self._need_paint = True
     self.timer.start(self._interval)
     # paint immediately if we can
     if self._last_paint is None or self._last_paint + self._interval < ptime.time():
         self.timeout()
Esempio n. 3
0
def reset_boats():
    global BOATS, CONTROLLERS, WAYPOINT_QUEUE, WAYPOINTS_INDEX, WAYPOINTS_BEFORE_RESET, LAST_COMPLETED_WP_TIME, LAST_TIME, FIRST_TIME, TEXT_BOXES
    BOATS = {"pid": Boat.Boat(design=Designs.AirboatDesign()),
             "q": Boat.Boat(design=Designs.TankDriveDesign())}
    # generate all the random waypoints
    generate_random_waypoints_queue()
    waypoint = WAYPOINT_QUEUE[0]
    px, py = xy_location_to_pixel_location(waypoint[0], waypoint[1])
    LAST_TIME = 0
    FIRST_TIME = ptime.time()
    for k in BOATS:
        boat = BOATS[k]
        WAYPOINTS_INDEX[k] = 0
        LAST_COMPLETED_WP_TIME[k] = 0
        boat.state = np.zeros((6,))
        boat.time = 0
        boat.name = k + " boat"
        NAVIGATION_LINES[k].set_data(pos=np.array([(px, py), xy_location_to_pixel_location(boat.state[0], boat.state[1])], dtype=np.float32))
        TEXT_BOXES["waypoint_symbol"][k].pos = (px, py)
        TEXT_BOXES["waypoint_text"][k].text = "[{:.0f}, {:.0f}]".format(px, py)
        TEXT_BOXES["waypoint_count"][k].text = "#{} of {}".format(WAYPOINTS_INDEX[k] + 1, WAYPOINTS_BEFORE_RESET)
        #boat.strategy = Strategies.DestinationOnly(boat, waypoint, controller_name=CONTROLLERS[k])
        #boat.strategy = Strategies.LineFollower(boat, waypoint, controller_name=CONTROLLERS[k])
        if (k == "pid"):
            boat.strategy = Strategies.PseudoRandomBalancedHeading(boat, fixed_thrust=0.2, angle_divisions=8)
        else:
            boat.strategy = Strategies.DoNothing(boat)

        boat.sourceLocation = boat.state[0:2]
        boat.destinationLocation = waypoint
        boat.calculateQState()  # need to initialize the state for Q learning
    def __init__(self, size, num_points: int, center, color, radius):
        '''[summary]
        
        Arguments:
            size {[type]} -- size of the rendered image
            num_points {int} -- number of valid depth values
            center {[type]} -- the (x, y) coordinate of each valid depth value
            color {[type]} -- encode the 16bit depth value to (R, G) channels, corresponding to 'center'
        '''

        # We hide the canvas upon creation.
        app.Canvas.__init__(self, show=False, size=size)
        self._t0 = time()
        # Texture where we render the scene.
        self._rendertex = gloo.Texture2D(shape=size[::-1] + (4, ))
        # FBO.
        self._fbo = gloo.FrameBuffer(self._rendertex,
                                     gloo.RenderBuffer(size[::-1]))
        # Regular program that will be rendered to the FBO.

        V = np.zeros(num_points, [("center", np.float32, 2),
                                  ("color", np.float32, 3),
                                  ("radius", np.float32, 1)])
        V["center"] = center
        V["color"] = color
        V["radius"] = radius

        self.program = gloo.Program(vertex, fragment)
        self.program.bind(gloo.VertexBuffer(V))
        self.program['resolution'] = self.size
        # We manually draw the hidden canvas.
        self.update()
Esempio n. 5
0
 def input_event(self, ev):
     if ev.type == 'mouse_wheel':
         self.last_time = ptime.time()
         self.wheel_target += ev.delta[1]
         self.timeout()
         if not self.timer.running:
             self.timer.start(interval=0.015)
Esempio n. 6
0
 def input_event(self, ev):
     if ev.type == 'mouse_wheel':
         self.last_time = ptime.time()
         self.wheel_target += ev.delta[1]
         self.timeout()
         if not self.timer.running:
             self.timer.start(interval=0.015)
Esempio n. 7
0
 def timeout(self, ev=None):
     if self._need_paint:
         # todo: need to join all regions
         self._last_paint = ptime.time()
         self(self._last_event)
         self._need_paint = False
     else:
         self.timer.stop()
 def update_time(self, ev):  # argument ev is required for scene, but doesn't have to be used
     t = ptime.time()
     self._time += t - self._last_time
     self._last_time = t
     self.shared_program['time'] = self._time
     x = t - self._first_time
     self.text = "t = {%.4f}".format(x)
     self.update()
Esempio n. 9
0
 def input_event(self, ev):
     self._last_event = ev
     self._need_paint = True
     self.timer.start(self._interval)
     # paint immediately if we can
     if self._last_paint is None or self._last_paint + self._interval < ptime.time(
     ):
         self.timeout()
Esempio n. 10
0
 def timeout(self, ev=None):
     if self._need_paint:
         # todo: need to join all regions
         self._last_paint = ptime.time()
         self(self._last_event)
         self._need_paint = False
     else:
         self.timer.stop()
Esempio n. 11
0
    def __init__(self, size=(960, 540)):

        app.Canvas.__init__(self, show=False, size=size)
        self._t0 = time()

        self._rendertex = gloo.Texture2D(shape=self.size[::-1] + (4, ))
        self._fbo = gloo.FrameBuffer(self._rendertex,
                                     gloo.RenderBuffer(self.size[::-1]))
        self.program = gloo.Program(vertex, fragment)
Esempio n. 12
0
def iterate(event):  # event is unused
    global FIRST_TIME, LAST_TIME, BOATS, CANVAS, TIME_DILATION, LAST_COMPLETED_WP_TIME, FAILED_WAYPOINT_TIMEOUT, WAYPOINTS_INDEX, CONTROLLERS, WAYPOINT_QUEUE
    global TEXT_BOXES, EXPERIENCES, TOTAL_ITERATIONS, NAVIGATION_LINES, TOTAL_BATCHES
    if TOTAL_ITERATIONS < 1:
        FIRST_TIME = ptime.time()  # there is a huge gap in time as the window opens, so we need this manual time reset for the very first iteration
    TOTAL_ITERATIONS += 1
    current_time = TIME_DILATION*(ptime.time() - FIRST_TIME)
    # print "Total iterations = {}, t = {}".format(TOTAL_ITERATIONS, current_time)
    TEXT_BOXES["time"].text = "t = {}".format(format_time_string(current_time, 2))
    # USE ODE TO PROPAGATE BOAT STATE
    times = np.linspace(LAST_TIME, current_time, 100)
    for k in BOATS:
        boat = BOATS[k]
        boat.control()
        # if the boat actually changes action, we should create a Q learning experience
        # (i.e. BEFORE we change actions here, the state before ode is s' in (s, a, r, s')
        # The experience is created in boat.control() right before new actions are selected
        boat.time = current_time
        states = spi.odeint(Boat.ode, boat.state, times, (boat,))
        boat.state = states[-1]
        boat.state[4] = Boat.wrapToPi(boat.state[4])
        px, py = xy_location_to_pixel_location(states[-1][0], states[-1][1])
        heading = Boat.wrapTo2Pi(states[-1][4])
        BOAT_VISUALS[k].new_pose(px, py, heading)
        if boat.strategy.finished or current_time - LAST_COMPLETED_WP_TIME[k] > FAILED_WAYPOINT_TIMEOUT:
            WAYPOINTS_INDEX[k] += 1
            LAST_COMPLETED_WP_TIME[k] = current_time
            if WAYPOINTS_INDEX[k] < len(WAYPOINT_QUEUE):
                waypoint = WAYPOINT_QUEUE[WAYPOINTS_INDEX[k]]
                px, py = xy_location_to_pixel_location(waypoint[0], waypoint[1])
                NAVIGATION_LINES[k].set_data(pos=np.array([(px, py), xy_location_to_pixel_location(boat.state[0], boat.state[1])], dtype=np.float32))
                TEXT_BOXES["waypoint_symbol"][k].pos = (px, py+15)  # py-0.5*fontsize to center the text vertically
                TEXT_BOXES["waypoint_text"][k].text = "[{:.0f}, {:.0f}]".format(px, py)
                TEXT_BOXES["waypoint_count"][k].text = "#{} of {}".format(WAYPOINTS_INDEX[k]+1, WAYPOINTS_BEFORE_RESET)
                #boat.strategy = Strategies.DestinationOnly(boat, waypoint, controller_name=CONTROLLERS[k])
                boat.strategy = Strategies.LineFollower(boat, waypoint, controller_name=CONTROLLERS[k])
                boat.sourceLocation = boat.state[0:2]
                boat.destinationLocation = waypoint
    if not WAYPOINTS_INDEX["pid"] < WAYPOINTS_BEFORE_RESET or not WAYPOINTS_INDEX["q"] < WAYPOINTS_BEFORE_RESET:
        TOTAL_BATCHES += 1
        reset_boats()
    else:
        LAST_TIME = current_time
    CANVAS.update()
Esempio n. 13
0
 def timeout(self, ev=None):
     now = ptime.time()
     dt = now - self.last_time
     self.last_time = now
     delta = (self.wheel_target - self.wheel) * (1.0-np.exp(-dt*self.speed))
     self.wheel += delta
     self(pos=(0,0), delta=(0,delta))
     
     if abs(self.wheel-self.wheel_target) < 0.01:
         self.timer.stop()
Esempio n. 14
0
def _test_multiple_canvases(backend):
    """Helper for testing multiple canvases from the same application"""
    n_check = 3
    a = Application(backend)
    with Canvas(app=a, size=_win_size, title=backend + ' same_0') as c0:
        with Canvas(app=a, size=_win_size, title=backend + ' same_1') as c1:
            ct = [0, 0]

            @c0.events.paint.connect
            def paint0(event):
                ct[0] += 1
                c0.update()

            @c1.events.paint.connect  # noqa, analysis:ignore
            def paint1(event):
                ct[1] += 1
                c1.update()

            c0.show()  # ensure visible
            c1.show()
            c0.update()  # force first paint
            c1.update()

            timeout = time() + 2.0
            while (ct[0] < n_check or ct[1] < n_check) and time() < timeout:
                a.process_events()
            print((ct, n_check))
            assert_true(n_check <= ct[0] <= n_check + 1)
            assert_true(n_check <= ct[1] <= n_check + 1)

            # check timer
            global timer_ran
            timer_ran = False

            def on_timer(_):
                global timer_ran
                timer_ran = True
            timeout = time() + 2.0
            Timer(0.1, app=a, connect=on_timer, iterations=1, start=True)
            while not timer_ran and time() < timeout:
                a.process_events()
            assert_true(timer_ran)
Esempio n. 15
0
 def on_draw(self, event):
     # Render in the FBO.
     with self._fbo:
         gloo.clear('black')
         gloo.set_viewport(0, 0, *self.size)
         self.program.draw()
         # Retrieve the contents of the FBO texture.
         self.im = _screenshot((0, 0, self.size[0], self.size[1]))
     self._time = time() - self._t0
     # Immediately exit the application.
     app.quit()
Esempio n. 16
0
    def __init__(self,
                 field,
                 spacing=10,
                 segments=3,
                 seg_len=0.5,
                 color=(1, 1, 1, 0.3)):
        self._time = 0.0
        self._last_time = ptime.time()
        rows = int(field.shape[0] / spacing)
        cols = int(field.shape[1] / spacing)
        index = np.empty((rows * cols, int(segments) * 2, 2), dtype=np.float32)

        # encodes starting position within vector field
        index[:, :, 0] = np.arange(rows * cols)[:, np.newaxis]
        # encodes distance along length of line
        index[:, ::2, 1] = np.arange(segments)[np.newaxis, :]
        index[:, 1::2, 1] = np.arange(segments)[np.newaxis, :] + 1
        self._index = gloo.VertexBuffer(index)
        if not isinstance(color, np.ndarray):
            color = np.array([[list(color)]], dtype='float32')
        self._color = gloo.Texture2D(color)

        offset = np.random.uniform(256, size=(rows, cols, 3)).astype(np.ubyte)
        self._offset = gloo.Texture2D(offset, format='rgb')
        self._field = gloo.Texture2D(field,
                                     format='rg',
                                     internalformat='rg32f',
                                     interpolation='linear')
        self._field_shape = field.shape[:2]

        visuals.Visual.__init__(self, vcode=self.vertex, fcode=self.fragment)
        self.timer = app.Timer(interval='auto',
                               connect=self.update_time,
                               start=False)
        self.freeze()

        self.shared_program['field'] = self._field
        self.shared_program['field_shape'] = self._field.shape[:2]
        self.shared_program['shape'] = (rows, cols)
        self.shared_program['index'] = self._index
        self.shared_program['spacing'] = spacing
        self.shared_program['t'] = self._time
        self.shared_program['offset'] = self._offset
        self.shared_program['speed'] = 1
        self.shared_program['color'] = self._color
        self.shared_program['seg_len'] = seg_len
        self.shared_program['nseg'] = segments
        self.shared_program['n_iter'] = 1
        self.shared_program['attractor'] = (0, 0)
        self.shared_program['time'] = 0
        self._draw_mode = 'lines'
        self.set_gl_state('translucent', depth_test=False)

        self.timer.start()
Esempio n. 17
0
    def timeout(self, ev=None):
        now = ptime.time()
        dt = now - self.last_time
        self.last_time = now
        delta = (self.wheel_target - self.wheel) * (1.0 -
                                                    np.exp(-dt * self.speed))
        self.wheel += delta
        self(pos=(0, 0), delta=(0, delta))

        if abs(self.wheel - self.wheel_target) < 0.01:
            self.timer.stop()
Esempio n. 18
0
 def on_draw(self, event):
     with self._fbo:
         gloo.clear('black')
         self.program.bind(self.verts_buf)
         self.program.draw('triangles', self.tris_buf)
         # Retrieve depth
         # For some reason, vispy flipuds images in read_pixels, so we
         # unflip here. This is inefficient but vispy-compatible
         self.depth = read_fbo_color_rgba32f(self._fbo)
         self.depth = np.flipud(self.depth)
     self._time = time() - self._t0
     app.quit()
Esempio n. 19
0
def iterate(event):  # event is unused
    global FIRST_TIME, LAST_TIME, CANVAS, TIME_DILATION, TOTAL_ITERATIONS, CANVAS, GLOBAL_TIMER

    GLOBAL_TIMER.stop()

    if TOTAL_ITERATIONS < 1:
        FIRST_TIME = ptime.time(
        )  # there is a huge gap in time as the window opens, so we need this manual time reset for the very first iteration
    TOTAL_ITERATIONS += 1
    current_time = TOTAL_ITERATIONS * ITERATION_INTERVAL
    # print "Total iterations = {}, t = {}".format(TOTAL_ITERATIONS, current_time)
    TEXT_BOXES["time"].text = "t = {}".format(
        SimpleOscillatorVisualization.format_time_string(current_time, 2))
    times = np.linspace(LAST_TIME, current_time, 10)

    for k in OSCILLATORS:
        oscillator = OSCILLATORS[k]
        oscillator.getAction(current_time)
        states = spi.odeint(SimpleOscillator.simpleOscillatorODE,
                            oscillator.getState(), times, (oscillator, ))
        oscillator.setState(states[-1])
        OSCILLATOR_VISUALS[k].new_pose(
            ARENA_CENTER[0] + oscillator.getState()[0], ARENA_CENTER[1])
        TEXT_BOXES["pos"].text = "x = {:.2f}".format(oscillator.getState()[0])
        TEXT_BOXES["force"].text = "f = {:.2f}".format(
            oscillator.getLastAction())
        LINES["goal"].set_data(
            pos=np.array([[
                ARENA_CENTER[0] + oscillator.getGoal(), ARENA_CENTER[1] -
                ARENA_HEIGHT / 2
            ],
                          [
                              ARENA_CENTER[0] +
                              oscillator.getGoal(), ARENA_CENTER[1] +
                              ARENA_HEIGHT / 2
                          ]],
                         dtype=np.float32))
        if np.abs(oscillator.getState()[0] - oscillator.getGoal()) < 1:
            if np.abs(oscillator.getState()[1]) < 1:
                TEXT_BOXES["pos"].color = COLORS["green"]
                print("Oscillator {} reached goal state in {} seconds".format(
                    oscillator.getName(), current_time))
                GLOBAL_TIMER.disconnect(
                    iterate
                )  # stop the simulation by disconnecting this callback
                return

    LAST_TIME = current_time
    CANVAS.update()

    GLOBAL_TIMER.start()
    return
Esempio n. 20
0
 def on_draw(self, event):
     # Render in the FBO.
     with self._fbo:
         gloo.clear('black')
         gloo.set_viewport(0, 0, *self.size)
         self.program.draw()
         # Retrieve the contents of the FBO texture.
         self.im = read_pixels((0, 0, self.size[0], self.size[1]),
                               True,
                               out_type='float')
     self._time = time() - self._t0
     # Immediately exit the application.
     app.quit()
Esempio n. 21
0
    def process_frame(self, tex):
        print('process_frame w/shader')
        with self._fbo:

            self.program['u_tex1'] = gloo.Texture2D(tex,
                                                    interpolation='linear')
            self.program.bind(gloo.VertexBuffer(data))

            self.program.draw('triangle_strip')

            self.sc = _screenshot((0, 0, self.size[0], self.size[1]),
                                  alpha=False)

        self._time = time() - self._t0
Esempio n. 22
0
File: glut.py Progetto: joe311/vispy
    def _idle_callback(cls):
        now = ptime.time()
        new_schedule = []

        ## see whether there are any timers ready
        while len(cls._schedule) > 0 and cls._schedule[0][0] <= now:
            timer = cls._schedule.pop(0)[1]
            timer._vispy_timer._timeout()
            if timer._vispy_timer.running:
                new_schedule.append((now + timer._vispy_timer.interval, timer))

        ## schedule next round of timeouts
        if len(new_schedule) > 0:
            cls._schedule.extend(new_schedule)
            cls._schedule.sort()
Esempio n. 23
0
    def _idle_callback(cls):
        now = ptime.time()
        new_schedule = []

        ## see whether there are any timers ready
        while len(cls._schedule) > 0 and cls._schedule[0][0] <= now:
            timer = cls._schedule.pop(0)[1]
            timer._vispy_timer._timeout()
            if timer._vispy_timer.running:
                new_schedule.append((now + timer._vispy_timer.interval, timer))

        ## schedule next round of timeouts
        if len(new_schedule) > 0:
            cls._schedule.extend(new_schedule)
            cls._schedule.sort()
Esempio n. 24
0
 def __init__(self, field, spacing=10, segments=3, seg_len=0.5,
              color=(1, 1, 1, 0.3)):
     self._time = 0.0
     self._last_time = ptime.time()
     rows = field.shape[0] / spacing
     cols = field.shape[1] / spacing
     index = np.empty((rows * cols, segments * 2, 2), dtype=np.float32)
     
     # encodes starting position within vector field
     index[:, :, 0] = np.arange(rows * cols)[:, np.newaxis]
     # encodes distance along length of line
     index[:, ::2, 1] = np.arange(segments)[np.newaxis, :]
     index[:, 1::2, 1] = np.arange(segments)[np.newaxis, :] + 1
     self._index = gloo.VertexBuffer(index)
     if not isinstance(color, np.ndarray):
         color = np.array([[list(color)]], dtype='float32')
     self._color = gloo.Texture2D(color)
     
     offset = np.random.uniform(256, size=(rows, cols, 3)).astype(np.ubyte)
     self._offset = gloo.Texture2D(offset, format='rgb')
     self._field = gloo.Texture2D(field, format='rg',
                                  internalformat='rg32f',
                                  interpolation='linear')
     self._field_shape = field.shape[:2]
     
     visuals.Visual.__init__(self, vcode=self.vertex, fcode=self.fragment)
     self.timer = app.Timer(interval='auto', connect=self.update_time,
                            start=False)
     self.freeze()
     
     self.shared_program['field'] = self._field
     self.shared_program['field_shape'] = self._field.shape[:2]
     self.shared_program['shape'] = (rows, cols)
     self.shared_program['index'] = self._index
     self.shared_program['spacing'] = spacing
     self.shared_program['t'] = self._time
     self.shared_program['offset'] = self._offset
     self.shared_program['speed'] = 1
     self.shared_program['color'] = self._color
     self.shared_program['seg_len'] = seg_len
     self.shared_program['nseg'] = segments
     self.shared_program['n_iter'] = 1
     self.shared_program['attractor'] = (0, 0)
     self.shared_program['time'] = 0
     self._draw_mode = 'lines'
     self.set_gl_state('translucent', depth_test=False)
     
     self.timer.start()
Esempio n. 25
0
 def __init__(self, mesh, color, size):
     # We hide the canvas upon creation.
     app.Canvas.__init__(self, show=False, size=size)
     self._t0 = time()
     # Texture where we render the scene.
     self._rendertex = gloo.Texture2D(shape=self.size[::-1] + (4, ),
                                      internalformat='rgba32f')
     # FBO.
     self._fbo = gloo.FrameBuffer(self._rendertex,
                                  gloo.RenderBuffer(self.size[::-1]))
     # Regular program that will be rendered to the FBO.
     self.program = gloo.Program(vertex_shader, fragment_shader)
     self.program["position"] = mesh
     self.program['color'] = color
     # We manually draw the hidden canvas.
     self.update()
Esempio n. 26
0
 def __init__(self, size=(600, 600)):
     # We hide the canvas upon creation.
     app.Canvas.__init__(self, show=False, size=size)
     self._t0 = time()
     # Texture where we render the scene.
     self._rendertex = gloo.Texture2D(self.size)
     # FBO.
     self._fbo = gloo.FrameBuffer(self._rendertex, gloo.RenderBuffer(self.size))
     # Regular program that will be rendered to the FBO.
     self.program = gloo.Program(vertex, fragment)
     self.program["position"] = [(-1, -1), (-1, 1), (1, 1), (-1, -1), (1, 1), (1, -1)]
     self.program["scale"] = 3
     self.program["center"] = [-0.5, 0]
     self.program["iter"] = 300
     self.program["resolution"] = self.size
     # We manually draw the hidden canvas.
     self.update()
Esempio n. 27
0
 def on_paint(self, ev):
     glClear(GL_COLOR_BUFFER_BIT)
     for visual in self.visuals:
         visual.draw()
     self.swap_buffers()
     
     now = ptime.time()
     if self.last_draw is None:
         self.last_draw = now
     else:
         dt = now - self.last_draw
         self.last_draw = now
         self.fps = 0.9*self.fps + 0.1/dt
         if self.fps_iter > 100:
             self.fps_iter = 0
             print('FPS: %0.2f' % self.fps)
         self.fps_iter += 1
Esempio n. 28
0
    def on_paint(self, ev):
        glClear(GL_COLOR_BUFFER_BIT)
        for visual in self.visuals:
            visual.draw()
        self.swap_buffers()

        now = ptime.time()
        if self.last_draw is None:
            self.last_draw = now
        else:
            dt = now - self.last_draw
            self.last_draw = now
            self.fps = 0.9 * self.fps + 0.1 / dt
            if self.fps_iter > 100:
                self.fps_iter = 0
                print('FPS: %0.2f' % self.fps)
            self.fps_iter += 1
Esempio n. 29
0
 def __init__(self, size=(600, 600)):
     # We hide the canvas upon creation.
     app.Canvas.__init__(self, show=False, size=size)
     self._t0 = time()
     # Texture where we render the scene.
     self._rendertex = gloo.Texture2D(shape=self.size + (4,))
     # FBO.
     self._fbo = gloo.FrameBuffer(self._rendertex,
                                  gloo.RenderBuffer(self.size))
     # Regular program that will be rendered to the FBO.
     self.program = gloo.Program(vertex, fragment)
     self.program["position"] = [(-1, -1), (-1, 1), (1, 1),
                                 (-1, -1), (1, 1), (1, -1)]
     self.program["scale"] = 3
     self.program["center"] = [-0.5, 0]
     self.program["iter"] = 300
     self.program['resolution'] = self.size
     # We manually draw the hidden canvas.
     self.update()
Esempio n. 30
0
    def _scroll_camera(self, ev):
        now = ptime.time()
        dt = now - self._last_camera_update
        self._last_camera_update = now

        cr = vispy.geometry.Rect(self.view.camera.rect)
        tr = self.camera_target

        crv = np.array(cr.pos + cr.size, dtype='float32')
        trv = np.array(tr.pos + tr.size, dtype='float32')

        if not np.any(abs(trv - crv) > 1e-2):
            return

        s = np.exp(-dt / 0.4)  # 400 ms settling time constant
        nrv = crv * s + trv * (1.0 - s)

        cr.pos = nrv[:2]
        cr.size = nrv[2:]
        self.view.camera.rect = cr
Esempio n. 31
0
    def on_paint(self, ev):
        glClear(GL_COLOR_BUFFER_BIT)
        x, y, width, height = self.geometry
        transform = np.array([[self.zoom, 0, 0, 0], [0, self.zoom, 0, 0],
                              [0, 0, 1, 0], [self.pan[0], self.pan[1], 0, 1]],
                             dtype=np.float32)
        for visual in self.visuals:
            visual.draw(transform)
        self.swap_buffers()

        now = ptime.time()
        if self.last_draw is None:
            self.last_draw = now
        else:
            dt = now - self.last_draw
            self.last_draw = now
            self.fps = 0.9 * self.fps + 0.1 / dt
            if self.fps_iter > 100:
                self.fps_iter = 0
                print('FPS: %0.2f' % self.fps)
            self.fps_iter += 1
Esempio n. 32
0
 def on_paint(self, ev):
     glClear(GL_COLOR_BUFFER_BIT)
     width,height = self.size
     transform = np.array([
         [self.zoom,0,0,0], 
         [0,self.zoom,0,0], 
         [0,0,1,0], 
         [self.pan[0],self.pan[1],0,1]
         ], dtype=np.float32)
     for visual in self.visuals:
         visual.draw(transform)
     #self.swap_buffers()
     
     now = ptime.time()
     if self.last_draw is None:
         self.last_draw = now
     else:
         dt = now - self.last_draw
         self.last_draw = now
         self.fps = 0.9*self.fps + 0.1/dt
         if self.fps_iter > 100:
             self.fps_iter = 0
             print('FPS: %0.2f' % self.fps)
         self.fps_iter += 1
Esempio n. 33
0
File: glut.py Progetto: joe311/vispy
 def _vispy_start(self, interval):
     now = ptime.time()
     self._schedule.append((now + interval, self))
Esempio n. 34
0
    def __init__(self, verts, tris, K, R, C, size, verbose=False):
        # We hide the canvas upon creation.
        app.Canvas.__init__(self, show=False, size=size)
        self._t0 = time()
        # Create FBO to render the scene
        self._depthbuf = gloo.RenderBuffer(shape=size[::-1], format='depth')
        self._coltex = gloo.Texture2D(
                shape=(size[1], size[0], 4),
                format=GL.GL_RGBA,
                internalformat=GL.GL_RGBA32F)

        self._fbo = gloo.FrameBuffer(self._coltex, self._depthbuf)
        self.program = gloo.Program(DEPTH_VERTEX, DEPTH_FRAG)

        gloo.set_viewport(0, 0, self.physical_size[0], self.physical_size[1])

        if True:
            # Autocompute near and far planes to maximize precision
            verts_cam = R.dot(verts[:, :3].T - C.reshape(-1, 1)).T
            depths = verts_cam[:,2]
            self.near = 10 ** np.floor(np.log10(depths.min()))
            self.far = 10 ** np.ceil(np.log10(depths.max()))
            if verbose:
                print "depths ", depths.min(), depths.max()
                print 'autonear|far : ', self.near, self.far
        else:
            self.near = 0.01
            self.far = 100.0

        # IMPORTANT: Opengl uses column-major matrices. Numpy uses row-major
        # by default. vispy works with transposed matrices so they can
        # directly be send to opengl. So the translate() from vispy acts like
        # this :
        #   In [81]: vispy.util.transforms.translate((1, 2, 3))
        #   Out[81]:
        #   array([[ 1.,  0.,  0.,  0.],
        #          [ 0.,  1.,  0.,  0.],
        #          [ 0.,  0.,  1.,  0.],
        #          [ 1.,  2.,  3.,  1.]])
        #
        self.projection = gl_proj_matrix_from_K(size[0], size[1], K,
                                                self.near, self.far)
        self.model = np.eye(4, dtype=np.float32)

        self.view = np.eye(4, dtype=np.float32)
        self.view[:3,:3] = R
        self.view[:3, 3] = -R.dot(C)

        # Our camera has z pointing towards the target while opengl has
        # z pointing outside of the screen => negate
        SHAPY_TO_OGL = np.array([
            [ 1,  0,  0,  0],
            [ 0,  1,  0,  0],
            [ 0,  0, -1,  0],
            [ 0,  0,  0,  1]], dtype=np.float32)
        self.view = SHAPY_TO_OGL.dot(self.view)

        # transpose to make them opengl row-major compatible (that's what
        # vispy does)
        self.program['u_projection'] = self.projection.T.copy()
        self.program['u_model'] = self.model.T.copy()
        self.program['u_view'] = self.view.T.copy()
        if verbose:
            print 'model\n', self.model
            print 'proj\n', self.projection
            print 'view matrix\n', self.view

        gloo.set_clear_color('black')
        gloo.set_state('opaque')

        # Create
        nverts = verts.shape[0]
        vtype = [('a_position', np.float32, 3),
                 ('a_color', np.float32, 3)]
        verts_colors = np.ones((nverts, 3), dtype=np.float32)
        # TODO: Can apply a colormap to body parts to show them
        #verts_colors = verts[:,3].astype(np.float32)
        # V should contain [(v[0], c[0]), (v[1], c[1])] where v and c are
        # 3-vector containing the position, respectively color of each vertex
        V = [(verts[i, :3], verts_colors[i]) for i in xrange(nverts)]
        V = np.array(V, dtype=vtype)
        I = np.array(tris, dtype=np.uint16).ravel()
        self.tris_buf = gloo.IndexBuffer(I)
        self.verts_buf = gloo.VertexBuffer(V)

        # Manually force update
        self.update()
Esempio n. 35
0
 def update_time(self, ev):
     t = ptime.time()
     self._time += t - self._last_time
     self._last_time = t
     self.shared_program['time'] = self._time
     self.update()
Esempio n. 36
0
def test_multiple_canvases():
    """Testing multiple canvases"""
    n_check = 3
    app = use_app()
    with Canvas(app=app, size=_win_size, title='same_0') as c0:
        with Canvas(app=app, size=_win_size, title='same_1') as c1:
            ct = [0, 0]

            @c0.events.draw.connect
            def draw0(event):
                ct[0] += 1
                c0.update()

            @c1.events.draw.connect  # noqa, analysis:ignore
            def draw1(event):
                ct[1] += 1
                c1.update()

            c0.show()  # ensure visible
            c1.show()
            c0.update()  # force first draw
            c1.update()

            timeout = time() + 2.0
            while (ct[0] < n_check or ct[1] < n_check) and time() < timeout:
                app.process_events()
            print((ct, n_check))
            assert n_check <= ct[0] <= n_check + 2  # be a bit lenient
            assert n_check <= ct[1] <= n_check + 2

            # check timer
            global timer_ran
            timer_ran = False

            def on_timer(_):
                global timer_ran
                timer_ran = True
            t = Timer(0.1, app=app, connect=on_timer, iterations=1,  # noqa
                      start=True)
            app.process_events()
            sleep(0.5)  # long for slow systems
            app.process_events()
            app.process_events()
            assert timer_ran

    if app.backend_name.lower() == 'wx':
        raise SkipTest('wx fails test #2')  # XXX TODO Fix this

    kwargs = dict(app=app, autoswap=False, size=_win_size,
                  show=True)
    with Canvas(title='0', **kwargs) as c0:
        with Canvas(title='1', **kwargs) as c1:
            bgcolors = [None] * 2

            @c0.events.draw.connect
            def draw00(event):
                print('  {0:7}: {1}'.format('0', bgcolors[0]))
                if bgcolors[0] is not None:
                    gl.glViewport(0, 0, *list(_win_size))
                    gl.glClearColor(*bgcolors[0])
                    gl.glClear(gl.GL_COLOR_BUFFER_BIT)
                    gl.glFinish()

            @c1.events.draw.connect
            def draw11(event):
                print('  {0:7}: {1}'.format('1', bgcolors[1]))
                if bgcolors[1] is not None:
                    gl.glViewport(0, 0, *list(_win_size))
                    gl.glClearColor(*bgcolors[1])
                    gl.glClear(gl.GL_COLOR_BUFFER_BIT)
                    gl.glFinish()

            for ci, canvas in enumerate((c0, c1)):
                print('draw %s' % canvas.title)
                bgcolors[ci] = [0.5, 0.5, 0.5, 1.0]
                _update_process_check(canvas, 127)

            for ci, canvas in enumerate((c0, c1)):
                print('test')
                _update_process_check(canvas, 127, draw=False)
                bgcolors[ci] = [1., 1., 1., 1.]
                _update_process_check(canvas, 255)
                bgcolors[ci] = [0.25, 0.25, 0.25, 0.25]
                _update_process_check(canvas, 64)
Esempio n. 37
0
 def update_time(self, ev):
     t = ptime.time()
     self._time += t - self._last_time
     self._last_time = t
     self.shared_program['time'] = self._time
     self.update()
Esempio n. 38
0
    def __init__(self, canvas):
        self.debug_line_of_sight = False
        self.debug_los_tex = False
        self.canvas = canvas

        # Setup input event handling
        self.input_dispatcher = InputDispatcher(canvas)
        self.default_input_handler = DefaultInputHandler(self)
        self.input_dispatcher.add_handler(self.default_input_handler)
        self.command_mode = False

        # setup UI
        self.view = canvas.central_widget.add_view()
        self.view.camera = 'panzoom'
        self.view.camera.rect = [0, -5, 120, 60]
        self.view.camera.aspect = 0.6

        self.camera_target = self.view.camera.rect
        self._last_camera_update = ptime.time()
        self.scroll_timer = vispy.app.Timer(start=True,
                                            connect=self._scroll_camera,
                                            interval=0.016)

        # generate a texture for each character we need
        self.atlas = CharAtlas()

        # create sprites visual
        self.txt = Sprites(self.atlas,
                           sprite_size=(1, 1),
                           point_cs='visual',
                           parent=self.view.scene)

        # create maze
        self.maze = Maze.load_image('level1.png')

        # add sprites for drawing maze
        self.maze.add_sprites(self.atlas, self.txt)

        # line-of-sight computation
        opacity = self.maze.opacity.astype('float32')
        tr = self.txt.transforms.get_transform('framebuffer', 'visual')

        ms = self.maze.shape
        self.supersample = 4
        self.texture_shape = (ms[0] * self.supersample,
                              ms[1] * self.supersample, 3)

        self.shadow_renderer = ShadowRenderer(self,
                                              opacity,
                                              supersample=self.supersample)
        self.norm_light = None

        self.light_cache = ArraySumCache()

        self.memory = np.zeros(self.texture_shape, dtype='float32')
        self.sight = np.zeros(self.texture_shape, dtype='float32')

        # filters scene for lighting, line of sight, and memory
        self.sight_texture = vispy.gloo.Texture2D(shape=self.texture_shape,
                                                  format='rgb',
                                                  interpolation='linear',
                                                  wrapping='repeat')
        self.sight_filter = TextureMaskFilter(self.sight_texture,
                                              tr,
                                              scale=(1. / ms[1], 1. / ms[0]))
        self.txt.attach(self.sight_filter)

        # track all items
        self.items = []

        # track monsters by location
        self.monsters = {}

        # add player
        self.player = Player(self)

        self._need_los_update = True

        self.move_player([7, 7])

        self.console_grid = self.canvas.central_widget.add_grid()

        self.stats_box = Console((2, 160))
        self.console_grid.add_widget(self.stats_box.view, 1, 0, 1, 2)
        self.stats_box.write(
            "HP:17/33   Food:56%  Water:34%  Sleep:65%   Weight:207(45)    Level:3  Int:12  Str:9  Wis:11  Cha:2"
        )
        self.stats_box.view.height_max = 30
        self.stats_box.view.stretch = (1, 10)

        self.info_box = Console((15, 80))
        self.console_grid.add_widget(self.info_box.view, 2, 0)
        self.info_box.write("There is a scroll of infinite recursion here.")
        self.info_box.view.height_max = 200
        self.stats_box.view.stretch = (1, 1)

        self.console = Console((15, 80))
        self.console_grid.add_widget(self.console.view, 2, 1)
        self.console.view.stretch = (1, 10)
        #self.console.view.parent = self.canvas.scene
        self.console.view.rect = vispy.geometry.Rect(30, 620, 1350, 250)
        self.console.transform = vispy.visuals.transforms.STTransform(
            (0, 0, -0.5))
        #self.console.view.camera.aspect = 0.6

        self.console.view.height_max = 200

        self.console.write('Hello?')
        self.console.write('Is anybody\n    there?')
        self.console.write(''.join([chr(i) for i in range(0x20, 128)]))
        #self.console.view.camera.rect = [-1, -1, 30, 3]

        self.command = CommandInterpreter(self)
        self.cmd_input_handler = CommandInputHandler(self.console,
                                                     self.command)

        self.canvas.events.draw.connect(self.on_draw)
Esempio n. 39
0
def test_multiple_canvases():
    """Testing multiple canvases"""
    n_check = 3
    app = use_app()
    if app.backend_name.lower() == "glut":
        raise SkipTest("glut cannot use multiple canvases")
    with Canvas(app=app, size=_win_size, title="same_0") as c0:
        with Canvas(app=app, size=_win_size, title="same_1") as c1:
            ct = [0, 0]

            @c0.events.draw.connect
            def draw0(event):
                ct[0] += 1
                c0.update()

            @c1.events.draw.connect  # noqa, analysis:ignore
            def draw1(event):
                ct[1] += 1
                c1.update()

            c0.show()  # ensure visible
            c1.show()
            c0.update()  # force first draw
            c1.update()

            timeout = time() + 2.0
            while (ct[0] < n_check or ct[1] < n_check) and time() < timeout:
                app.process_events()
            print((ct, n_check))
            assert_true(n_check <= ct[0] <= n_check + 1)
            assert_true(n_check <= ct[1] <= n_check + 1)

            # check timer
            global timer_ran
            timer_ran = False

            def on_timer(_):
                global timer_ran
                timer_ran = True

            timeout = time() + 2.0
            Timer(0.1, app=app, connect=on_timer, iterations=1, start=True)
            while not timer_ran and time() < timeout:
                app.process_events()
            assert_true(timer_ran)

    if app.backend_name.lower() == "wx":
        raise SkipTest("wx fails test #2")  # XXX TODO Fix this

    kwargs = dict(app=app, autoswap=False, size=_win_size, show=True)
    with Canvas(title="0", **kwargs) as c0:
        with Canvas(title="1", **kwargs) as c1:
            bgcolors = [None] * 2

            @c0.events.draw.connect
            def draw00(event):
                print("  {0:7}: {1}".format("0", bgcolors[0]))
                if bgcolors[0] is not None:
                    gl.glViewport(0, 0, *list(_win_size))
                    gl.glClearColor(*bgcolors[0])
                    gl.glClear(gl.GL_COLOR_BUFFER_BIT)
                    gl.glFinish()

            @c1.events.draw.connect
            def draw11(event):
                print("  {0:7}: {1}".format("1", bgcolors[1]))
                if bgcolors[1] is not None:
                    gl.glViewport(0, 0, *list(_win_size))
                    gl.glClearColor(*bgcolors[1])
                    gl.glClear(gl.GL_COLOR_BUFFER_BIT)
                    gl.glFinish()

            for ci, canvas in enumerate((c0, c1)):
                print("draw %s" % canvas.title)
                bgcolors[ci] = [0.5, 0.5, 0.5, 1.0]
                _update_process_check(canvas, 127)

            for ci, canvas in enumerate((c0, c1)):
                print("test")
                _update_process_check(canvas, 127, draw=False)
                bgcolors[ci] = [1.0, 1.0, 1.0, 1.0]
                _update_process_check(canvas, 255)
                bgcolors[ci] = [0.25, 0.25, 0.25, 0.25]
                _update_process_check(canvas, 64)
Esempio n. 40
0
 def _vispy_start(self, interval):
     now = ptime.time()
     self._schedule.append((now + interval, self))