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()
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()
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()
def nakresli_caru(x1, y1, x2, y2): gl.glBegin(gl.GL_LINES) # gl.glBegin(gl.GL_TRIANGLE_FAN) gl.glVertex2f(x1, y1) gl.glVertex2f(x2, y2) gl.glEnd()
def render_boid(self): glBegin(GL_TRIANGLES) glColor3f(*self.color) glVertex2f(-(self.size), 0.0) glVertex2f(self.size, 0.0) glVertex2f(0.0, self.size * 3.0) glEnd()
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(self): self.loadStartPosition() gl.glRotatef(90.0, 0.0, 0.0, 1.0) gl.glBegin(gl.GL_QUADS) gl.glColor3f(1.0, 1.0, 0.0) tenth = math.pi * 2.0 / 10.0 for z in [-0.1, 0.1]: for i in xrange(5): a = float(i) * tenth * 2.0 gl.glVertex3f(0.0, 0.0, z) gl.glVertex3f(0.4 * math.cos(a - tenth), 0.4 * math.sin(a - tenth), z) gl.glVertex3f(math.cos(a), math.sin(a), z) gl.glVertex3f(0.4 * math.cos(a + tenth), 0.4 * math.sin(a + tenth), z) for i in xrange(5): a = float(i) * tenth * 2.0 gl.glVertex3f(0.4 * math.cos(a - tenth), 0.4 * math.sin(a - tenth), 0.1) gl.glVertex3f(math.cos(a), math.sin(a), 0.1) gl.glVertex3f(math.cos(a), math.sin(a), -0.1) gl.glVertex3f(0.4 * math.cos(a - tenth), 0.4 * math.sin(a - tenth), -0.1) gl.glVertex3f(0.4 * math.cos(a + tenth), 0.4 * math.sin(a + tenth), 0.1) gl.glVertex3f(math.cos(a), math.sin(a), 0.1) gl.glVertex3f(math.cos(a), math.sin(a), -0.1) gl.glVertex3f(0.4 * math.cos(a + tenth), 0.4 * math.sin(a + tenth), -0.1) gl.glEnd() self.loadStartPosition() gl.glTranslatef(0.0, 0.0, 0.1) gl.glScalef(0.01, 0.01, 0.0) self.label.draw() gl.glLoadIdentity()
def draw(self, colour=(0., 0., 1.)): s = self.size # just a shorthand gl.glColor3f(*colour) gl.glBegin(gl.GL_LINE_LOOP) for vertex in self.verticies: gl.glVertex2f(*vertex) gl.glEnd()
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,))
def draw_button(self, x, y, point_down, over): if over and self.pressed: border = SCROLL_BAR_PRESSED elif over: border = SCROLL_BAR_OVER else: border = SCROLL_BAR_BORDER draw_rectangle(x, y, x + self.width, y + self.buttonHeight, border) draw_rectangle(x + 1, y + 1, x + self.width - 1, y + self.buttonHeight - 1, SCROLL_BAR_BUTTON) glBegin(GL_TRIANGLES) glColor4ub(*SCROLL_BAR_POINTER) singleX = self.width / 3.0 upperX = self.width / 2.0 singleY = self.buttonHeight / 3.0 if point_down: y -= 1 glVertex2f(singleX + x, singleY * 2 + y) glVertex2f(upperX + x, singleY + y) glVertex2f(singleX * 2 + x, singleY * 2 + y) else: glVertex2f(singleX + x, singleY + y) glVertex2f(upperX + x, singleY * 2 + y) glVertex2f(singleX * 2 + x, singleY + y) glEnd()
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()
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()
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()
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 draw_block(block): transformed = [world_to_screen(block.GetWorldPoint(p)) for p in POINTS] gl.glColor3f(1.0, 0.1, 0) gl.glBegin(gl.GL_LINE_LOOP) for p in transformed: gl.glVertex2f(*p) gl.glEnd()
def draw(self): from miru.context import context gl.glEnable(gl.GL_LINE_STIPPLE) gl.glLineStipple(1, 0x51315) gl.glColor4f(*self.color) for c in self.metaCamera.cameras: vp = c.projection x = vp.x == 0 and 1 or vp.x width = vp.x == 0 and vp.width - 1 or vp.width width = (vp.x + vp.width) >= context.window.width and width - 1 or width y = vp.y == 0 and 1 or vp.y height = vp.y == 0 and vp.height - 1 or vp.height height = (vp.y + vp.height) >= context.window.height and height - 1 or height gl.glBegin(gl.GL_LINE_LOOP) gl.glVertex2f(x, y) gl.glVertex2f(x, y + height) gl.glVertex2f(x + width, y + height) gl.glVertex2f(x + width, y) gl.glEnd() gl.glDisable(gl.GL_LINE_LOOP) gl.glColor4f(1,1,1,1)
def rectangle(x1, y1, x2, y2): gl.glBegin(gl.GL_TRIANGLE_STRIP) gl.glVertex2f(x1, y1) gl.glVertex2f(x1, y2) gl.glVertex2f(x2, y1) gl.glVertex2f(x2, y2) gl.glEnd()
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_points(self): self.points_lock.acquire() try: from pyglet.gl import glCallList, glBegin, GL_LINE_STRIP, glColor4f, glVertex2f, glEnd # line from previous iterate to next iterate of function glBegin(GL_LINE_STRIP) glColor4f(1, 1, 1, 0.3) state_index = self.state_indices[0] for i in [1, 2]: state_previous = self.point.state[state_index] self.point.state = self.system.iterate(self.point.state, self.point.parameters) glVertex2f(state_previous, state_previous) glVertex2f(state_previous, self.point.state[state_index]) glEnd() # call display lists, doesn't work in init_points() if self.server.iteration <= 1: glCallList(self.iterate_list) glCallList(self.reflection_list) except Exception, detail: print "draw_points()", type(detail), detail
def draw_circle(cx, cy, r, num_segments=None, mode=gl.GL_POLYGON, color=(1, 1, 1, 1)): if not num_segments: num_segments = get_num_circle_segments(r) theta = 2 * math.pi / float(num_segments) tangetial_factor = math.tan(theta) radial_factor = math.cos(theta) x = float(r) y = 0.0 gl.glColor4f(color[0], color[1], color[2], color[3]) gl.glBegin(mode) for i in xrange(num_segments): gl.glVertex2f(x + cx, y + cy) tx = y * -1 ty = x x += tx * tangetial_factor y += ty * tangetial_factor x *= radial_factor y *= radial_factor gl.glEnd()
def draw_points(self): self.points_lock.acquire() try: from pyglet.gl import glBegin, glEnd, GL_POINTS, glColor4f, glVertex2f glBegin(GL_POINTS) for i in xrange(0, len(self.points)): p = self.points[i] parameter_index = self.parameter_indices[0] state_index = self.state_indices[0] glColor4f(1, 1, 1, p.age / (self.age_max*2.0)) glVertex2f(p.parameters[parameter_index], p.state[state_index]) p.state = self.system.iterate(p.state, p.parameters) p.age += 1 #if p.age >= self.age_max: # self.points[i] = OrbitPoint(tool=self, varying_parameter=p.parameters[parameter_index]) glEnd() except Exception, detail: print 'draw_points()', type(detail), detail
def _set_texture(self, t): self._texture = t from pyglet import gl try: gl.glFramebufferTexture2DEXT( gl.GL_FRAMEBUFFER_EXT, gl.GL_COLOR_ATTACHMENT0_EXT, t.target, t.id, 0, ) except gl.GLException: # HACK: Some Intel card return errno == 1286L # which means GL_INVALID_FRAMEBUFFER_OPERATION_EXT # but IT ACTUALLY WORKS FINE!! pass gl.glViewport(0, 0, t.width, t.height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluOrtho2D(0, t.width, 0, t.height) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() # ATI cards hack gl.glBegin(gl.GL_LINES) gl.glEnd()
def draw(self): gl.glClear(gl.GL_DEPTH_BUFFER_BIT) if self.game.game_over: if self.game.game_lost: gl.glBegin(gl.GL_QUADS) gl.glColor3f(1, 1, 1) gl.glVertex3f(0, 0, -0.9) gl.glVertex3f(0, defs.WINDOW_HEIGHT, -0.9) gl.glVertex3f(defs.WINDOW_WIDTH, defs.WINDOW_HEIGHT, -0.9) gl.glVertex3f(defs.WINDOW_WIDTH, 0, -0.9) gl.glEnd() self.you_died.draw() self.you_died2.draw() if not self.died: for name in self.VALUES: getattr(self, name + '_label').color = (0, 0, 0, 255) self.died = True else: gl.glBegin(gl.GL_QUADS) gl.glColor3f(0, 0, 0) gl.glVertex3f(0, 0, -0.9) gl.glVertex3f(0, defs.WINDOW_HEIGHT, -0.9) gl.glVertex3f(defs.WINDOW_WIDTH, defs.WINDOW_HEIGHT, -0.9) gl.glVertex3f(defs.WINDOW_WIDTH, 0, -0.9) gl.glEnd() self.you_win.draw() for name in self.VALUES: getattr(self, name + '_label').draw()
def blit_buffer(self, framebuffer, parent_width, parent_height, **kwargs): """Draw the texture into the parent scene .. warning: This method's arguments are not part of the API yet and may change at any time. """ gl.glViewport(0, 0, parent_width, parent_height) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST) gl.glBindTexture(gl.GL_TEXTURE_2D, framebuffer.texture_id) gl.glEnable(gl.GL_TEXTURE_2D) gl.glColor4fv((gl.GLfloat * 4)(*self.color + (self.opacity, ))) gl.glBlendFunc(gl.GL_ONE, gl.GL_ONE_MINUS_SRC_ALPHA) # premultipl. gl.glBegin(gl.GL_TRIANGLE_STRIP) gl.glTexCoord2f(0, 0) gl.glVertex2i(0, 0) gl.glTexCoord2f(0, parent_height) gl.glVertex2i(0, parent_height) gl.glTexCoord2f(parent_width, 0) gl.glVertex2i(parent_width, 0) gl.glTexCoord2f(parent_width, parent_height) gl.glVertex2i(parent_width, parent_height) gl.glEnd() gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR) gl.glDisable(gl.GL_TEXTURE_2D) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glViewport(0, 0, parent_width, parent_height)
def draw(self): self.label.draw() gl.glBegin(pyglet.gl.GL_TRIANGLES) gl.glVertex2f(0, 0) gl.glVertex2f(100, 0) gl.glVertex2f(100, 200) gl.glEnd()
def draw_rectangle(x1, y1, x2, y2, r, g, b): gl.glColor4ub(r, g, b, 255) gl.glBegin(gl.GL_QUADS) gl.glVertex2f(x1, y1) gl.glVertex2f(x2, y1) gl.glVertex2f(x2, y2) gl.glVertex2f(x1, y2) gl.glEnd()
def draw(self): r = self.size.x gl.glColor3f(1.0, .7, 0.0) with shiftView(self.body.position): gl.glBegin(gl.GL_LINE_LOOP) for angle in xrange(0, 360, 360/12): gl.glVertex2f(*self.size.rotated_degrees(angle)) gl.glEnd()
def draw_rect(x, y, width, height): """A convenience function to draw the button rectangle.""" gl.glBegin(gl.GL_QUADS) gl.glVertex2f(x, y) gl.glVertex2f(x + width, y) gl.glVertex2f(x + width, y + height) gl.glVertex2f(x, y + height) gl.glEnd()
def display_pointer(angle, color, radius): x_value = math.cos(angle) y_value = math.sin(angle) glBegin(GL_LINES) glColor3ub(*color) glVertex2f(0.0, 0.0) glVertex2f(x_value * radius, y_value * radius) glEnd()
def rectangle(x1, y1, x2, y2, color=(1, 0, 0, 1)): glColor4f(*color) glBegin(GL_POLYGON) glVertex2f(x1, y1) glVertex2f(x1, y2) glVertex2f(x2, y2) glVertex2f(x2, y1) glEnd()
def bezier_draw(cps, n=20, red=False): pts = [bezier_point(cps, i / (n - 1)) for i in range(0, n)] gl.glBegin(gl.GL_LINE_STRIP) if red: gl.glColor3f(1, 0, 0) else: gl.glColor3f(0, 0, 1) for i, p in enumerate(pts): gl.glVertex3f(*p) gl.glEnd() gl.glColor3f(1, 1, 1)
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(self): gl.glLineWidth(lw) rgb = mcol.hsv_to_rgb(self.hsv) gl.glColor4f(rgb[0], rgb[1], rgb[2], 0.5) gl.glBegin(gl.GL_LINE_STRIP) if self.camera == None: for i in range(self.num_vts): gl.glVertex2f(self.vtsW[0, i], self.vtsW[1, i]) gl.glEnd() else: self.project2Camera() for i in range(self.num_vts): gl.glVertex2f(self.vtsC[0, i], self.vtsC[1, i]) gl.glEnd()
def draw(self): super(Square, self).draw() gl.glColor4f(*self.layer_color) x, y = self.x, self.y w = x + self.size h = y + self.size gl.glBegin(gl.GL_QUADS) gl.glVertex2f(x, y) gl.glVertex2f(x, h) gl.glVertex2f(w, h) gl.glVertex2f(w, y) gl.glEnd() gl.glColor4f(1, 1, 1, 1)
def draw(self): """ Draws a circle. """ self.before_draw() # vertices in counter-clockwise gl.glBegin(gl.GL_TRIANGLE_FAN) for angle in range(0, 360, 10): rads = math.radians(angle) s = self.radius * math.sin(rads) c = self.radius * math.cos(rads) gl.glVertex3f(self.x + c, self.y + s, -10) 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 on_draw(self): self.fenetre3d.clear() #pgl.glPushMatrix() #pgl.glRotatef(self.fenetre3d.xRotation, 0, 1, 0) #pgl.glRotatef(0, 0, 1, 1) pgl.glBegin(ogl.GL_QUADS) #bleu pgl.glColor3ub(0, 0, 255) pgl.glVertex3f(0, 0, 0) pgl.glVertex3f(0, 10, 0) pgl.glVertex3f(self.arene.nb_colonne, 10, 0) pgl.glVertex3f(self.arene.nb_colonne, 0, 0) #rouge pgl.glColor3ub(255, 0, 0) pgl.glVertex3f(self.arene.nb_colonne, 0, 0) pgl.glVertex3f(self.arene.nb_colonne, 10, 0) pgl.glVertex3f(self.arene.nb_colonne, 10, self.arene.nb_ligne) pgl.glVertex3f(self.arene.nb_colonne, 0, self.arene.nb_ligne) #vert pgl.glColor3ub(0, 255, 0) pgl.glVertex3f(0, 0, self.arene.nb_ligne) pgl.glVertex3f(0, 10, self.arene.nb_ligne) pgl.glVertex3f(self.arene.nb_colonne, 10, self.arene.nb_ligne) pgl.glVertex3f(self.arene.nb_colonne, 0, self.arene.nb_ligne) #jaune pgl.glColor3ub(255, 255, 0) pgl.glVertex3f(00, 0, 00) pgl.glVertex3f(00, 10, 00) pgl.glVertex3f(00, 10, self.arene.nb_ligne) pgl.glVertex3f(00, 0, self.arene.nb_ligne) #blanc pgl.glColor3ub(255, 255, 255) pgl.glVertex3f(00, 0, self.arene.nb_ligne) pgl.glVertex3f(00, 0, 0) pgl.glVertex3f(self.arene.nb_colonne, 0, 0) pgl.glVertex3f(self.arene.nb_colonne, 0, self.arene.nb_ligne) self.draw() pgl.glEnd() """Permet de faire un screenshot""" kitten = pyglet.image.load('balise2.png') sprite = pyglet.sprite.Sprite(kitten) sprite.set_position(00, 80) sprite.draw() pyglet.image.get_buffer_manager().get_color_buffer().save( 'screenshot.png') pgl.glFlush()
def init_model(self, scene): """ Add the pyramid normals and vertices to the View :param scene: The view to render the model to. :type scene: pyglet_helper.objects.View """ # Note that this model is also used by arrow! scene.pyramid_model.gl_compile_begin() vertices = [[0, .5, .5], [0, -.5, .5], [0, -.5, -.5], [0, .5, -.5], [1, 0, 0]] triangle_indices = [0, 0, 0] * 6 triangle_indices[0] = [3, 0, 4] # top triangle_indices[1] = [1, 2, 4] # bottom triangle_indices[2] = [0, 1, 4] # front triangle_indices[3] = [3, 4, 2] # back triangle_indices[4] = [0, 3, 2] # left (base) 1 triangle_indices[5] = [0, 2, 1] # left (base) 2 normals = [[1, 2, 0], [1, -2, 0], [1, 0, 2], [1, 0, -2], [-1, 0, 0], [-1, 0, 0]] gl.glEnable(gl.GL_CULL_FACE) gl.glBegin(gl.GL_TRIANGLES) # Inside for face in range(0, 6): gl.glNormal3f(-normals[face][0], -normals[face][1], -normals[face][2]) for vertex in range(0, 3): #print triangle_indices[face] #print vertices[triangle_indices[face]][2 - vertex] vert = [gl.GLfloat(i) for i in vertices[triangle_indices[face][2 - vertex]]] gl.glVertex3f(*vert) # Outside for face in range(0, 6): gl.glNormal3fv(*[gl.GLfloat(i) for i in normals[face]]) for vertex in range(0, 3): gl.glVertex3f(*[gl.GLfloat(i) for i in vertices[triangle_indices[ face][vertex]]]) gl.glEnd() gl.glDisable(gl.GL_CULL_FACE) self.compiled = True scene.pyramid_model.gl_compile_end()
def render_agent(self, b): if b.type == "Client": color = 1 if b.onboard == 1: return if b.type == "Taxi": color = 2 glBegin(GL_TRIANGLES) glColor3f(*colors[color]) glVertex2f(-(5), 0.0) glVertex2f(5, 0.0) glVertex2f(0.0, 5 * 3.0) glEnd()
def draw_filled_circle(position, size=20, num_of_segments=30, color=None): if color is None: glColor3ub(255, 0, 0) else: glColor3f(*color) twice_pi = 2.0 * 3.141596 glBegin(GL_TRIANGLE_FAN) glVertex2f(position.x, position.y) for i in range(num_of_segments + 1): glVertex2f( (position.x + (size * math.cos(i * twice_pi / num_of_segments))), (position.y + (size * math.sin(i * twice_pi / num_of_segments)))) glEnd()
def nakresli_zivot(x, y, barva): """Nakresli zivot na dane pozici a v dane barve.""" gl.glTranslatef(x, y, 0.0) gl.glRotatef(45, 0.0, 0.0, 1.0) gl.glColor3f(*barva) gl.glBegin(gl.GL_TRIANGLE_FAN) gl.glVertex2f(-ZIVOT_VELIKOST / 2, -ZIVOT_VELIKOST / 2) gl.glVertex2f(-ZIVOT_VELIKOST / 2, +ZIVOT_VELIKOST / 2) gl.glVertex2f(+ZIVOT_VELIKOST / 2, +ZIVOT_VELIKOST / 2) gl.glVertex2f(+ZIVOT_VELIKOST / 2, -ZIVOT_VELIKOST / 2) gl.glEnd() gl.glRotatef(-45, 0.0, 0.0, 1.0) gl.glTranslatef(-x, -y, 0.0)
def nakresli_granat(x, y, rotace, barva): """Nakresli granat na dane pozici a v dane barve.""" gl.glTranslatef(x, y, 0.0) gl.glRotatef(rotace, 0.0, 0.0, 1.0) gl.glColor3f(*barva) gl.glBegin(gl.GL_TRIANGLE_FAN) gl.glVertex2f(-GRANAT_SIRKA / 2, -GRANAT_DELKA / 2) gl.glVertex2f(-GRANAT_SIRKA / 2, +GRANAT_DELKA / 2) gl.glVertex2f(+GRANAT_SIRKA / 2, +GRANAT_DELKA / 2) gl.glVertex2f(+GRANAT_SIRKA / 2, -GRANAT_DELKA / 2) gl.glEnd() gl.glRotatef(-rotace, 0.0, 0.0, 1.0) gl.glTranslatef(-x, -y, 0.0)
def multi_draw(*args, **kwargs): gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_ONE, gl.GL_ONE) gl.glEnable(gl.GL_LINE_SMOOTH) gl.glEnable(gl.GL_POINT_SMOOTH) gl.glClearColor(0, 0, 0, 0) gl.glColor4f(1, 1, 1, 1) gl.glClear(gl.GL_COLOR_BUFFER_BIT) if not buffers: buffers.append(image.get_buffer_manager()) x, y, w, h = buffers[0].get_viewport() #Draw lowres version gl.glViewport(0, 0, 256, 256) func(*args, **kwargs) texes.append(buffers[0].get_color_buffer().texture) if len(texes) > num_texes: texes.pop(0) #Lay down copies of lowres version gl.glViewport(x, y, w, h) gl.glClear(gl.GL_COLOR_BUFFER_BIT) if texes: alphas = range(1, len(texes) + 1) alphas = [float(f) / sum(alphas) for f in alphas] for tex, alpha in zip(texes, alphas): gl.glBindTexture(tex.target, tex.id) gl.glEnable(gl.GL_TEXTURE_2D) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR) gl.glColor4f(1, 1, 1, alpha) gl.glBegin(gl.GL_QUADS) gl.glTexCoord2f(0, 0) gl.glVertex3f(0, 0, -.5) gl.glTexCoord2f(1, 0) gl.glVertex3f(w, 0, -.5) gl.glTexCoord2f(1, 1) gl.glVertex3f(w, h, -.5) gl.glTexCoord2f(0, 1) gl.glVertex3f(0, h, -.5) gl.glEnd() gl.glDisable(gl.GL_TEXTURE_2D) #Draw real thing gl.glColor4f(1, 1, 1, 1) func(*args, **kwargs)
def draw(self): if not self.visible: return if core.getTime() % 1 > 0.6: # Flash every other second return gl.glLineWidth(self.width) rgb = self._desiredRGB gl.glColor4f( rgb[0], rgb[1], rgb[2], self.textbox.opacity ) gl.glBegin(gl.GL_LINES) gl.glVertex2f(self.vertices[0, 0], self.vertices[0, 1]) gl.glVertex2f(self.vertices[1, 0], self.vertices[1, 1]) gl.glEnd()
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) # bar gl.glVertex3f(W, 5 * h, 0) gl.glVertex3f(W, H, 0) gl.glVertex3f(W - 3 * s, H, 0) gl.glVertex3f(W - 3 * s, 5 * h, 0) gl.glVertex3f(3 * s, 5 * h, 0) gl.glVertex3f(3 * s, H, 0) gl.glVertex3f(0, H, 0) gl.glVertex3f(0, 5 * h, 0) # end of bar 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()
def multi_draw(*args, **kwargs): gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_ONE, gl.GL_ONE) gl.glEnable(gl.GL_LINE_SMOOTH) gl.glEnable(gl.GL_POINT_SMOOTH) gl.glClearColor(0, 0, 0, 0) gl.glColor4f(1, 1, 1, 1) gl.glClear(gl.GL_COLOR_BUFFER_BIT) if not buffers: buffers.append(image.get_buffer_manager()) x, y, w, h = buffers[0].get_viewport() #Draw lowres version gl.glViewport(0, 0, 256, 256) func(*args, **kwargs) ctex[0] = buffers[0].get_color_buffer().texture #Lay down copies of lowres version gl.glViewport(x, y, w, h) gl.glClear(gl.GL_COLOR_BUFFER_BIT) gl.glBindTexture(ctex[0].target, ctex[0].id) gl.glEnable(gl.GL_TEXTURE_2D) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR) if not disp_list: disp_list.append(gl.glGenLists(1)) gl.glNewList(disp_list[0], gl.GL_COMPILE) for u in range(-3, 4, 3): for v in range(-3, 4, 3): gl.glColor4f(1, 1, 1, (20.0 - u**2 - v**2) / 72) gl.glBegin(gl.GL_QUADS) gl.glTexCoord2f(0, 0) gl.glVertex3f(u, v, -.5) gl.glTexCoord2f(1, 0) gl.glVertex3f(u + w, v, -.5) gl.glTexCoord2f(1, 1) gl.glVertex3f(u + w, v + h, -.5) gl.glTexCoord2f(0, 1) gl.glVertex3f(u, v + h, -.5) gl.glEnd() gl.glEndList() gl.glCallList(disp_list[0]) gl.glDisable(gl.GL_TEXTURE_2D) #Draw real thing gl.glColor4f(1, 1, 1, 1) func(*args, **kwargs)
def draw(self): try: gl.glPushMatrix() gl.glTranslatef(self.position.x,self.position.y,0) gl.glRotatef(self.angle*180./np.pi,0,0,1) for p in self.primitives: gl.glColor3f(*p.color) gl.glBegin(p.primtype) for vert in p.verts: gl.glVertex2f(*vert) gl.glEnd() gl.glPopMatrix() except Exception,e: time.sleep(0.0001) print(e,traceback.print_exc())
def init_model(self, scene): """ Add the Vertexes and Normals to the compile list. :param scene: The view to render the model to. :type scene: pyglet_helper.objects.View """ # Note that this model is also used by arrow! scene.box_model.gl_compile_begin() gl.glEnable(gl.GL_CULL_FACE) gl.glBegin(gl.GL_TRIANGLES) self.generate_model() gl.glEnd() gl.glDisable(gl.GL_CULL_FACE) scene.box_model.gl_compile_end() self.initialized = True
def draw(): """ Iterate through the calculated vectors and draw them. """ glBegin(GL_LINES) for u in Gvl: for v in u: point = [[v[0][0], v[0][1], v[0][2]], [ v[0][0] + v[1][0], v[0][1] + v[1][1], v[0][2] + v[1][2] ]] draw_arrow(point[0], point[1]) glEnd()
def gl_boat(x, y): # Draw boat shape gl.glBegin(gl.GL_LINES) gl.glColor3f(0.9, 0.9, 0.9) gl.glVertex2f(x, y) gl.glVertex2f(x + boatw, y) gl.glVertex2f(x + boatw, y) gl.glVertex2f(x + boatw, y + 2 * h) gl.glVertex2f(x + boatw, y + 2 * h) gl.glVertex2f(x + boatw / 2, y + 2.5 * h) gl.glVertex2f(x + boatw / 2, y + 2.5 * h) gl.glVertex2f(x, y + 2 * h) gl.glVertex2f(x, y + 2 * h) gl.glVertex2f(x, y) gl.glEnd()
def draw_rectangle(position, size, color=(.0, .0, .0)): x, y = position dx, dy = size if len(color) == 3: gl.glColor3d(*color) else: gl.glColor4d(*color) gl.glBegin(gl.GL_TRIANGLES) gl.glVertex2f(x, y + dy) gl.glVertex2f(x, y) gl.glVertex2f(x + dx, y + dy) gl.glVertex2f(x + dx, y + dy) gl.glVertex2f(x, y) gl.glVertex2f(x + dx, y) gl.glEnd()
def renderBodyPart(self, b): halfside = 4 glColor3f(*colors[1]) for q in b.body.tail: glPushMatrix() glTranslatef(q.x, q.y, 0.0) glBegin(GL_POLYGON) glVertex2f(halfside, halfside) glVertex2f(halfside, -halfside) glVertex2f(-halfside, -halfside) glVertex2f(-halfside, halfside) glEnd() glPopMatrix()
def render_view(self, b): glColor3f(0.6, 0.1, 0.1) glBegin(GL_LINE_LOOP) step = 10 # render a circle for the boid's view for i in range(-b.body.fustrum.angle, b.body.fustrum.angle + step, step): glVertex2f( b.body.fustrum.radius / self.scaleFactor * math.sin(math.radians(i)), (b.body.fustrum.radius / self.scaleFactor * math.cos(math.radians(i)))) #glVertex2f(0.0, 0.0) glEnd()
def paintGL(self): _import_gl() glClear(GL_COLOR_BUFFER_BIT) glColor3f(1.0,1.0,1.0) glLoadIdentity() self.lookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) glScalef(1.0, 2.0, 1.0) glBegin(GL_TRIANGLES) glNormal3f(1,0,0) glVertex3f(1,0,0) glVertex3f(0,1,0) glVertex3f(1,1,0) glEnd() glFlush()
def draw(self): ecsm.draw() if self.paused or self.victory or self.defeat: gl.glColor4f(.2, .2, .2, .4) gl.glBegin(gl.GL_QUADS) gl.glVertex2f(0, 0) gl.glVertex2f(const.WIDTH, 0) gl.glVertex2f(const.WIDTH, const.HEIGHT) gl.glVertex2f(0, const.HEIGHT) gl.glEnd() self.title_label.draw() self.text_label.draw() self.control_label.draw()
def render(self): di = (self.healthbar / 100) if di < 1.0: di = 1.0 gl.glBegin(gl.GL_QUADS) gl.glColor4f(1.0 / di, 1.0 - 1 / di, 0.0, 0.5) gl.glVertex2f(50, 10) gl.glVertex2f(50 + self.healthbar, 10) gl.glVertex2f(50 + self.healthbar, 20) gl.glVertex2f(50, 20) gl.glEnd() for item in self.labels: item.draw()
def render_road(self): gl.glBegin(gl.GL_QUADS) gl.glColor4f(0, 0, 0, 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()
def _draw_box(self, x, y, w, h): gl.glBegin(gl.GL_LINES) gl.glColor3f(0, 1, 0) gl.glVertex2f(x, y) gl.glVertex2f(x, y + h) gl.glColor3f(1, 0, 0) gl.glVertex2f(x, y + h) gl.glVertex2f(x + w, y + h) gl.glColor3f(0, 0, 1) gl.glVertex2f(x + w, y + h) gl.glVertex2f(x + w, y) gl.glColor3f(1, 0, 1) gl.glVertex2f(x + w, y) gl.glVertex2f(x, y) gl.glEnd()
def drawVertex(self, x, y, z=0, v=(), color=(0, 0, 0, 1), stroke=0, rotation=0.0, style=0): gl.glColor4f(*self.color) gl.glPushMatrix() gl.glTranslatef(x, y, -z) gl.glRotatef(self.rotation, 0, 0, 0.1) if self.style: gl.glEnable(gl.GL_LINE_STIPPLE) gl.glLineStipple(1, style) ## else : ## glDisable(GL_LINE_STIPPLE) ## 0xF0F0 # dashed line ## 0xF00F # long dashed line ## 0x8888 # dotted lines ## glRect(x1,y,1,x1,x2) ## glRectiv(v1,v2) # oposite vertex of rectangle # -- start drawing if self.stroke: # outlined polygon gl.glLineWidth(self.stroke) gl.glBegin(gl.GL_LINE_LOOP) else: # filled polygon if len(v) == 4: gl.glBegin(gl.GL_QUADS) elif len(v) > 4: gl.glBegin(gl.GL_POLYGON) else: gl.glBegin( gl.GL_TRIANGLES) # which type of polygon are we drawing? for p in v: gl.glVertex3f(p[0], p[1], 0) # draw each vertex gl.glEnd() # -- end drawing if self.style: gl.glDisable(gl.GL_LINE_STIPPLE) gl.glPopMatrix()
def render_indicators(self, agent_id, 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.cars[agent_id].hull.linearVelocity[0]) \ + np.square(self.cars[agent_id].hull.linearVelocity[1])) vertical_ind(5, 0.02 * true_speed, (1, 1, 1)) vertical_ind(7, 0.01 * self.cars[agent_id].wheels[0].omega, (0.0, 0, 1)) # ABS sensors vertical_ind(8, 0.01 * self.cars[agent_id].wheels[1].omega, (0.0, 0, 1)) vertical_ind(9, 0.01 * self.cars[agent_id].wheels[2].omega, (0.2, 0, 1)) vertical_ind(10, 0.01 * self.cars[agent_id].wheels[3].omega, (0.2, 0, 1)) horiz_ind(20, -10.0 * self.cars[agent_id].wheels[0].joint.angle, (0, 1, 0)) horiz_ind(30, -0.8 * self.cars[agent_id].hull.angularVelocity, (1, 0, 0)) gl.glEnd() self.score_label.text = "%04i" % self.reward[agent_id] self.score_label.draw() # Render backwards flag if driving backward and backwards flag render is enabled if self.driving_backward[agent_id] and self.backwards_flag: pyglet.graphics.draw(3, gl.GL_TRIANGLES, ('v2i', (W - 100, 30, W - 75, 70, W - 50, 30)), ('c3B', (0, 0, 255) * 3))