Exemple #1
0
 def draw_line(self, v, color):
     o = self._p._origin
     pgl.glBegin(pgl.GL_LINES)
     pgl.glColor3f(*color)
     pgl.glVertex3f(v[0][0] + o[0], v[0][1] + o[1], v[0][2] + o[2])
     pgl.glVertex3f(v[1][0] + o[0], v[1][1] + o[1], v[1][2] + o[2])
     pgl.glEnd()
 def render1(self):
     if   len(self.v) == 4 : gl.glBegin(gl.GL_QUADS)
     elif len(self.v)  > 4 : gl.glBegin(gl.GL_POLYGON)
     else: gl.glBegin(gl.GL_TRIANGLES)
     for p in self.v:
         gl.glVertex3f(p[0], p[1],0)  # draw each vertex
     gl.glEnd()
Exemple #3
0
    def draw(self, scale, pos):
        LINE_COLOUR = (255, 255, 255)

        # gl.glEnable(gl.GL_DEPTH_TEST);
        gl.glEnable(gl.GL_BLEND)
        gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
        gl.glEnable(gl.GL_LINE_SMOOTH)
        gl.glHint(gl.GL_LINE_SMOOTH_HINT, gl.GL_DONT_CARE)

        gl.glBegin(gl.GL_LINES)
        for link in self.links:
            if link.highlight:
                gl.glColor3ub(*self.colour)
                gl.glColor3ub(*self.colour)
            else:
                gl.glColor3ub(*LINE_COLOUR)
                gl.glColor3ub(*LINE_COLOUR)
            if link.highlight:
                depth = 0.5
            else:
                depth = 0.5
            gl.glVertex3f(*util.add_tup(pos, util.scale_tup(link.points[0].pos, scale)) + (depth,))
            gl.glVertex3f(*util.add_tup(pos, util.scale_tup(link.points[1].pos, scale)) + (depth,))
            print util.add_tup(pos, util.scale_tup(link.points[0].pos, scale))
        gl.glEnd()
Exemple #4
0
 def f():
     for u in range(1, len(self.u_set)):
         pgl.glBegin(pgl.GL_QUAD_STRIP)
         for v in range(len(self.v_set)):
             pa = self.verts[u - 1][v]
             pb = self.verts[u][v]
             if pa is None or pb is None:
                 pgl.glEnd()
                 pgl.glBegin(pgl.GL_QUAD_STRIP)
                 continue
             if use_cverts:
                 ca = self.cverts[u - 1][v]
                 cb = self.cverts[u][v]
                 if ca is None:
                     ca = (0, 0, 0)
                 if cb is None:
                     cb = (0, 0, 0)
             else:
                 if use_solid_color:
                     ca = cb = self.default_solid_color
                 else:
                     ca = cb = self.default_wireframe_color
             pgl.glColor3f(*ca)
             pgl.glVertex3f(*pa)
             pgl.glColor3f(*cb)
             pgl.glVertex3f(*pb)
         pgl.glEnd()
Exemple #5
0
def bounce(thing, other, vector=None):
    screen = thing.screen

    velocity_perpendicular = thing.vel.proj(vector)
    velocity_parallel = thing.vel - velocity_perpendicular

    if vector * thing.vel > 0:
        thing.vel = (
            screen.constants["friction"] * velocity_parallel + screen.constants["elasticity"] * velocity_perpendicular
        )
    else:
        thing.vel = (
            screen.constants["friction"] * velocity_parallel - screen.constants["elasticity"] * velocity_perpendicular
        )

    thing.position_component.position += screen.constants["displace"] * vector

    if other.immobile:
        thing.position_component.position += vector

    if screen.draw_debug:
        opengl.glBegin(opengl.GL_LINES)
        p = thing.position_component.position
        opengl.glVertex3f(p.x, p.y, 11)
        opengl.glVertex3f(p.x + 5 * vector.x, p.y + 5 * vector.y, 11)
        opengl.glEnd()
                def draw_arrow(p1, p2):
                    """
                    Draw a single vector.
                    """
                    glColor3f(0.4, 0.4, 0.9)
                    glVertex3f(*p1)

                    glColor3f(0.9, 0.4, 0.4)
                    glVertex3f(*p2)
Exemple #7
0
	def renderCatalog(cat, far):
		gl.glDisable(gl.GL_LIGHTING)
		for s in cat:
			c = s.getRgb()
			gl.glPointSize(s.getSize())
			gl.glBegin(gl.GL_POINTS)
			gl.glColor3f(c[0], c[1], c[2])
			gl.glVertex3f(far * cos(s.ra_rad) * cos(s.dec_rad), far * sin(s.ra_rad) * cos(s.dec_rad), far * sin(s.dec_rad))
			gl.glEnd()
		gl.glEnable(gl.GL_LIGHTING)
Exemple #8
0
def draw_lines(point_list, color, border_width=1):
    """
    Draw a set of lines.

    Draw a line between each pair of points specified.

    Args:
        :point_list: List of points making up the lines. Each point is
         in a list. So it is a list of lists.
        :color: color, specified in a list of 3 or 4 bytes in RGB or
         RGBA format.
        :border_width: Width of the line in pixels.
    Returns:
        None
    Raises:
        None

    Example:

    >>> import arcade
    >>> arcade.open_window("Drawing Example", 800, 600)
    >>> arcade.set_background_color(arcade.color.WHITE)
    >>> arcade.start_render()
    >>> point_list = ((390, 450), \
(450, 450), \
(390, 480), \
(450, 480), \
(390, 510), \
(450, 510))
    >>> arcade.draw_lines(point_list, arcade.color.BLUE, 3)
    >>> arcade.finish_render()
    >>> arcade.quick_run(0.25)
    """
    GL.glEnable(GL.GL_BLEND)
    GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA)
    GL.glEnable(GL.GL_LINE_SMOOTH)
    GL.glHint(GL.GL_LINE_SMOOTH_HINT, GL.GL_NICEST)
    GL.glHint(GL.GL_POLYGON_SMOOTH_HINT, GL.GL_NICEST)

    GL.glLoadIdentity()

    # Set line width
    GL.glLineWidth(border_width)

    # Set color
    if len(color) == 4:
        GL.glColor4ub(color[0], color[1], color[2], color[3])
    elif len(color) == 3:
        GL.glColor4ub(color[0], color[1], color[2], 255)

    GL.glBegin(GL.GL_LINES)
    for point in point_list:
        GL.glVertex3f(point[0], point[1], 0.5)
    GL.glEnd()
Exemple #9
0
 def draw_callback(self, nx, ny, z):
     gl.glLineWidth(self.BOLT_WIDTH)
     gl.glBegin(gl.GL_LINES)
     gl.glColor4f(self.COLOR[0], self.COLOR[1], self.COLOR[2], 1)
     gl.glVertex3f(self.x, self.y, z)
     gl.glColor4f(self.COLOR[0] * 0.7, self.COLOR[1] * 0.7,
                  self.COLOR[2] * 0.7, 0)
     gl.glVertex3f(self.x + nx * self.BOLT_LENGTH,
                   self.y + ny * self.BOLT_LENGTH, z)
     gl.glColor3f(1, 1, 1)
     gl.glEnd()
def draw_canvas():
    # Draw full-screen canvas
    gl.glDrawBuffer(gl.GL_COLOR_ATTACHMENT0_EXT)
    gl.glBegin(gl.GL_QUADS)
    for coords in [(-1.0, -1.0),
                   (1.0, -1.0),
                   (1.0, 1.0),
                   (-1.0,  1.0)]:
        gl.glVertex3f(coords[0], coords[1], 0.0)

    gl.glEnd()
Exemple #11
0
    def draw(self):
        glPushMatrix()

        glBegin(GL_LINES)
        glColor4f(*self.color)
        for di in [-1, 1]:
            for dj in [-1, 1]:
                glVertex3f(0, 0, 0)
                glVertex3f(self.scale * di, self.scale * dj, self.height)
        glEnd()

        glPopMatrix()
Exemple #12
0
    def draw(self):
        p = adjust_for_cam(self._body.position)

        gl.glEnable(pyglet.gl.GL_TEXTURE_2D)
        gl.glBindTexture(gl.GL_TEXTURE_2D, BLASTER_IMAGE.id)
        gl.glEnable(gl.GL_POINT_SPRITE)
        gl.glTexEnvi(gl.GL_POINT_SPRITE, gl.GL_COORD_REPLACE, gl.GL_TRUE)
        gl.glPointSize(4 * SPACE.scale)

        gl.glBegin(gl.GL_POINTS)
        # TODO: more optimized to draw as one large batch
        gl.glVertex3f(p.x, p.y, 0)
        gl.glEnd();
Exemple #13
0
def draw_line(start_x, start_y, end_x, end_y, color, border_width=1):

    """
    Draw a line.

    Args:
        :start_x: x position of line starting point.
        :start_y: y position of line starting point.
        :end_x: x position of line ending point.
        :end_y: y position of line ending point.
        :color: color, specified in a list of 3 or 4 bytes in RGB or
         RGBA format.
        :border_width: Width of the line in pixels.
    Returns:
        None
    Raises:
        None

    Example:

    >>> import arcade
    >>> arcade.open_window("Drawing Example", 800, 600)
    >>> arcade.set_background_color(arcade.color.WHITE)
    >>> arcade.start_render()
    >>> arcade.draw_line(270, 495, 300, 450, arcade.color.WOOD_BROWN, 3)
    >>> color = (127, 0, 127, 127)
    >>> arcade.draw_line(280, 495, 320, 450, color, 3)
    >>> arcade.finish_render()
    >>> arcade.quick_run(0.25)
    """
    GL.glEnable(GL.GL_BLEND)
    GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA)
    GL.glEnable(GL.GL_LINE_SMOOTH)
    GL.glHint(GL.GL_LINE_SMOOTH_HINT, GL.GL_NICEST)
    GL.glHint(GL.GL_POLYGON_SMOOTH_HINT, GL.GL_NICEST)

    GL.glLoadIdentity()

    # Set line width
    GL.glLineWidth(border_width)

    # Set color
    if len(color) == 4:
        GL.glColor4ub(color[0], color[1], color[2], color[3])
    elif len(color) == 3:
        GL.glColor4ub(color[0], color[1], color[2], 255)

    GL.glBegin(GL.GL_LINES)
    GL.glVertex3f(start_x, start_y, 0.5)
    GL.glVertex3f(end_x, end_y, 0.5)
    GL.glEnd()
Exemple #14
0
 def render_indicators(self, W, H):
     gl.glBegin(gl.GL_QUADS)
     s = W/40.0
     h = H/40.0
     gl.glColor4f(0,0,0,1)
     gl.glVertex3f(W, 0, 0)
     gl.glVertex3f(W, 5*h, 0)
     gl.glVertex3f(0, 5*h, 0)
     gl.glVertex3f(0, 0, 0)
     def vertical_ind(place, val, color):
         gl.glColor4f(color[0], color[1], color[2], 1)
         gl.glVertex3f((place+0)*s, h + h*val, 0)
         gl.glVertex3f((place+1)*s, h + h*val, 0)
         gl.glVertex3f((place+1)*s, h, 0)
         gl.glVertex3f((place+0)*s, h, 0)
     def horiz_ind(place, val, color):
         gl.glColor4f(color[0], color[1], color[2], 1)
         gl.glVertex3f((place+0)*s, 4*h , 0)
         gl.glVertex3f((place+val)*s, 4*h, 0)
         gl.glVertex3f((place+val)*s, 2*h, 0)
         gl.glVertex3f((place+0)*s, 2*h, 0)
     true_speed = np.sqrt(np.square(self.car.hull.linearVelocity[0]) + np.square(self.car.hull.linearVelocity[1]))
     vertical_ind(5, 0.02*true_speed, (1,1,1))
     vertical_ind(7, 0.01*self.car.wheels[0].omega, (0.0,0,1)) # ABS sensors
     vertical_ind(8, 0.01*self.car.wheels[1].omega, (0.0,0,1))
     vertical_ind(9, 0.01*self.car.wheels[2].omega, (0.2,0,1))
     vertical_ind(10,0.01*self.car.wheels[3].omega, (0.2,0,1))
     horiz_ind(20, -10.0*self.car.wheels[0].joint.angle, (0,1,0))
     horiz_ind(30, -0.8*self.car.hull.angularVelocity, (1,0,0))
     gl.glEnd()
     self.score_label.text = "%04i" % self.reward
     self.score_label.draw()
Exemple #15
0
def draw_polygon_outline(point_list, color, border_width=1):
    """
    Draw a polygon outline. Also known as a "line loop."

    Args:
        :point_list: List of points making up the lines. Each point is
         in a list. So it is a list of lists.
        :color: color, specified in a list of 3 or 4 bytes in RGB or
         RGBA format.
        :border_width: Width of the line in pixels.
    Returns:
        None
    Raises:
        None

    >>> import arcade
    >>> arcade.open_window("Drawing Example", 800, 600)
    >>> arcade.set_background_color(arcade.color.WHITE)
    >>> arcade.start_render()
    >>> point_list = ((30, 240), \
(45, 240), \
(60, 255), \
(60, 285), \
(45, 300), \
(30, 300))
    >>> arcade.draw_polygon_outline(point_list, arcade.color.SPANISH_VIOLET, 3)
    >>> arcade.finish_render()
    >>> arcade.quick_run(0.25)
    """
    GL.glEnable(GL.GL_BLEND)
    GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA)
    GL.glEnable(GL.GL_LINE_SMOOTH)
    GL.glHint(GL.GL_LINE_SMOOTH_HINT, GL.GL_NICEST)
    GL.glHint(GL.GL_POLYGON_SMOOTH_HINT, GL.GL_NICEST)

    # Set line width
    GL.glLineWidth(border_width)

    GL.glLoadIdentity()

    # Set color
    if len(color) == 4:
        GL.glColor4ub(color[0], color[1], color[2], color[3])
    elif len(color) == 3:
        GL.glColor4ub(color[0], color[1], color[2], 255)

    GL.glBegin(GL.GL_LINE_LOOP)
    for point in point_list:
        GL.glVertex3f(point[0], point[1], 0.5)
    GL.glEnd()
Exemple #16
0
 def run(self):
     while not self.has_exit:
         dt = pyglet.clock.tick()
         self.dispatch_events()
         gl.glClear(gl.GL_COLOR_BUFFER_BIT)
         gl.glLoadIdentity()
         self.shader.use()
         self.shader["real"] = self.real
         self.shader["w"] = self.w
         self.shader["imag"] = self.imag
         self.shader["h"] = self.h
         gl.glBegin(gl.GL_QUADS)
         gl.glVertex3f(0.0, 0.0, 0.0)
         gl.glVertex3f(0.0, self.height, 0.0)
         gl.glVertex3f(self.width, self.height, 0.0)
         gl.glVertex3f(self.width, 0.0, 0.0)
         gl.glEnd()
         self.shader.stop()
         if self.show_fps:
             self.fps.draw()
         if self.auto_zoom_in:
             self.zoom_in(dt)
         if self.auto_zoom_out:
             self.zoom_out(dt)
         self.key_move(dt=dt)
         self.flip()
Exemple #17
0
    def draw(self):
        glPushMatrix()

        glTranslatef(self.xoffset, self.yoffset, self.zoffset)

        def color(i):
            if i % self.graduations_major == 0:
                glColor4f(*self.color_grads_major)
            elif i % (self.graduations_major / 2) == 0:
                glColor4f(*self.color_grads_interm)
            else:
                if self.light: return False
                glColor4f(*self.color_grads_minor)
            return True

        # draw the grid
        glBegin(GL_LINES)
        for i in range(0, int(math.ceil(self.width + 1))):
            if color(i):
                glVertex3f(float(i), 0.0, 0.0)
                glVertex3f(float(i), self.depth, 0.0)

        for i in range(0, int(math.ceil(self.depth + 1))):
            if color(i):
                glVertex3f(0, float(i), 0.0)
                glVertex3f(self.width, float(i), 0.0)
        glEnd()

        # draw fill
        glColor4f(*self.color_fill)
        glRectf(0.0, 0.0, float(self.width), float(self.depth))

        glPopMatrix()
Exemple #18
0
def rect(a, b, color=(1.0,1.0,1.0), alpha=1.0):
    """
    Draws a rectangle in the plane spanned by a,b with GL_QUADS

    :param a: Point a
    :type a: 2-float tuple

    :param b: Point b
    :type b: 2-float tuple

    :param color: the color in [0..1] range
    :type color: 3-float tuple

    :param aa: Anti aliasing Flag

    :param alpha: the alpha value in [0..1] range

    """

    glDisable(GL_TEXTURE_2D)

    glBegin(GL_QUADS)
    glVertex3f(a[0], a[1], 0)
    glVertex3f(b[0], a[1], 0)
    glVertex3f(b[0], b[1], 0)
    glVertex3f(a[0], b[1], 0)

    glEnd()
    glEnable(GL_TEXTURE_2D)
    glColor4f(color+(alpha,))
Exemple #19
0
def draw_rectangle_outline(center_x, center_y, width, height, color,
                           border_width=1, tilt_angle=0):
    """
    Draw a rectangle outline.

    Args:
        :x: x coordinate of top left rectangle point.
        :y: y coordinate of top left rectangle point.
        :width: width of the rectangle.
        :height: height of the rectangle.
        :color: color, specified in a list of 3 or 4 bytes in RGB or
         RGBA format.
        :border_width: width of the lines, in pixels.
        :angle: rotation of the rectangle. Defaults to zero.
    Returns:
        None
    Raises:
        None

    Example:

    >>> import arcade
    >>> arcade.open_window("Drawing Example", 800, 600)
    >>> arcade.set_background_color(arcade.color.WHITE)
    >>> arcade.start_render()
    >>> arcade.draw_rectangle_outline(278, 150, 45, 105, \
arcade.color.BRITISH_RACING_GREEN, 2)
    >>> arcade.finish_render()
    >>> arcade.quick_run(0.25)
    """

    GL.glEnable(GL.GL_BLEND)
    GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA)
    GL.glEnable(GL.GL_LINE_SMOOTH)
    GL.glHint(GL.GL_LINE_SMOOTH_HINT, GL.GL_NICEST)
    GL.glHint(GL.GL_POLYGON_SMOOTH_HINT, GL.GL_NICEST)

    GL.glLoadIdentity()
    GL.glTranslatef(center_x, center_y, 0)
    if tilt_angle:
        GL.glRotatef(tilt_angle, 0, 0, 1)

    # Set line width
    GL.glLineWidth(border_width)

    # Set color
    if len(color) == 4:
        GL.glColor4ub(color[0], color[1], color[2], color[3])
    elif len(color) == 3:
        GL.glColor4ub(color[0], color[1], color[2], 255)

    GL.glBegin(GL.GL_LINE_LOOP)
    GL.glVertex3f(-width // 2, -height // 2, 0.5)
    GL.glVertex3f(width // 2, -height // 2, 0.5)
    GL.glVertex3f(width // 2, height // 2, 0.5)
    GL.glVertex3f(-width // 2, height // 2, 0.5)
    GL.glEnd()
Exemple #20
0
def draw_polygon_filled(point_list, color):
    """
    Draw a polygon that is filled in.

    Args:
        :point_list: List of points making up the lines. Each point is
         in a list. So it is a list of lists.
        :color: color, specified in a list of 3 or 4 bytes in RGB or
         RGBA format.
    Returns:
        None
    Raises:
        None

    >>> import arcade
    >>> arcade.open_window("Drawing Example", 800, 600)
    >>> arcade.set_background_color(arcade.color.WHITE)
    >>> arcade.start_render()
    >>> point_list = ((150, 240), \
(165, 240), \
(180, 255), \
(180, 285), \
(165, 300), \
(150, 300))
    >>> arcade.draw_polygon_filled(point_list, arcade.color.SPANISH_VIOLET)
    >>> arcade.finish_render()
    >>> arcade.quick_run(0.25)
    """
    GL.glEnable(GL.GL_BLEND)
    GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA)
    GL.glEnable(GL.GL_LINE_SMOOTH)
    GL.glHint(GL.GL_LINE_SMOOTH_HINT, GL.GL_NICEST)
    GL.glHint(GL.GL_POLYGON_SMOOTH_HINT, GL.GL_NICEST)

    GL.glLoadIdentity()

    # Set color
    if len(color) == 4:
        GL.glColor4ub(color[0], color[1], color[2], color[3])
    elif len(color) == 3:
        GL.glColor4ub(color[0], color[1], color[2], 255)

    GL.glBegin(GL.GL_POLYGON)
    for point in point_list:
        GL.glVertex3f(point[0], point[1], 0.5)
    GL.glEnd()
Exemple #21
0
 def f():
     pgl.glBegin(pgl.GL_LINE_STRIP)
     for t in range(len(self.t_set)):
         p = self.verts[t]
         if p is None:
             pgl.glEnd()
             pgl.glBegin(pgl.GL_LINE_STRIP)
             continue
         if use_cverts:
             c = self.cverts[t]
             if c is None:
                 c = (0, 0, 0)
             pgl.glColor3f(*c)
         else:
             pgl.glColor3f(*self.default_wireframe_color)
         pgl.glVertex3f(*p)
     pgl.glEnd()
Exemple #22
0
    def draw_points(self):
        self.points_lock.acquire()

        try:
            from pyglet.gl import glBegin, GL_POINTS, GL_LINES, glColor4f, glVertex3f, glEnd

            # pack parameters into array
            parameters = numpy.zeros(len(self.system.get_parameter_names()))

            for i in range(0, len(parameters)):
                parameters[i] = self.parameter_ranges[i][0]

            # draw lines or points
            endpoints = [1]

            if self.use_lines:
                glBegin(GL_LINES)
                endpoints = [1, 2]
            else:
                glBegin(GL_POINTS)

            for i in xrange(0, self.density):
                p = self.points[i]

                # draw two endpoints if using lines, iterating system after first endpoint
                for endpoint in endpoints:
                    x = p.state[self.state_indices[0]]
                    y = 0
                    z = 0

                    if len(self.state_ranges) > 1:
                        y = p.state[self.state_indices[1]]

                    if len(self.state_ranges) > 2:
                        z = p.state[self.state_indices[2]]

                    glColor4f(1, 1, 1, 0.1)
                    glVertex3f(x, y, z)

                    if endpoint == 1:
                        p.state = self.system.iterate(p.state, parameters)

            glEnd()
        except Exception, detail:
            print "draw_points()", type(detail), detail
Exemple #23
0
def draw_points(point_list, color, size):
    """
    Draw a set of points.

    Args:
        :point_list: List of points Each point is
         in a list. So it is a list of lists.
        :color: color, specified in a list of 3 or 4 bytes in RGB or
         RGBA format.
        :size: Size of the point in pixels.
    Returns:
        None
    Raises:
        None

    Example:

    >>> import arcade
    >>> arcade.open_window("Drawing Example", 800, 600)
    >>> arcade.set_background_color(arcade.color.WHITE)
    >>> arcade.start_render()
    >>> point_list = ((165, 495), \
(165, 480), \
(165, 465), \
(195, 495), \
(195, 480), \
(195, 465))
    >>> arcade.draw_points(point_list, arcade.color.ZAFFRE, 10)
    >>> arcade.finish_render()
    >>> arcade.quick_run(0.25)
    """
    GL.glEnable(GL.GL_BLEND)
    GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA)

    GL.glLoadIdentity()

    GL.glPointSize(size)
    if len(color) == 4:
        GL.glColor4ub(color[0], color[1], color[2], color[3])
    elif len(color) == 3:
        GL.glColor4ub(color[0], color[1], color[2], 255)
    GL.glBegin(GL.GL_POINTS)
    for point in point_list:
        GL.glVertex3f(point[0], point[1], 0.5)
    GL.glEnd()
Exemple #24
0
def draw_rect(texture, points, direction=0, use_cam=True):
    # Set the texture
    gl.glEnable(gl.GL_TEXTURE_2D)
    gl.glBindTexture(gl.GL_TEXTURE_2D, texture)
    # Allow alpha blending
    gl.glEnable(gl.GL_BLEND)
    gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
    # draw
    gl.glBegin(gl.GL_QUADS)
    for i, vert in enumerate(points):
        b = (i + direction) % 4 # render according to the direction
        if use_cam:
            x, y = adjust_for_cam(vert)
        else:
            x, y = vert
        texture = b // 2, ((b + 1) // 2) % 2
        gl.glTexCoord2f(*texture)
        gl.glVertex3f(x, y, 0)
    gl.glEnd()
Exemple #25
0
 def generate_model(self):
     """ Generate the vertices and normals.
     """
     corner = 0.5
     vertices = [
         [[+corner, +corner, +corner], [+corner, -corner, +corner],
          [+corner, -corner, -corner], [+corner, +corner, -corner]],
         # Right face
         [[-corner, +corner, -corner], [-corner, -corner, -corner],
          [-corner, -corner, +corner], [-corner, +corner, +corner]],
         # Left face
         [[-corner, -corner, +corner], [-corner, -corner, -corner],
          [+corner, -corner, -corner], [+corner, -corner, +corner]],
         # Bottom face
         [[-corner, +corner, -corner], [-corner, +corner, +corner],
          [+corner, +corner, +corner], [+corner, +corner, -corner]],
         # Top face
         [[+corner, +corner, +corner], [-corner, +corner, +corner],
          [-corner, -corner, +corner], [+corner, -corner, +corner]],
         # Front face
         [[-corner, -corner, -corner], [-corner, +corner, -corner],
          [+corner, +corner, -corner], [+corner, -corner, -corner]]
         # Back face
     ]
     normals = [[+1, 0, 0], [-1, 0, 0], [0, -1, 0],
                [0, +1, 0], [0, 0, +1], [0, 0, -1]]
     # Draw inside (reverse winding and normals)
     for face in range(self.skip_right_face, 6):
         gl.glNormal3f(-normals[face][0], -normals[face][1],
                              -normals[face][2])
         for vertex in range(0, 3):
             gl.glVertex3f(gl.GLfloat(vertices[face][3 - vertex][0]),
                        gl.GLfloat(vertices[face][3 - vertex][1]),
                        gl.GLfloat(vertices[face][3 - vertex][2]))
         for vertex in (0, 2, 3):
             gl.glVertex3f(gl.GLfloat(vertices[face][3 - vertex][0]),
                        gl.GLfloat(vertices[face][3 - vertex][1]),
                        gl.GLfloat(vertices[face][3 - vertex][2]))
     # Draw outside
     for face in range(self.skip_right_face, 6):
         gl.glNormal3f(gl.GLfloat(normals[face][0]),
                              gl.GLfloat(normals[face][1]),
                    gl.GLfloat(normals[face][2]))
         for vertex in range(0, 3):
             gl.glVertex3f(gl.GLfloat(vertices[face][vertex][0]),
                        gl.GLfloat(vertices[face][vertex][1]),
                        gl.GLfloat(vertices[face][vertex][2]))
         for vertex in (0, 2, 3):
             gl.glVertex3f(gl.GLfloat(vertices[face][vertex][0]),
                        gl.GLfloat(vertices[face][vertex][1]),
                        gl.GLfloat(vertices[face][vertex][2]))
Exemple #26
0
    def draw(self):
        # TODO: Let's use particles. And lighting! Way more fun.

        p = adjust_for_cam(self._body.position)

        if off_screen(self._body.position):
            return

        gl.glEnable(gl.GL_TEXTURE_2D) # enables texturing, without it everything is white
        gl.glBindTexture(gl.GL_TEXTURE_2D, EXPLOSION_ANIM.id)
        gl.glEnable(gl.GL_BLEND)
        gl.glBlendFunc(gl.GL_SRC_ALPHA,gl.GL_ONE_MINUS_SRC_ALPHA);

        # TODO: more optimized to draw as one large batch
        gl.glBegin(gl.GL_QUADS)

        r = (self.radius + BLOCK_SIZE) * SPACE.scale
        for i, vert in enumerate([Vec2d(p.x - r, p.y - r),
                                  Vec2d(p.x + r, p.y - r),
                                  Vec2d(p.x + r, p.y + r),
                                  Vec2d(p.x - r, p.y + r),]):

            # TODO: This is really ugly, and a lot of shared code with the
            #       blocks. Let's write a draw_rect function to handle this
            #       with an optional animation option.

            x = (i // 2)
            if x == 0:
                x = FRAME_POSITIONS[int((20 - self.ticks) / 20. * ANIM_FRAMES)][0]
            else:
                x = FRAME_POSITIONS[int((20 - self.ticks) / 20. * ANIM_FRAMES)][0] + 1 / ANIM_COLUMNS
            y = ((i + 1) // 2) % 2
            if y == 0:
                y = FRAME_POSITIONS[int((20 - self.ticks) / 20. * ANIM_FRAMES)][1]
            else:
                y = FRAME_POSITIONS[int((20 - self.ticks) / 20. * ANIM_FRAMES)][1] + 1 / ANIM_ROWS

            gl.glTexCoord2f(x, y)
            gl.glVertex3f(vert.x, vert.y, 0)

        gl.glEnd()
Exemple #27
0
def draw_point(x, y, color, size):
    """
    Draw a point.

    Args:
        :x: x position of point.
        :y: y position of point.
        :color: color, specified in a list of 3 or 4 bytes in RGB or
         RGBA format.
        :size: Size of the point in pixels.
    Returns:
        None
    Raises:
        None

    Example:

    >>> import arcade
    >>> arcade.open_window("Drawing Example", 800, 600)
    >>> arcade.set_background_color(arcade.color.WHITE)
    >>> arcade.start_render()
    >>> arcade.draw_point(60, 495, arcade.color.RED, 10)
    >>> arcade.finish_render()
    >>> arcade.quick_run(0.25)
    """
    GL.glEnable(GL.GL_BLEND)
    GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA)

    GL.glLoadIdentity()

    GL.glPointSize(size)
    if len(color) == 4:
        GL.glColor4ub(color[0], color[1], color[2], color[3])
    elif len(color) == 3:
        GL.glColor4ub(color[0], color[1], color[2], 255)
    GL.glBegin(GL.GL_POINTS)
    GL.glVertex3f(x, y, 0.5)
    GL.glEnd()
Exemple #28
0
def draw_rectangle_filled(center_x, center_y, width, height, color,
                          tilt_angle=0):
    """
    Draw a filled-in rectangle.

    Args:
        :x: x coordinate of top left rectangle point.
        :y: y coordinate of top left rectangle point.
        :width: width of the rectangle.
        :height: height of the rectangle.
        :color: color, specified in a list of 3 or 4 bytes in RGB or
         RGBA format.
        :angle: rotation of the rectangle. Defaults to zero.

    Example:

    >>> import arcade
    >>> arcade.open_window("Drawing Example", 800, 600)
    >>> arcade.set_background_color(arcade.color.WHITE)
    >>> arcade.start_render()
    >>> arcade.draw_rectangle_filled(390, 150, 45, 105, arcade.color.BLUSH)
    >>> arcade.finish_render()
    >>> arcade.quick_run(0.25)
    """

    GL.glEnable(GL.GL_BLEND)
    GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA)
    GL.glEnable(GL.GL_LINE_SMOOTH)
    GL.glHint(GL.GL_LINE_SMOOTH_HINT, GL.GL_NICEST)
    GL.glHint(GL.GL_POLYGON_SMOOTH_HINT, GL.GL_NICEST)

    # Set color
    if len(color) == 4:
        GL.glColor4ub(color[0], color[1], color[2], color[3])
    elif len(color) == 3:
        GL.glColor4ub(color[0], color[1], color[2], 255)

    GL.glLoadIdentity()
    GL.glTranslatef(center_x, center_y, 0)
    if tilt_angle:
        GL.glRotatef(tilt_angle, 0, 0, 1)

    GL.glBegin(GL.GL_QUADS)
    GL.glVertex3f(-width // 2, -height // 2, 0.5)
    GL.glVertex3f(width // 2, -height // 2, 0.5)
    GL.glVertex3f(width // 2, height // 2, 0.5)
    GL.glVertex3f(-width // 2, height // 2, 0.5)
    GL.glEnd()
Exemple #29
0
 def drawBackground(self):
     glColor3f(self.backColour[0],self.backColour[1],self.backColour[2])
     glTranslatef(self.focusX, self.focusY, 0)
     glBegin(GL_QUADS)
     glVertex3i(0, 0, 0)
     glVertex3f(0, float(self.height), 0)
     glVertex3f(float(self.width), float(self.height), 0)
     glVertex3f(float(self.width), 0, 0)
     glEnd()
     glTranslatef(-self.focusX, -self.focusY, 0)
Exemple #30
0
 def flush(self):
     gl.glPushMatrix()
     gl.glScalef(defs.WINDOW_SCALE[0], defs.WINDOW_SCALE[1], 1)
     gl.glTranslatef(-self.game.camera_x, -self.game.camera_y, 0)
     # draw quads
     keys = list(set(self.callbacks.keys() + self.quads.keys()))
     keys.sort()
     for k in keys:
         gl.glBegin(gl.GL_QUADS)
         quads = self.quads.get(k)
         if quads:
             for x, y, w, h, z, c1, c2, c3, c4, bf in quads:
                 if bf:
                     gl.glEnd()
                     gl.glBlendFunc(bf[0], bf[1])
                     gl.glBegin(gl.GL_QUADS)
                 if c1:
                     gl.glColor4f(c1[0], c1[1], c1[2], c1[3])
                 gl.glVertex3f(x, y + h, z)
                 if c2:
                     gl.glColor4f(c2[0], c2[1], c2[2], c2[3])
                 gl.glVertex3f(x + w, y + h, z)
                 if c3:
                     gl.glColor4f(c3[0], c3[1], c3[2], c3[3])
                 gl.glVertex3f(x + w, y, z)
                 if c4:
                     gl.glColor4f(c4[0], c4[1], c4[2], c4[3])
                 gl.glVertex3f(x, y, z)
                 if c1 or c2 or c3 or c4:
                     gl.glColor3f(1, 1, 1)
                 if bf:
                     gl.glEnd()
                     gl.glBlendFunc(gl.GL_SRC_ALPHA,
                                    gl.GL_ONE_MINUS_SRC_ALPHA)
                     gl.glBegin(gl.GL_QUADS)
         gl.glEnd()
         callbacks = self.callbacks.get(k)
         if callbacks:
             for callback, args, kwargs in callbacks:
                 callback(*args, **kwargs)
     gl.glPopMatrix()
     self.callbacks = {}
     self.quads = {}
     self.flush_labels()
Exemple #31
0
 def render1(self):
     (GL_POINTS)  # draw point
     glVertex3f(0.0, 0.0, 0.0)
     glEnd()
Exemple #32
0
 def _render(self):
     arg = gl.GL_LINE_LOOP if self.close else gl.GL_LINE_STRIP
     gl.glBegin(arg)
     for vertex in self.vertices:
         gl.glVertex3f(vertex[0], vertex[1], 0.)
     gl.glEnd()
Exemple #33
0
 def horiz_ind(place, val, color):
     gl.glColor4f(color[0], color[1], color[2], 1)
     gl.glVertex3f((place + 0) * s, 4 * h, 0)
     gl.glVertex3f((place + val) * s, 4 * h, 0)
     gl.glVertex3f((place + val) * s, 2 * h, 0)
     gl.glVertex3f((place + 0) * s, 2 * h, 0)
Exemple #34
0
    def on_draw_(self):
        with self._lock:
            for i in range(0, self._analyzer.n):
                # TODO: Calculate those in on_resize()
                x_min = (i * self._bars_width +
                         i * self.bars_spacing) * self.width
                x_max = ((i + 1) * self._bars_width +
                         i * self.bars_spacing) * self.width

                if self._bars_height[i] > 0:
                    gl.glBegin(gl.GL_QUADS)
                    gl.glColor3f(1.0, 0.0, 3.0)
                    gl.glVertex3f(x_min, 0.0, 0.0)
                    gl.glVertex3f(x_max, 0.0, 0.0)
                    gl.glColor3f(0.3, 0.0, 1.0)
                    gl.glVertex3f(x_max, self._bars_height[i] * self.height,
                                  0.0)
                    gl.glVertex3f(x_min, self._bars_height[i] * self.height,
                                  0.0)
                    gl.glEnd()

                if self.draw_ticks and self._ticks_y[i] > 0:
                    gl.glLineWidth(2.0)
                    gl.glBegin(gl.GL_LINES)
                    gl.glColor3f(1.0, 1.0, 1.0)
                    gl.glVertex3f(x_min, self._ticks_y[i] * self.height + 2.0,
                                  0.0)
                    gl.glVertex3f(x_max, self._ticks_y[i] * self.height + 2.0,
                                  0.0)
                    gl.glEnd()
        # print(x, y, z)

        # use OpenGL rendering commands here...

        # Just draw a triangle 2 meters away. Let's use the ovrMatrix4f type to
        # handle the translation. You can do whatever you like to the position
        # every frame.
        #
        triangle_origin = ovrVector3f(0.0, 0.0, -2.0)
        M = ovrMatrix4f.translation(triangle_origin)

        GL.glPushMatrix()
        GL.glMultMatrixf(M.ctypes)  # multiply the scene by the matrix
        GL.glBegin(GL.GL_TRIANGLES)
        GL.glColor3f(1, 0, 0)
        GL.glVertex3f(-1.0, -1.0, 0.0)
        GL.glColor3f(0, 1, 0)
        GL.glVertex3f(1.0, -1.0, 0.0)
        GL.glColor3f(0, 0, 1)
        GL.glVertex3f(0.0, 1.0, 0.0)
        GL.glEnd()
        GL.glPopMatrix()

    # send the rendered buffer to the HMD
    hmd.flip()

    # check if the application should exit
    if event.getKeys('q') or hmd.shouldQuit:
        stopApp = True
    elif event.getKeys('r') or hmd.shouldRecenter:
        hmd.recenterTrackingOrigin()
Exemple #36
0
 def render1(self):
     gl.glBegin(gl.GL_LINE_LOOP if self.close else gl.GL_LINE_STRIP)
     for p in self.v:
         gl.glVertex3f(p[0], p[1],0)  # draw each vertex
     gl.glEnd()
Exemple #37
0
    def render(self,
               mode="human",
               close=False,
               lidar_scan_override=None,
               save_to_file=False,
               draw_score=True):
        if close:
            if self.viewer is not None:
                self.viewer.close()
            return

        # get last obs
        goal_pred = self.data["robotstates"][self.current_iteration - 1][:2]
        action = self.data["actions"][self.current_iteration - 1]
        scan = self.data["scans"][self.current_iteration - 1]
        scan[scan == 0] = MAX_LIDAR_DIST  # render 0 returns as inf
        if lidar_scan_override is not None:
            scan = lidar_scan_override

        if mode == "rgb_array":
            raise NotImplementedError
        elif mode in ["human", "rings"]:
            # Window and viewport size
            WINDOW_W = 256
            WINDOW_H = 256
            M_PER_PX = 13.6 / WINDOW_H
            VP_W = WINDOW_W
            VP_H = WINDOW_H
            from gym.envs.classic_control import rendering
            import pyglet
            from pyglet import gl

            # enable this to render for hg_archivenv visual
            if False:
                draw_score = False
                save_to_file = True
                WINDOW_W = 512
                WINDOW_H = 512
                M_PER_PX = 51.2 / WINDOW_H

            # Create viewer
            if self.viewer is None:
                self.viewer = rendering.Viewer(WINDOW_W, WINDOW_H)
                self.score_label = pyglet.text.Label(
                    "0000",
                    font_size=12,
                    x=20,
                    y=WINDOW_H * 2.5 / 40.00,
                    anchor_x="left",
                    anchor_y="center",
                    color=(255, 255, 255, 255),
                )
                #                 self.transform = rendering.Transform()
                self.currently_rendering_iteration = 0
                self.image_lock = threading.Lock()
            # Render in pyglet
            def make_circle(c, r, res=10):
                thetas = np.linspace(0, 2 * np.pi, res + 1)[:-1]
                verts = np.zeros((res, 2))
                verts[:, 0] = c[0] + r * np.cos(thetas)
                verts[:, 1] = c[1] + r * np.sin(thetas)
                return verts

            with self.image_lock:
                self.currently_rendering_iteration += 1
                self.viewer.draw_circle(r=10, color=(0.3, 0.3, 0.3))
                win = self.viewer.window
                win.switch_to()
                win.dispatch_events()
                win.clear()
                gl.glViewport(0, 0, VP_W, VP_H)
                # colors
                #                 bgcolor = np.array([0.4, 0.8, 0.4])
                bgcolor = np.array([0.7, 0.75, 0.86])
                nosecolor = np.array([0.3, 0.3, 0.3])
                #                 lidarcolor = np.array([1.0, 0.0, 0.0])
                lidarcolor = np.array([1., 0.3, 0.0])
                #                 fovcolor = np.array([1., 1., 0.])
                fovcolor = np.array([0.8, 0.8, 0.2])
                # Green background
                gl.glBegin(gl.GL_QUADS)
                gl.glColor4f(bgcolor[0], bgcolor[1], bgcolor[2], 1.0)
                gl.glVertex3f(0, VP_H, 0)
                gl.glVertex3f(VP_W, VP_H, 0)
                gl.glVertex3f(VP_W, 0, 0)
                gl.glVertex3f(0, 0, 0)
                gl.glEnd()
                # LIDAR
                i = WINDOW_W / 2.0
                j = WINDOW_H / 2.0
                angle = np.pi / 2.0
                lidar_angles = np.linspace(0, 2 * np.pi, len(scan) + 1)[:-1]
                lidar_angles = lidar_angles + np.pi / 2.  # make robot face up
                i_ray_ends = i + scan / M_PER_PX * np.cos(lidar_angles)
                j_ray_ends = j + scan / M_PER_PX * np.sin(lidar_angles)
                is_in_fov = np.cos(lidar_angles - angle) >= 0.78
                for ray_idx in range(len(scan)):
                    end_i = i_ray_ends[ray_idx]
                    end_j = j_ray_ends[ray_idx]
                    gl.glBegin(gl.GL_LINE_LOOP)
                    if is_in_fov[ray_idx]:
                        gl.glColor4f(fovcolor[0], fovcolor[1], fovcolor[2],
                                     0.1)
                    else:
                        gl.glColor4f(lidarcolor[0], lidarcolor[1],
                                     lidarcolor[2], 0.1)
                    gl.glVertex3f(i, j, 0)
                    gl.glVertex3f(end_i, end_j, 0)
                    gl.glEnd()
                # Agent body
                i = WINDOW_W / 2.0
                j = WINDOW_H / 2.0
                r = 0.3 / M_PER_PX
                angle = np.pi / 2.0
                poly = make_circle((i, j), r)
                gl.glBegin(gl.GL_POLYGON)
                color = np.array([1.0, 1.0, 1.0])
                gl.glColor4f(color[0], color[1], color[2], 1)
                for vert in poly:
                    gl.glVertex3f(vert[0], vert[1], 0)
                gl.glEnd()
                # Direction triangle
                inose = i + r * np.cos(angle)
                jnose = j + r * np.sin(angle)
                iright = i + 0.3 * r * -np.sin(angle)
                jright = j + 0.3 * r * np.cos(angle)
                ileft = i - 0.3 * r * -np.sin(angle)
                jleft = j - 0.3 * r * np.cos(angle)
                gl.glBegin(gl.GL_TRIANGLES)
                gl.glColor4f(nosecolor[0], nosecolor[1], nosecolor[2], 1)
                gl.glVertex3f(inose, jnose, 0)
                gl.glVertex3f(iright, jright, 0)
                gl.glVertex3f(ileft, jleft, 0)
                gl.glEnd()
                # Goal
                goalcolor = np.array([1., 1., 0.3])
                px_goal = goal_pred / M_PER_PX
                igoal = i - px_goal[1]  # rotate 90deg to face up
                jgoal = j + px_goal[0]
                # Goal line
                gl.glBegin(gl.GL_LINE_LOOP)
                gl.glColor4f(goalcolor[0], goalcolor[1], goalcolor[2], 1)
                gl.glVertex3f(i, j, 0)
                gl.glVertex3f(igoal, jgoal, 0)
                gl.glEnd()
                # Goal markers
                gl.glBegin(gl.GL_TRIANGLES)
                gl.glColor4f(goalcolor[0], goalcolor[1], goalcolor[2], 1)
                triangle = make_circle((igoal, jgoal), r, res=3)
                for vert in triangle:
                    gl.glVertex3f(vert[0], vert[1], 0)
                gl.glEnd()
                # Text
                self.score_label.text = "File {} It {} a {:.1f} {:.1f} {:.1f}".format(
                    self.current_iteration // 1000,
                    self.current_iteration % 1000,
                    action[0],
                    action[1],
                    action[2],
                )
                if draw_score:
                    self.score_label.draw()
                win.flip()
                if save_to_file:
                    pyglet.image.get_buffer_manager().get_color_buffer().save(
                        "/tmp/archivenv{:05}.png".format(
                            self.currently_rendering_iteration))
                return self.viewer.isopen
Exemple #38
0
 def render1(self) -> None:
     glBegin(GL_POINTS)  # draw point
     glVertex3f(0.0, 0.0, 0.0)
     glEnd()
Exemple #39
0
    def draw_objects(self):
        '''called in the middle of ondraw after the buffer has been cleared'''
        self.create_objects()

        glPushMatrix()
        glTranslatef(0, 0, -self.dist)
        glMultMatrixd(build_rotmatrix(
            self.basequat))  # Rotate according to trackball
        glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE,
                     vec(0.2, 0.2, 0.2, 1))
        glTranslatef(-self.build_dimensions[3] - self.platform.width / 2,
                     -self.build_dimensions[4] - self.platform.depth / 2,
                     0)  # Move origin to bottom left of platform
        # Draw platform
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
        glDisable(GL_LIGHTING)
        self.platform.draw()
        glEnable(GL_LIGHTING)
        # Draw mouse
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
        inter = self.mouse_to_plane(self.mousepos[0],
                                    self.mousepos[1],
                                    plane_normal=(0, 0, 1),
                                    plane_offset=0,
                                    local_transform=False)
        if inter is not None:
            glPushMatrix()
            glTranslatef(inter[0], inter[1], inter[2])
            glBegin(GL_TRIANGLES)
            glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE,
                         vec(1, 0, 0, 1))
            glNormal3f(0, 0, 1)
            glVertex3f(2, 2, 0)
            glVertex3f(-2, 2, 0)
            glVertex3f(-2, -2, 0)
            glVertex3f(2, -2, 0)
            glVertex3f(2, 2, 0)
            glVertex3f(-2, -2, 0)
            glEnd()
            glPopMatrix()

        # Draw objects
        glDisable(GL_CULL_FACE)
        glPushMatrix()
        glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE,
                     vec(0.3, 0.7, 0.5, 1))
        for i in self.parent.models:
            model = self.parent.models[i]
            glPushMatrix()
            glTranslatef(*(model.offsets))
            glRotatef(model.rot, 0.0, 0.0, 1.0)
            glTranslatef(*(model.centeroffset))
            glScalef(*model.scale)
            model.batch.draw()
            glPopMatrix()
        glPopMatrix()
        glEnable(GL_CULL_FACE)

        # Draw cutting plane
        if self.parent.cutting:
            # FIXME: make this a proper Actor
            axis = self.parent.cutting_axis
            fixed_dist = self.parent.cutting_dist
            dist, plane_width, plane_height = self.get_cutting_plane(
                axis, fixed_dist)
            if dist is not None:
                glPushMatrix()
                if axis == "x":
                    glRotatef(90, 0, 1, 0)
                    glRotatef(90, 0, 0, 1)
                    glTranslatef(0, 0, dist)
                elif axis == "y":
                    glRotatef(90, 1, 0, 0)
                    glTranslatef(0, 0, -dist)
                elif axis == "z":
                    glTranslatef(0, 0, dist)
                glDisable(GL_CULL_FACE)
                glBegin(GL_TRIANGLES)
                glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE,
                             vec(0, 0.9, 0.15, 0.3))
                glNormal3f(0, 0, self.parent.cutting_direction)
                glVertex3f(plane_width, plane_height, 0)
                glVertex3f(0, plane_height, 0)
                glVertex3f(0, 0, 0)
                glVertex3f(plane_width, 0, 0)
                glVertex3f(plane_width, plane_height, 0)
                glVertex3f(0, 0, 0)
                glEnd()
                glEnable(GL_CULL_FACE)
                glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
                glEnable(GL_LINE_SMOOTH)
                orig_linewidth = (GLfloat)()
                glGetFloatv(GL_LINE_WIDTH, orig_linewidth)
                glLineWidth(4.0)
                glBegin(GL_LINE_LOOP)
                glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE,
                             vec(0, 0.8, 0.15, 1))
                glVertex3f(0, 0, 0)
                glVertex3f(0, plane_height, 0)
                glVertex3f(plane_width, plane_height, 0)
                glVertex3f(plane_width, 0, 0)
                glEnd()
                glLineWidth(orig_linewidth)
                glDisable(GL_LINE_SMOOTH)
                glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
                glPopMatrix()

        glPopMatrix()
Exemple #40
0
    def render_floors(self):
        PLAYFIELD = 2000
        gl.glBegin(gl.GL_QUADS)
        gl.glColor4f(1, 1, 1, 1.0)
        gl.glVertex3f(-PLAYFIELD, +PLAYFIELD, 0)
        gl.glVertex3f(+PLAYFIELD, +PLAYFIELD, 0)
        gl.glVertex3f(+PLAYFIELD, -PLAYFIELD, 0)
        gl.glVertex3f(-PLAYFIELD, -PLAYFIELD, 0)

        # increase range by one to add line on the top
        for floor in range(self.floor_num + 1):
            gl.glColor4f(0, 0, 0, 1)
            gl.glVertex3f(self.screen_width, 50 + self.floor_padding * floor,
                          0)
            gl.glVertex3f(self.screen_width,
                          50 + self.floor_padding * floor + 1, 0)
            gl.glVertex3f(self.screen_width / 2,
                          50 + self.floor_padding * floor + 1, 0)
            gl.glVertex3f(self.screen_width / 2,
                          50 + self.floor_padding * floor, 0)
        gl.glEnd()
Exemple #41
0
 def vertical_ind(place, val, color):
     gl.glColor4f(color[0], color[1], color[2], 1)
     gl.glVertex3f((place + 0) * s, h + h * val, 0)
     gl.glVertex3f((place + 1) * s, h + h * val, 0)
     gl.glVertex3f((place + 1) * s, h, 0)
     gl.glVertex3f((place + 0) * s, h, 0)
Exemple #42
0
 def render_road(self):
     gl.glBegin(gl.GL_QUADS)
     gl.glColor4f(0.4, 0.8, 0.4, 1.0)
     gl.glVertex3f(-PLAYFIELD, +PLAYFIELD, 0)
     gl.glVertex3f(+PLAYFIELD, +PLAYFIELD, 0)
     gl.glVertex3f(+PLAYFIELD, -PLAYFIELD, 0)
     gl.glVertex3f(-PLAYFIELD, -PLAYFIELD, 0)
     gl.glColor4f(0.4, 0.9, 0.4, 1.0)
     k = PLAYFIELD / 20.0
     for x in range(-20, 20, 2):
         for y in range(-20, 20, 2):
             gl.glVertex3f(k * x + k, k * y + 0, 0)
             gl.glVertex3f(k * x + 0, k * y + 0, 0)
             gl.glVertex3f(k * x + 0, k * y + k, 0)
             gl.glVertex3f(k * x + k, k * y + k, 0)
     for poly, color in self.road_poly:
         gl.glColor4f(color[0], color[1], color[2], 1)
         for p in poly:
             gl.glVertex3f(p[0], p[1], 0)
     gl.glEnd()
Exemple #43
0
    def render(self, mode="human", close=False):
        rings_pred = (self.vae.decode(self.sequence_z[0, -1].reshape(1, _Z)) *
                      self.rings_def["rings_to_bool"])
        predicted_ranges = self.rings_def["rings_to_lidar"](rings_pred, 1080)

        if mode == "rgb_array":
            raise NotImplementedError
        elif mode == "human":
            # Window and viewport size
            WINDOW_W = 256
            WINDOW_H = 256
            M_PER_PX = 25.6 / WINDOW_H
            VP_W = WINDOW_W
            VP_H = WINDOW_H
            from gym.envs.classic_control import rendering
            import pyglet
            from pyglet import gl

            # Create viewer
            if self.viewer is None:
                self.viewer = rendering.Viewer(WINDOW_W, WINDOW_H)
                self.score_label = pyglet.text.Label(
                    "0000",
                    font_size=12,
                    x=20,
                    y=WINDOW_H * 2.5 / 40.00,
                    anchor_x="left",
                    anchor_y="center",
                    color=(255, 255, 255, 255),
                )
                #                 self.transform = rendering.Transform()
                self.currently_rendering_iteration = 0
                self.image_lock = threading.Lock()
            # Render in pyglet
            def make_circle(c, r, res=10):
                thetas = np.linspace(0, 2 * np.pi, res + 1)[:-1]
                verts = np.zeros((res, 2))
                verts[:, 0] = c[0] + r * np.cos(thetas)
                verts[:, 1] = c[1] + r * np.sin(thetas)
                return verts

            with self.image_lock:
                self.currently_rendering_iteration += 1
                self.viewer.draw_circle(r=10, color=(0.3, 0.3, 0.3))
                win = self.viewer.window
                win.switch_to()
                win.dispatch_events()
                win.clear()
                gl.glViewport(0, 0, VP_W, VP_H)
                # colors
                bgcolor = np.array([0.4, 0.8, 0.4])
                nosecolor = np.array([0.3, 0.3, 0.3])
                lidarcolor = np.array([1.0, 0.0, 0.0])
                # Green background
                gl.glBegin(gl.GL_QUADS)
                gl.glColor4f(bgcolor[0], bgcolor[1], bgcolor[2], 1.0)
                gl.glVertex3f(0, VP_H, 0)
                gl.glVertex3f(VP_W, VP_H, 0)
                gl.glVertex3f(VP_W, 0, 0)
                gl.glVertex3f(0, 0, 0)
                gl.glEnd()
                # LIDAR
                i = WINDOW_W / 2.0
                j = WINDOW_H / 2.0
                angle = np.pi / 2.0
                scan = np.squeeze(predicted_ranges)
                lidar_angles = np.linspace(0, 2 * np.pi, len(scan) + 1)[:-1]
                i_ray_ends = i + scan / M_PER_PX * np.cos(lidar_angles)
                j_ray_ends = j + scan / M_PER_PX * np.sin(lidar_angles)
                is_in_fov = np.cos(lidar_angles - angle) >= 0.78
                for ray_idx in range(len(scan)):
                    end_i = i_ray_ends[ray_idx]
                    end_j = j_ray_ends[ray_idx]
                    gl.glBegin(gl.GL_LINE_LOOP)
                    if is_in_fov[ray_idx]:
                        gl.glColor4f(1.0, 1.0, 0.0, 0.1)
                    else:
                        gl.glColor4f(lidarcolor[0], lidarcolor[1],
                                     lidarcolor[2], 0.1)
                    gl.glVertex3f(i, j, 0)
                    gl.glVertex3f(end_i, end_j, 0)
                    gl.glEnd()
                # Agent body
                i = WINDOW_W / 2.0
                j = WINDOW_H / 2.0
                r = 0.3 / M_PER_PX
                angle = np.pi / 2.0
                poly = make_circle((i, j), r)
                gl.glBegin(gl.GL_POLYGON)
                color = np.array([1.0, 1.0, 1.0])
                gl.glColor4f(color[0], color[1], color[2], 1)
                for vert in poly:
                    gl.glVertex3f(vert[0], vert[1], 0)
                gl.glEnd()
                # Direction triangle
                inose = i + r * np.cos(angle)
                jnose = j + r * np.sin(angle)
                iright = i + 0.3 * r * -np.sin(angle)
                jright = j + 0.3 * r * np.cos(angle)
                ileft = i - 0.3 * r * -np.sin(angle)
                jleft = j - 0.3 * r * np.cos(angle)
                gl.glBegin(gl.GL_TRIANGLES)
                gl.glColor4f(nosecolor[0], nosecolor[1], nosecolor[2], 1)
                gl.glVertex3f(inose, jnose, 0)
                gl.glVertex3f(iright, jright, 0)
                gl.glVertex3f(ileft, jleft, 0)
                gl.glEnd()
                # Text
                self.score_label.text = "A {:.1f} {:.1f} {:.1f} S {}".format(
                    self.prev_action[0],
                    self.prev_action[1],
                    self.prev_action[2],
                    self.episode_step,
                )
                self.score_label.draw()
                win.flip()
                return self.viewer.isopen
Exemple #44
0
 def render1(self):
     gl.glBegin(gl.GL_POINTS) # draw point
     gl.glVertex3f(0.0, 0.0, 0.0)
     gl.glEnd()
Exemple #45
0
 def _render(self):
     gl.glBegin(gl.GL_POINTS)
     gl.glVertex3f(0., 0., 0.)
     gl.glEnd()
Exemple #46
0
 def _render_rings(self, close, save_to_file=False):
     if close:
         self.viewer.close()
         return
     # rendering
     if self.backend in ["VAE1DLSTM", "GPT1D", "VAE1D_LSTM"]:
         return False
     else:
         last_rings_obs = self.last_lidar_obs.reshape((_64, _64))
         last_rings_pred = self.vae.decode(self.lidar_z.reshape((1,self._Z))).reshape((_64, _64))
         # Window and viewport size
         ring_size = _64  # grid cells
         padding = 4  # grid cells
         grid_size = 1  # px per grid cell
         WINDOW_W = (2 * ring_size + 3 * padding) * grid_size
         WINDOW_H = (1 * ring_size + 2 * padding) * grid_size
         VP_W = WINDOW_W
         VP_H = WINDOW_H
         from gym.envs.classic_control import rendering
         import pyglet
         from pyglet import gl
         # Create viewer
         if self.viewer is None:
             self.viewer = rendering.Viewer(WINDOW_W, WINDOW_H)
             self.rendering_iteration = 0
         # Render in pyglet
         win = self.viewer.window
         win.switch_to()
         win.dispatch_events()
         win.clear()
         gl.glViewport(0, 0, VP_W, VP_H)
         # colors
         bgcolor = np.array([0.4, 0.8, 0.4])
         # Green background
         gl.glBegin(gl.GL_QUADS)
         gl.glColor4f(bgcolor[0], bgcolor[1], bgcolor[2], 1.0)
         gl.glVertex3f(0, VP_H, 0)
         gl.glVertex3f(VP_W, VP_H, 0)
         gl.glVertex3f(VP_W, 0, 0)
         gl.glVertex3f(0, 0, 0)
         gl.glEnd()
         # rings - observation
         w_offset = 0
         for rings in [last_rings_obs, last_rings_pred]:
             for i in range(ring_size):
                 for j in range(ring_size):
                     cell_color = 1 - rings[i, j]
                     cell_y = (padding + i) * grid_size  # px
                     cell_x = (padding + j + w_offset) * grid_size  # px
                     gl.glBegin(gl.GL_QUADS)
                     gl.glColor4f(cell_color, cell_color, cell_color, 1.0)
                     gl.glVertex3f(cell_x+       0,  cell_y+grid_size, 0)  # noqa
                     gl.glVertex3f(cell_x+grid_size, cell_y+grid_size, 0)  # noqa
                     gl.glVertex3f(cell_x+grid_size, cell_y+        0, 0)  # noqa
                     gl.glVertex3f(cell_x+        0, cell_y+        0, 0)  # noqa
                     gl.glEnd()
             w_offset += ring_size + padding
         if save_to_file:
             pyglet.image.get_buffer_manager().get_color_buffer().save(
                 "/tmp/encodeder_rings{:04d}.png".format(self.rendering_iteration))
         # actualize
         win.flip()
         self.rendering_iteration += 1
         return self.viewer.isopen
 def render_background(self):
     gl.glBegin(gl.GL_QUADS)
     gl.glColor4f(0.4, 0.8, 0.4, 1.0)
     gl.glVertex3f(-PLAYFIELD, +PLAYFIELD, 0)
     gl.glVertex3f(+PLAYFIELD, +PLAYFIELD, 0)
     gl.glVertex3f(+PLAYFIELD, -PLAYFIELD, 0)
     gl.glVertex3f(-PLAYFIELD, -PLAYFIELD, 0)
     gl.glColor4f(0.4, 0.9, 0.4, 1.0)
     k = PLAYFIELD / 20.0
     for x in range(-20, 20, 2):
         for y in range(-20, 20, 2):
             gl.glVertex3f(k * x + k, k * y + 0, 0)
             gl.glVertex3f(k * x + 0, k * y + 0, 0)
             gl.glVertex3f(k * x + 0, k * y + k, 0)
             gl.glVertex3f(k * x + k, k * y + k, 0)
     gl.glEnd()
     self.buff_areas.render(gl)
     self.supply_areas.render(gl)
Exemple #48
0
    def render_road(self):
        gl.glBegin(gl.GL_QUADS)
        gl.glColor4f(0.4, 0.8, 0.4, 1.0)
        gl.glVertex3f(-PLAYFIELD, +PLAYFIELD, 0)
        gl.glVertex3f(+PLAYFIELD, +PLAYFIELD, 0)
        gl.glVertex3f(+PLAYFIELD, -PLAYFIELD, 0)
        gl.glVertex3f(-PLAYFIELD, -PLAYFIELD, 0)
        gl.glColor4f(0.4, 0.9, 0.4, 1.0)
        k = PLAYFIELD / 20.0
        for x in range(-20, 20, 2):
            for y in range(-20, 20, 2):
                gl.glVertex3f(k * x + k, k * y + 0, 0)
                gl.glVertex3f(k * x + 0, k * y + 0, 0)
                gl.glVertex3f(k * x + 0, k * y + k, 0)
                gl.glVertex3f(k * x + k, k * y + k, 0)
        for poly, color in self.road_poly:
            gl.glColor4f(color[0], color[1], color[2], 1)
            for p in poly:
                gl.glVertex3f(p[0], p[1], 0)

        gl.glEnd()

        if self.highlight_loc is not None:
            gl.glColor4f(1, 0, 0, 1)
            gl.glRectf(self.highlight_loc[0], self.highlight_loc[1],
                       self.highlight_loc[0] + 3, self.highlight_loc[1] + 3)