def circle(color, center, radius, width=0, npoints=24): """Draw a circle :Parameters: `color` : tuple The colour to use, as a 3- or 4-tuple of floats `center` : tuple The co-ordinates of the center of the circle `radius` : float The radius of the circle `width` : float The line width for the circle. If zero, circle is filled. Defaults to zero. `npoints` : int The number of vertices on the circumference of the circle. Defaults to 24. """ _set_gl_color(color) if width: gl.glLineWidth(width) gl.glBegin(gl.GL_LINE_LOOP) else: gl.glBegin(gl.GL_POLYGON) for i in xrange(npoints): theta = i * math.pi * 2 / npoints gl.glVertex2f(center[0] + radius * math.cos(theta), center[1] + radius * math.sin(theta)) gl.glEnd()
def draw(self, state = [0,0,0]): gl.glEnable (gl.GL_LINE_SMOOTH); gl.glHint (gl.GL_LINE_SMOOTH_HINT, gl.GL_NICEST) gl.glLoadIdentity() gl.glLineWidth(1) gl.glColor3f(.7,.7,.7) draw_rect((-.5,0), (.5,-.01)) draw_rect((-.01,0),(.01,1)) gl.glTranslatef(0,self.r_roller,0); gl.glPushMatrix(); gl.glTranslatef(state[0]*self.r_roller, 0, 0) gl.glColor3f(0,0,0) gl.glLineWidth(3) # gl.glPushMatrix() gl.glRotatef(-R2D*state[0],0,0,1) draw_mass_center(self.r_roller, (0,0)) # gl.glPopMatrix() gl.glRotatef(-R2D*state[1], 0, 0, 1) gl.glTranslatef(0,self.r_roller,0) gl.glPushMatrix() gl.glRotatef(R2D*(state[1]+state[0]),0,0,1) gl.glPushAttrib(gl.GL_ENABLE_BIT); gl.glColor3f(.7,.2,.2) gl.glLineStipple(1, 0xF00F) # [1] gl.glEnable(gl.GL_LINE_STIPPLE) draw_line((0,0),(0,1)) gl.glPopAttrib() gl.glPopMatrix() gl.glTranslatef(-state[1] * self.r_roller,0,0) gl.glColor3f(0,0,0) draw_rect( (-self.l_board/2,0), (self.l_board/2,.02)) gl.glColor3f(.5,.5,.5) draw_rect((-.01,0), (.01,self.h_body)) gl.glPushMatrix() gl.glTranslatef(0, self.h_body, 0) gl.glRotatef(R2D*state[2], 0, 0, 1) gl.glColor3f(0,0,0); draw_mass_center(.1, (0,0)) gl.glPopMatrix() if len(state) >= 8: gl.glPushMatrix() gl.glTranslatef(0, self.h_body, 0) gl.glRotatef(R2D*state[3], 0, 0, 1) gl.glTranslatef(0, -self.h_body+self.r_roller+.05, 0) gl.glColor3f(0,0,0); draw_mass_center(.03, (0,0)) gl.glPopMatrix() gl.glPopMatrix();
def rect(color, corner, size, width=0): """Draw a rectangle :Parameters: `color` : tuple The colour to use, as a 3- or 4-tuple of floats `corner` : tuple The co-ordinates of the top-left corner of the rectangle `size` : tuple A tuple of (width, height) for the rectangle `width` : float The line width for the rectangle. If zero, rectangle is filled. Defaults to zero. """ _set_gl_color(color) if width: gl.glLineWidth(width) gl.glBegin(gl.GL_LINE_LOOP) else: gl.glBegin(gl.GL_QUADS) gl.glVertex2f(*corner) gl.glVertex2f(corner[0], corner[1] + size[1]) gl.glVertex2f(corner[0] + size[0], corner[1] + size[1]) gl.glVertex2f(corner[0] + size[0], corner[1]) gl.glEnd()
def axes(size=1, width=1): """draw 3d axes""" gl.glLineWidth(width) pyglet.graphics.draw( 6, gl.GL_LINES, ('v3f', (0, 0, 0, size, 0, 0, 0, 0, 0, 0, size, 0, 0, 0, 0, 0, 0, size)), ('c3f', ( 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, )))
def ellipse(color, corner, size, width=0, npoints=24): """Draw an axis-aligned ellipse :Parameters: `color` : tuple The colour to use, as a 3- or 4-tuple of floats `corner` : tuple The co-ordinates of the top-left corner of the ellipse's enclosing rectangle `size` : tuple A tuple of (width, height) for the ellipse `width` : float The line width for the ellipse. If zero, ellipse is filled. Defaults to zero. `npoints` : int The number of vertices on the circumference of the ellipse. Defaults to 24. """ _set_gl_color(color) center = (corner[0] + .5 * size[0], corner[1] + .5 * size[1]) if width: gl.glLineWidth(width) gl.glBegin(gl.GL_LINE_LOOP) else: gl.glBegin(gl.GL_POLYGON) for i in xrange(npoints): theta = i * math.pi * 2 / npoints gl.glVertex2f(center[0] + .5 * size[0] * math.cos(theta), center[1] + .5 * size[1] * math.sin(theta)) gl.glEnd()
def draw(self): """Draw the object to the display buffer""" from pyglet import gl gl.glUseProgram(self._program) for kind in ('fill', 'line'): if self._counts[kind] > 0: if kind == 'line': if self._line_width <= 0.0: continue gl.glLineWidth(self._line_width) if self._line_loop: mode = gl.GL_LINE_LOOP else: mode = gl.GL_LINE_STRIP cmd = partial(gl.glDrawArrays, mode, 0, self._counts[kind]) else: gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, self._buffers[kind]['index']) cmd = partial(gl.glDrawElements, gl.GL_TRIANGLES, self._counts[kind], gl.GL_UNSIGNED_INT, 0) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self._buffers[kind]['array']) loc_pos = gl.glGetAttribLocation(self._program, b'a_position') gl.glEnableVertexAttribArray(loc_pos) gl.glVertexAttribPointer(loc_pos, 2, gl.GL_FLOAT, gl.GL_FALSE, 0, 0) loc_col = gl.glGetUniformLocation(self._program, b'u_color') gl.glUniform4f(loc_col, *self._colors[kind]) cmd() # The following line is probably only necessary because # Pyglet makes some assumptions about the GL state that # it perhaps shouldn't. Without it, Text might not # render properly (see #252) gl.glDisableVertexAttribArray(loc_pos) gl.glUseProgram(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()
def draw(self): if self.camera == None: gl.glLineWidth(lw) rgb = mcol.hsv_to_rgb(self.hsv) gl.glColor4f(rgb[0], rgb[1], rgb[2], 0.5) gl.glBegin(gl.GL_LINES) gl.glVertex2f(self.vtsW[0, 0], self.vtsW[1, 0]) gl.glVertex2f(self.vtsW[0, 1], self.vtsW[1, 1]) gl.glEnd() gl.glBegin(gl.GL_LINE_STRIP) gl.glVertex2f(self.vtsW[0, -1], self.vtsW[1, -1]) gl.glVertex2f(self.vtsW[0, -3], self.vtsW[1, -3]) gl.glVertex2f(self.vtsW[0, -2], self.vtsW[1, -2]) gl.glEnd() else: self.project2Camera() gl.glLineWidth(lw) rgb = mcol.hsv_to_rgb(self.hsv) gl.glColor4f(rgb[0], rgb[1], rgb[2], 0.5) gl.glBegin(gl.GL_LINES) gl.glVertex2f(self.vtsC[0, 0], self.vtsC[1, 0]) gl.glVertex2f(self.vtsC[0, 1], self.vtsC[1, 1]) gl.glEnd() gl.glBegin(gl.GL_LINE_STRIP) gl.glVertex2f(self.vtsC[0, -1], self.vtsC[1, -1]) gl.glVertex2f(self.vtsC[0, -3], self.vtsC[1, -3]) gl.glVertex2f(self.vtsC[0, -2], self.vtsC[1, -2]) gl.glEnd()
def draw_canvas(self, canvas): sprites, batch = canvas.get_sprites(self.scale) batch.draw() if self.highlighted_cell and self.draw_borders: h_x, h_y = self.highlighted_cell gl.glLineWidth(2.0) pyglet.graphics.draw(8, gl.GL_LINES, ("v2i", (h_x*self.scale*self.tile_size[0], h_y*self.scale*self.tile_size[1], h_x*self.scale*self.tile_size[0], (h_y+1)*self.scale*self.tile_size[1], h_x*self.scale*self.tile_size[0], (h_y+1)*self.scale*self.tile_size[1], (h_x+1)*self.scale*self.tile_size[0], (h_y+1)*self.scale*self.tile_size[1], (h_x+1)*self.scale*self.tile_size[0], (h_y+1)*self.scale*self.tile_size[1], (h_x+1)*self.scale*self.tile_size[0], h_y*self.scale*self.tile_size[1], (h_x+1)*self.scale*self.tile_size[0], h_y*self.scale*self.tile_size[1], h_x*self.scale*self.tile_size[0], h_y*self.scale*self.tile_size[1],)), ("c3B", (0,0,0,255,255,255)*4))
def render(shape: VertexBuffer): """ Render an shape previously created with a ``create`` function. """ # Set color if shape.color is None: raise ValueError("Error: Color parameter not set.") gl.glLoadIdentity() gl.glBindBuffer(gl.GL_ARRAY_BUFFER, shape.vbo_vertex_id) gl.glVertexPointer(2, gl.GL_FLOAT, 0, 0) if shape.line_width: gl.glLineWidth(shape.line_width) if len(shape.color) == 4: gl.glColor4ub(shape.color[0], shape.color[1], shape.color[2], shape.color[3]) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) elif len(shape.color) == 3: gl.glDisable(gl.GL_BLEND) gl.glColor4ub(shape.color[0], shape.color[1], shape.color[2], 255) gl.glDrawArrays(shape.draw_mode, 0, shape.size)
def draw_message(self): if event.message != self.current_message: self.current_message = event.message self.message_label.text = event.message self.message_label.font_size = event.message_size xa = self.message_label.content_width // 2 + 20 ya = self.message_label.content_height // 2 + 5 gl.glLineWidth(3.0) gl.glPointSize(1.0) draw.set_color(0, 0, 0, 0.8) draw.rect( self.message_label.x - xa, self.message_label.y - ya, self.message_label.x + xa, self.message_label.y + ya ) draw.set_color(0, 0, 0, 1) draw.rect_outline( self.message_label.x - xa, self.message_label.y - ya, self.message_label.x + xa, self.message_label.y + ya ) draw.points( ( self.message_label.x - xa, self.message_label.y - ya, self.message_label.x + xa, self.message_label.y - ya, self.message_label.x + xa, self.message_label.y + ya, self.message_label.x - xa, self.message_label.y + ya, ) ) self.message_label.draw()
def interact(bld, fullscreen=False): # pragma: no cover """Interact with the billiard simulation. Args: bld: A billiard simulation. fullscreen (optional): Set window to fullscreen, defaults to False. """ # print instructions print("Play/Pause: Space, move: WASD, zoom: QE, simspeed: RF, exit: Esc") # create window window = BilliardWindow(bld, width=800, height=600, resizable=True) if fullscreen: window.set_fullscreen() # OpenGL settings for high quality line drawing 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(pyglet.gl.GL_LINE_SMOOTH_HINT, gl.GL_NICEST) gl.glLineWidth(2) # show window, start simulation pyglet.app.run()
def lines(self, *points): glColor4f(*self._stroke_color) glLineWidth(self._line_width) glBegin(GL_LINE_STRIP) for point in points: glVertex2f(point.x, point.y) glEnd()
def arc(color, corner, size, start_angle, stop_angle, width=1, npoints=24): """Draw an arc of an axis-aligned ellipse :Parameters: `color` : tuple The colour to use, as a 3- or 4-tuple of floats `corner` : tuple The co-ordinates of the top-left corner of the ellipse's enclosing rectangle `size` : tuple A tuple of (width, height) for the ellipse `start_angle` : float The start angle of the arc, in radians, counterclockwise from the positive x-axis `stop_angle` : float The start angle of the arc, in radians, counterclockwise from the positive x-axis `width` : float The line width for the ellipse. Defaults to 1.0 `npoints` : int The number of vertices on the circumference of the ellipse. Defaults to 24. """ _set_gl_color(color) center = (corner[0] + .5 * size[0], corner[1] + .5 * size[1]) gl.glLineWidth(width) gl.glBegin(gl.GL_LINE_STRIP) for i in range(npoints + 1): theta = start_angle + (float(i * (stop_angle - start_angle)) / npoints) gl.glVertex2f(center[0] + .5 * size[0] * math.cos(theta), center[1] + .5 * size[1] * math.sin(theta)) gl.glEnd()
def render(self): """ Draws Line """ p1 = self.a2 p2 = self.b2 gl.glColor4f(*self.color) color = (gl.GLfloat * 4)(*self.color) gl.glPushMatrix() gl.glTranslatef(self.x, self.y, -self.z) # translate to GL loc ppint gl.glRotatef(self.rotation, 0, 0, 0.1) if self.style: gl.glEnable(gl.GL_LINE_STIPPLE) gl.glLineStipple(1, self.style) ## else : ## glDisable(GL_LINE_STIPPLE) if self.stroke <= 0: self.stroke = 1 gl.glLineWidth(self.stroke) gl.glBegin(gl.GL_LINES) gl.glVertex2i(int(p1[0]), int(p1[1])) # draw pixel points gl.glVertex2i(int(p2[0]), int(p2[1])) gl.glEnd() if self.style: gl.glDisable(gl.GL_LINE_STIPPLE) gl.glPopMatrix()
def line(a, b, color=(1.0,1.0,1.0), width=1, aa=False, alpha=1.0): """ Draws a line from point *a* to point *b* using GL_LINE_STRIP optionaly with GL_LINE_SMOOTH when *aa=True* :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 width: The with for glLineWidth() :param aa: Anti aliasing Flag :param alpha: the alpha value in [0..1] range """ glLineWidth(width) if aa: glEnable(GL_LINE_SMOOTH) draw(2,GL_LINES,('v2f',(a[0],a[1],b[0],b[1]) ) )
def on_draw(self): self.parent.set_caption(str(pyglet.clock.get_fps())) gl.glClear(gl.GL_COLOR_BUFFER_BIT) gl.glColor4ub(*[255,255,255,255]) self.room.bg.blit(0,0) self.room.render() gl.glColor4ub(255,255,255,255) self.player.draw() self.room.lightbatch.draw() self.player.draw_eye() self.player.draw_integrity() if self.pause: gl.glColor4ub(50,50,50,150) left = self.message['text'].x-self.message['text'].width/2 -5 down = self.message['text'].y-self.message['text'].height/2 -5 right = self.message['text'].x+self.message['text'].width/2 + 5 up = self.message['text'].y+self.message['text'].height/2 + 5 gl.glRecti(left,down,right,up) gl.glLineWidth(2) gl.glColor4ub(200,200,200,200) gl.glBegin(gl.GL_LINE_LOOP) gl.glVertex2i(left,down) gl.glVertex2i(left,up) gl.glVertex2i(right,up) gl.glVertex2i(right,down) gl.glEnd() gl.glLineWidth(1) gl.glColor4ub(255,255,255,255) self.message['text'].draw() self.message['helper'].draw() self.message['sprite'].draw()
def draw(self): x, y = self.factor glLineWidth(5) # Draw y-axis (green) graphics.draw(2, GL_LINES, ('v3i', (0, 0, 0, 0, y * self.rows, 0)), ('c3B', (0, 255, 0, 0, 255, 0))) # Draw x-axis (red) graphics.draw(2, GL_LINES, ('v3i', (0, 0, 0, self.columns * x, 0, 0)), ('c3B', (255, 0, 0, 255, 0, 0))) # Draw z-axis (blue) graphics.draw(2, GL_LINES, ('v3i', (0, 0, 0, 0, 0, y * self.rows)), ('c3B', (0, 0, 255, 0, 0, 255))) glLineWidth(2) # Draw outer grid graphics.draw( 5, GL_LINE_STRIP, ('v3i', (0, self.size * self.level, 0, self.columns * x, self.size * self.level, 0, self.columns * x, self.size * self.level, y * self.rows, 0, self.size * self.level, y * self.rows, 0, self.size * self.level, 0)), ('c3B', (255, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 255))) # Draw the inner grid for i in range(1, self.columns): graphics.draw(2, GL_LINES, ('v3i', (i * x, self.size * self.level, 0, i * x, self.size * self.level, y * self.rows)), ('c3B', (0, 0, 255, 0, 0, 0))) for i in range(1, self.rows): graphics.draw(2, GL_LINES, ('v3i', (0, self.size * self.level, i * y, x * self.columns, self.size * self.level, i * y)), ('c3B', (255, 0, 0, 0, 0, 0)))
def line(x0, y0, x, y, color=(1, 0, 0, 1), thickness=1): glColor4f(*color) glLineWidth(thickness) glBegin(GL_LINES) glVertex2f(x0, y0) glVertex2f(x, y) glEnd()
def draw_line(x1, y1, x2, y2, width=2): glLineStipple(1, 0xFF) glEnable(GL_LINE_STIPPLE) glLineWidth(width) glBegin(GL_LINES) glVertex2f(x1, y1) glVertex2f(x2, y2) glEnd()
def set_state(self): self.program.bind() gl.glEnable(pyglet.gl.GL_LINE_SMOOTH) gl.glLineWidth(3) gl.glEnable(gl.GL_DEPTH_TEST) gl.glDepthFunc(gl.GL_LESS)
def display(self, mode_2d=False): glEnable(GL_LINE_SMOOTH) orig_linewidth = (GLfloat)() glGetFloatv(GL_LINE_WIDTH, orig_linewidth) glLineWidth(3.0) glCallList(self.display_list) glLineWidth(orig_linewidth) glDisable(GL_LINE_SMOOTH)
def set_state(self): gl.glPushAttrib(gl.GL_LINE_BIT | gl.GL_CURRENT_BIT) gl.glLineWidth(2) if self.facet.is_border_facet: colour = self.BORDER_FACET_COLOUR else: colour = self.INNER_FACET_COLOUR gl.glColor4f(*colour)
def _gl_enable_smooth_lines(): # make the lines from Path3D objects less ugly gl.glEnable(gl.GL_LINE_SMOOTH) gl.glHint(gl.GL_LINE_SMOOTH_HINT, gl.GL_NICEST) # set the width of lines to 4 pixels gl.glLineWidth(4) # set PointCloud markers to 4 pixels in size gl.glPointSize(4)
def draw_line(self): """Function for drawing a line""" gl.glColor4f(*self.color) gl.glLineWidth(self.thickness) gl.glBegin(gl.GL_LINES) gl.glVertex2f(self.coordx[0], self.coordy[0]) gl.glVertex2f(self.coordx[1], self.coordy[1]) gl.glEnd()
def gl_enable_smooth_lines(): gl.glEnable(gl.GL_LINE_SMOOTH) # make the lines from Path3D objects less ugly gl.glHint(gl.GL_LINE_SMOOTH_HINT, gl.GL_NICEST) gl.glLineWidth(0.5) # set the width of lines to 2 pixels gl.glPointSize(12) # set PointCloud markers to 8 pixels in size gl.glEnable(gl.GL_PROGRAM_POINT_SIZE) # set gl.glEnable(gl.GL_POINT_SMOOTH) gl.glEnable(gl.GL_BLEND)
def __init__(self, window): """ Initialize the gamescreen. window is the parent window. """ self.window = window self.width = window.width self.height= window.height self.draw_debug = False self.killcount = 0 self.total_time = 0 self.constants = {'drag':10, 'gravity':v(0,-30000), 'elasticity':0.7, 'friction':0.9, 'displace':0.7} opengl.glEnable(opengl.GL_BLEND) opengl.glBlendFunc(opengl.GL_SRC_ALPHA,opengl.GL_ONE) opengl.glLineWidth(2.0) # opengl.glEnable(opengl.GL_POINT_SMOOTH) # opengl.glHint(opengl.GL_POINT_SMOOTH_HINT,opengl.GL_NICEST) # opengl.glEnable(opengl.GL_LINE_SMOOTH) # opengl.glHint(opengl.GL_LINE_SMOOTH_HINT,opengl.GL_NICEST) # opengl.glEnable(opengl.GL_POLYGON_SMOOTH) # opengl.glHint(opengl.GL_POLYGON_SMOOTH_HINT,opengl.GL_NICEST) #Activate the depth buffer. opengl.glEnable(opengl.GL_DEPTH_TEST) #Lighting! #opengl.glEnable(opengl.GL_LIGHTING) #opengl.glEnable(opengl.GL_LIGHT0) #opengl.glLightf(opengl.GL_LIGHT0, opengl.GL_LINEAR_ATTENUATION, 0.05) ########### # Now, since this screen represents gameplay, we're going to initialize all the elements of the game we're playing. # For now, this is just a pile of stuff. ########### # Set up all the different lists of objects in the world. These roughly correspond to managers! Sort of. self.entities = [] self.physics_objects = [] self.collision_objects = [] self.nonstatic_objects = [] self.coltree = collision_structures.SpatialGrid() self.draw_objects = [] self.draw_priority = [] self.draw_tree = collision_structures.SpatialGrid() self.draw_tree.camera_rect = CollisionComponent(owner=None, pos=v(0,0), shape=shapes.Rectangle(-1,1,-1,1)) self.listeners = [] label = text.Label( 'THIS IS A TEST', 'Arial', 24, color = (0, 0, 0, 200), x = self.window.width/2, y = self.window.height/4, anchor_x="center", anchor_y="center", width=3*self.window.width/4, height=3*self.window.height/4, multiline=1) self.draw_objects.append(label) self.draw_priority.append(label)
def draw(): global main_batch gl.glClearColor(0.2, 0.4, 0.5, 1.0) gl.glBlendFunc (gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glEnable (gl.GL_BLEND) gl.glEnable (gl.GL_LINE_SMOOTH); gl.glLineWidth (3) main_batch.draw()
def draw_rectangle(self, vertexes): '''Draws rectangle around game field. ''' gl.glLineWidth(2) gl.glClearColor(0, 0, 0, 1) gl.glColor3f(1, 1, 1) gl.glBegin(gl.GL_LINE_LOOP) for point in vertexes: gl.glVertex2f(point[0], point[1]) gl.glEnd()
def draw_line(a, b, color): gl.glPushMatrix() gl.glColor3f(color[0], color[1], color[2]) gl.glLineWidth(2) gl.glBegin(gl.GL_LINES) gl.glVertex2f(a[0], a[1]) gl.glVertex2f(b[0], b[1]) gl.glEnd() gl.glPopMatrix()
def draw_cartpole(cartpole, screen_width): world_width = cartpole.x_threshold * 2 scale = screen_width / world_width carty = 100 # TOP OF CART polewidth = 10.0 polelen = scale * 1.0 * (cartpole.length / 0.5) cartwidth = 50.0 cartheight = 30.0 # Draw track glColor4f(0, 0, 0, 1.0) glLineWidth(1.0) glBegin(gl.GL_LINES) glVertex2f(0, carty) glVertex2f(screen_width, carty) glEnd() # Draw Cart l, r, t, b = -cartwidth / 2, cartwidth / 2, cartheight / 2, -cartheight / 2 cartx = cartpole.x * scale + screen_width / 2.0 # MIDDLE OF CART glColor4f(0., 0., 0., 1.0) glPushMatrix() # Push Translation glTranslatef(cartx, carty, 0) glBegin(gl.GL_QUADS) glVertex3f(l, b, 0) glVertex3f(l, t, 0) glVertex3f(r, t, 0) glVertex3f(r, b, 0) glEnd() # Draw Pole l, r, t, b = (-polewidth / 2, polewidth / 2, polelen - polewidth / 2, -polewidth / 2) glColor4f(.8, .6, .4, 1.0) glPushMatrix() # Push Rotation glRotatef(RAD2DEG * -cartpole.theta, 0, 0, 1.0) glBegin(gl.GL_QUADS) glVertex3f(l, b, 0) glVertex3f(l, t, 0) glVertex3f(r, t, 0) glVertex3f(r, b, 0) glEnd() glPopMatrix() # Pop Rotation # Draw Axle radius = polewidth / 2 glColor4f(0.5, 0.5, 0.8, 1.0) glBegin(gl.GL_POLYGON) for i in range(12): ang = 2 * math.pi * i / 12 x = math.cos(ang) * radius y = math.sin(ang) * radius glVertex3f(x, y, 0) glEnd() glPopMatrix() # Pop Translation
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()
def line(x, y, size, angle, color=(1, 0, 0, 1), thickness=1): x1, y1 = x, y x2, y2 = x1 + cos(angle) * size, y1 + sin(angle) * size glColor4f(*color) glLineWidth(thickness) glBegin(GL_LINES) glVertex2f(x1, y1) glVertex2f(x2, y2) glEnd()
def line_loop(self, *points): if len(points) <= 2: raise ValueError() glColor4f(*self._stroke_color) glLineWidth(self._line_width) glBegin(GL_LINE_LOOP) for point in points: glVertex2f(point.x, point.y) glEnd()
def __init__(self, window, file_name): """ Initialize the gamescreen. window is the parent window. """ self.window = window self.width = window.width self.height= window.height self.draw_debug = False self.killcount = 0 self.total_time = 0 self.camera_rect = components.Collider(owner=None, shape=components.shapes.Rectangle(-1,1,-1,1)) self.constants = {'drag':10, 'gravity':v(0,-30000), 'elasticity':0.7, 'friction':0.9, 'displace':0.7} opengl.glEnable(opengl.GL_BLEND) opengl.glBlendFunc(opengl.GL_SRC_ALPHA, opengl.GL_ONE) opengl.glLineWidth(2.0) # opengl.glEnable(opengl.GL_POINT_SMOOTH) # opengl.glHint(opengl.GL_POINT_SMOOTH_HINT,opengl.GL_NICEST) # opengl.glEnable(opengl.GL_LINE_SMOOTH) # opengl.glHint(opengl.GL_LINE_SMOOTH_HINT,opengl.GL_NICEST) # opengl.glEnable(opengl.GL_POLYGON_SMOOTH) # opengl.glHint(opengl.GL_POLYGON_SMOOTH_HINT,opengl.GL_NICEST) #Activate the depth buffer. opengl.glEnable(opengl.GL_DEPTH_TEST) ########### # Now, since this screen represents gameplay, we're going to initialize all the elements of the game we're playing. # For now, this is just a pile of stuff. ########### # Set up all the different lists of objects in the world. These roughly correspond to managers! Sort of. self.entities = [] self.physics_objects = [] self.collision_objects = [] self.nonstatic_objects = [] self.coltree = collision_structures.SpatialGrid() self.draw_objects = [] self.draw_priority = [] self.draw_tree = collision_structures.SpatialGrid() self.draw_tree.camera_rect = self.camera_rect self.listeners = [] try: import importlib level_data = importlib.import_module("levels." + file_name) for item in level_data.generate(self): self.add_entity(item) except ImportError as e: print("Error loading the level " + file_name) raise e
def draw_bone(self, bone): p1 = bone.get_start() p2 = bone.get_end() gl.glColor4ub(*self.color) gl.glLineWidth(5) gl.glBegin(gl.GL_LINES) gl.glVertex2f(*p1) gl.glVertex2f(*p2) gl.glEnd()
def draw(self): if not self.visible: return gl.glLineWidth(2) # deprecated gl.glColor3ub(*self.color3) gl.glBegin(gl.GL_LINE_STRIP) for v in self.vertexes: gl.glVertex2f(*v) gl.glVertex2f(*self.vertexes[0]) gl.glEnd()
def draw_line(start, end, color=(1, 1, 0, 1), width=2): gl.glColor4f(*color) gl.glLineWidth(width) gl.glBegin(gl.GL_LINES) gl.glVertex2f(*start) gl.glVertex2f(*end) gl.glEnd() # -- reset color gl.glColor4f(1, 1, 1, 1)
def draw_path(points, color=(1, 0, 1, 1), width=5): gl.glColor4f(*color) gl.glLineWidth(width) gl.glBegin(gl.GL_LINE_STRIP) for point in points: gl.glVertex2f(*point) gl.glEnd() # -- reset color gl.glColor4f(1, 1, 1, 1)
def update(self): if self.pending_actions: with self.fbo: while self.pending_actions: action = self.pending_actions.pop() if action[0] == Pen.Action.Line: gl.glLineWidth(action[2]) pyglet.graphics.draw(2, gl.GL_LINES, ('v2f', action[1]), ('c3B', 2*action[3])) else: gl.glClearColor(0, 0, 0, 0) gl.glClear(gl.GL_COLOR_BUFFER_BIT)
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()
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 axes(size=1, width=1): """draw 3d axes""" gl.glLineWidth(width) pyglet.graphics.draw(6, gl.GL_LINES, ('v3f', (0, 0, 0, size, 0, 0, 0, 0, 0, 0, size, 0, 0, 0, 0, 0, 0, size)), ('c3f', (1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, )) )
def on_draw(): window.clear() gl.glColor4f(1.0,0,0,1.0) #glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) #glEnable (GL_BLEND) gl.glEnable (gl.GL_LINE_SMOOTH); gl.glHint (gl.GL_LINE_SMOOTH_HINT, gl.GL_DONT_CARE) gl.glLineWidth (3) pts = [] for (x,y) in turtle.points: pts.append(x) pts.append(y) pyglet.graphics.draw(len(turtle.points), pyglet.gl.GL_LINE_STRIP, ('v2f', tuple(pts)))
def draw_mass_graph(): if mass_graph: mass_verts = [] mass_graph_size = foo_size.y / 6 dx = mass_graph_size / len(mass_graph) dy = mass_graph_size / max(mass_graph) for i, mass in enumerate(mass_graph): mass_verts.append(i * dx + win_size.x - mass_graph_size) mass_verts.append(mass * dy) gl.glLineWidth(3) gl.glColor4f(0, 0, 1, 1) pyglet.graphics.draw(len(mass_graph), gl.GL_LINE_STRIP, ("v2f", mass_verts)) gl.glLineWidth(1)
def drawseam( self ): 'draw manip and dotted line for the seam' pole = self.getpole() spin = self.getspin() def drawpoint( pt ): 'draw a single point with white outline and black interior' gl.glPointSize( 6 ) gl.glBegin( gl.GL_POINTS ) gl.glColor3f( 1, 1, 1) gl.glVertex3d( pt[0], pt[1], pt[2] ) gl.glEnd() gl.glPointSize( 4 ) gl.glBegin( gl.GL_POINTS ) gl.glColor3f( 0, 0, 0) gl.glVertex3d( pt[0], pt[1], pt[2] ) gl.glEnd() gl.glPointSize( 3 ) n = 60 idxs = numpy.cast[constants.INTDTYPE]( numpy.mgrid[:n] ) hcircle = numpy.cast[constants.DTYPE]( numpy.mgrid[:n] ) hcircle /= n hcircle -= .5 hcircle *= math.pi circlepts = numpy.zeros( (n, 3), dtype = constants.DTYPE) circlepts[:, 0] = -numpy.cos( hcircle ) circlepts[:, 2] = numpy.sin( hcircle ) gl.glPushMatrix() polemx = vecutil.vecspace2( pole, spin ) glmultmatrix( polemx ) gl.glLineWidth( 5 ) gl.glColor3f( 0, 0, 0) self.drawlines( circlepts, None, idxs ) gl.glLineWidth( 1 ) gl.glColor3f( 1, 1, 0) self.drawlines( circlepts, None, idxs ) gl.glPopMatrix() gl.glColor3f( 1, 1, 1) drawpoint( pole ) drawpoint( spin )
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()
def draw_segment(shape, color): position = shape.body.position a = shape.a b = shape.b radius = shape.radius gl.glPushMatrix() gl.glColor3f(color[0], color[1], color[2]) gl.glTranslatef(position[0], position[1], 0) gl.glLineWidth(radius) gl.glBegin(gl.GL_LINES) gl.glVertex2f(a[0], a[1]) gl.glVertex2f(b[0], b[1]) gl.glEnd() gl.glPopMatrix()
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()
def update_all(self): """ 在绘制之前,针对形变进行计算,通过设置openGL的属性来达到绘制出变形的图形 """ self.update_points() self.update_vertex_list() self.update_anchor() gl.glLoadIdentity() # reset gl gl.glLineWidth(self.line_width) gl.glPointSize(self.point_size) self.transform.update_gl() # handle shapes click envets all_shapes.discard(self) all_shapes.add(self)
def grid(size=1, n=10, width=1): """draw a grid on xz plane""" gl.glLineWidth(width) s = size / float(n) s2 = 0.5 * size batch = pyglet.graphics.Batch() for i in range(0, n + 1): x = -s2 + i * s batch.add(2, gl.GL_LINES, None, ('v3f', (x, 0, -s2, x, 0, s2))) for i in range(0, n + 1): z = -s2 + i * s batch.add(2, gl.GL_LINES, None, ('v3f', (-s2, 0, z, s2, 0, z))) batch.draw()
def draw_ai_message(self): if event.ai_message != self.current_ai_message: self.current_ai_message = event.ai_message self.ai_message_label.text = event.ai_message w = self.ai_message_label.content_width mx = self.ai_message_label.x my = self.ai_message_label.y gl.glLineWidth(3.0) gl.glPointSize(1.0) draw.set_color(0, 0, 0, 0.8) draw.rect(mx - 80, 10, mx + w + 10, 100) self.ai_message_label.draw() draw.set_color(1, 1, 1, 1) if event.ai_head != None: event.ai_head.blit(mx - 80, 17)
def draw(self): glPushMatrix() glPushAttrib(GL_STENCIL_BUFFER_BIT) glClear(GL_STENCIL_BUFFER_BIT) glTranslatef(self.parent.x, self.parent.y, 0) glDisable(GL_TEXTURE_2D) loader = self.loader if loader.shape == LINE_SHAPE: glEnable(GL_LINE_SMOOTH) glEnable(GL_POINT_SMOOTH) glLineWidth(loader.borderSize) glPointSize(loader.borderSize) self.vertex.draw(GL_LINES) self.vertex.draw(GL_POINTS) elif loader.shape == RECTANGLE_SHAPE: width = self.parent.width height = self.parent.height if loader.fillType != NONE_FILL: # for counters if self.counter_stencil: glEnable(GL_STENCIL_TEST) glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE) glStencilFunc(GL_ALWAYS, 1, 1) glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE) self.counter_stencil.draw(GL_QUADS) glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE) glStencilFunc(GL_EQUAL, 1, 1) self.draw_rectangle() glDisable(GL_STENCIL_TEST) else: self.draw_rectangle() if self.border is not None: self.border.draw(GL_QUADS) elif loader.shape == ELLIPSE_SHAPE: if loader.fillType != NONE_FILL: glEnable(GL_STENCIL_TEST) glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE) glStencilFunc(GL_ALWAYS, 1, 1) glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE) self.stencil.draw(GL_TRIANGLES) glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE) glStencilFunc(GL_EQUAL, 1, 1) self.draw_rectangle(True) glDisable(GL_STENCIL_TEST) if self.border: self.border.draw(GL_QUADS) glPopAttrib() glPopMatrix()
def draw_level(self): draw.clear(0, 0, 0, 1) draw.set_color(1, 1, 1, 1) level.background_image.blit_tiled(0, 0, 0, level.width, level.height) gl.glLineWidth(3.0) env.batch.draw() particle.draw() if debug_draw: draw.set_color(1, 0, 0, 1) gl.glLineWidth(1.0) for body in physics.body_update_list: if hasattr(body, "draw_collisions"): body.draw_collisions() for unit in physics.unit_update_list: if hasattr(unit, "draw_collisions"): unit.draw_collisions()