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 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 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 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 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 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 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 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 _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_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 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 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 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(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 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 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(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): 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_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 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 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 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 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 draw_red_square(): gl.glColor3ub(127, 0, 0) gl.glBegin(gl.GL_QUADS) gl.glVertex2f(-10, -10) gl.glVertex2f(10, -10) gl.glVertex2f(10, 10) gl.glVertex2f(-10, 10) 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 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 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_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): self.label.draw() gl.glBegin(pyglet.gl.GL_TRIANGLES) gl.glVertex2f(0, 0) gl.glVertex2f(100, 0) gl.glVertex2f(100, 200) gl.glEnd()
def gl_arrow(x, y, angle, length, color=(0.9, 0.9, 0.9)): L = 50 T = np.clip(7 * length, 0, 7) hx, hy = x + length * L * cos(angle), y + length * L * sin(angle) gl.glEnable(gl.GL_LINE_SMOOTH) gl.glLineWidth(2) gl.glBegin(gl.GL_LINES) gl.glColor3f(*color) gl.glVertex2f(x, y) gl.glVertex2f(hx, hy) gl.glEnd() gl.glBegin(gl.GL_TRIANGLES) gl.glVertex2f(hx + T * cos(angle), hy + T * sin(angle)) gl.glVertex2f(hx + T * cos(angle + 2 * pi / 3), hy + T * sin(angle + 2 * pi / 3)) gl.glVertex2f(hx + T * cos(angle + 4 * pi / 3), hy + T * sin(angle + 4 * pi / 3)) gl.glEnd()
def on_draw(): window.clear() # Draw texture backed by ImageSurface gl.glEnable(gl.GL_TEXTURE_2D) gl.glBindTexture(gl.GL_TEXTURE_2D, texture.id) gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA, width, height, 0, gl.GL_BGRA, gl.GL_UNSIGNED_BYTE, surface_data) gl.glBegin(gl.GL_QUADS) gl.glTexCoord2f(0.0, 1.0) gl.glVertex2i(0, 0) gl.glTexCoord2f(1.0, 1.0) gl.glVertex2i(width, 0) gl.glTexCoord2f(1.0, 0.0) gl.glVertex2i(width, height) gl.glTexCoord2f(0.0, 0.0) gl.glVertex2i(0, height) gl.glEnd()
def draw_poly(shape, color): """Draw a rectangle using OpenGL based on the provided spacial information. Keyword arguments: vertices -- points on the rectangle presented in clockwise order position -- x and y position to start the operation angle -- draw angle in radians (default 0) """ vertices = shape.verts position = shape.body.position angle = shape.body.angle gl.glPushMatrix() gl.glColor3f(color[0], color[1], color[2]) gl.glTranslatef(position[0], position[1], 0) gl.glRotatef(angle * 57.3, 0, 0, 1) gl.glBegin(gl.GL_TRIANGLE_STRIP) for vertex in vertices[:2] + vertices[:1:-1]: gl.glVertex2f(vertex[0], vertex[1]) gl.glEnd() gl.glPopMatrix()
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()
def _draw_pipes(x, spacing, height, delta=80): """Draw the pair of pipes.""" GL.glPushMatrix() GL.glColor4f(1., 1., 1., 1.) GL.glBegin(GL.GL_QUADS) GL.glVertex2f(x, 0) GL.glVertex2f(x + delta, 0) GL.glVertex2f(x + delta, height - 2 * spacing) GL.glVertex2f(x, height - 2 * spacing) GL.glEnd() GL.glBegin(GL.GL_QUADS) GL.glVertex2f(x, height) GL.glVertex2f(x, height - spacing) GL.glVertex2f(x + delta, height - spacing) GL.glVertex2f(x + delta, height) GL.glEnd() GL.glPopMatrix()
def nakresli_obdelnik(x1, y1, x2, y2): """Nakresli obdelnik na dane souradnice Nazorny diagram:: y2 - +-----+ |/////| y1 - +-----+ : : x1 x2 """ # Tady pouzijeme volani OpenGL, ktere je pro nas zatim asi nejjednodussi # na pouziti gl.glBegin(gl.GL_TRIANGLE_FAN) # zacni kreslit spojene trojuhelniky gl.glVertex2f(int(x1), int(y1)) # vrchol A gl.glVertex2f(int(x1), int(y2)) # vrchol B gl.glVertex2f(int(x2), int(y2)) # vrchol C, nakresli trojuhelnik ABC gl.glVertex2f(int(x2), int(y1)) # vrchol D, nakresli trojuhelnik BCD # dalsi souradnice E by nakreslila trojuhelnik CDE, atd. gl.glEnd() # ukonci kresleni trojuhelniku
def _render_indicators(self, W, H): 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) s = W/4 #horizontal slot separation #h = H_INDI #vertical pixels definition h = H / 40.0 #black bar, 5x h height gl.glBegin(gl.GL_QUADS) 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) #3 hor indicators 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)) horiz_ind(1.0, 0.015*true_speed, (1,1,1)) horiz_ind(2.5, -1*self.car.wheels[0].joint.angle, (0,1,0)) horiz_ind(3.5, np.clip(-0.03*self.car.hull.angularVelocity,-0.4,0.4), (1,1,0)) #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)) gl.glEnd() #total_reward self.score_label.text = "%02.1f" % self.ep_return self.score_label.draw()
def draw(self): gl.glLineWidth(1) rgb = mcol.hsv_to_rgb(self.hsv) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glEnable(gl.GL_BLEND) gl.glColor4f(rgb[0], rgb[1], rgb[2], self.alpha) gl.glBegin(gl.GL_LINE_STRIP) if self.camera == None: for i in range(self.endPoint-self.startPoint): gl.glVertex2f(self.vts[0, self.startPoint+i], self.vts[1, self.startPoint+i]) gl.glEnd() else: self.vtsC = self.camera.project(self.vts) for i in range(self.endPoint-self.startPoint): id = self.endPoint-i-1 self.signals[id] = self.signals[max(self.startPoint, id-self.propagationVel)] self.color[:, id] = self.color[:, max(self.startPoint, id-self.propagationVel)] col = mcol.hsv_to_rgb([self.color[0, id], self.color[1, id], self.signals[id]])#self.color[2, id] + self.signal2colorV(self.signals[id])]) gl.glColor4f(col[0], col[1], col[2], self.color[3, id]) gl.glVertex2f(self.vtsC[0, id], self.vtsC[1, id]) gl.glEnd()
def line(color, start, end, width=1, endcolor=None): """Draw a line segment :Parameters: `color` : tuple The colour to use, as a 3- or 4-tuple of floats `start` : tuple The co-ordinates of the start point of the line segment `end` : tuple The co-ordinates of the end point of the line segment `width` : float The line width. Defaults to 1.0 """ _set_gl_color(color) gl.glLineWidth(width) gl.glBegin(gl.GL_LINES) gl.glVertex2f(*start) if endcolor: _set_gl_color(endcolor) gl.glVertex2f(*end) gl.glEnd()
def circle(x, y, radius): """ We want a pixel perfect circle. To get one, we have to approximate it densely with triangles. Each triangle thinner than a pixel is enough to do it. Sin and cosine are calculated once and then used repeatedly to rotate the vector. I dropped 10 iterations intentionally for fun. """ iterations = int(2*radius*math.pi) s = math.sin(2*math.pi / iterations) c = math.cos(2*math.pi / iterations) dx, dy = radius, 0 glBegin(GL_TRIANGLE_FAN) glVertex2f(x, y) for i in range(iterations+1): glVertex2f(x+dx, y+dy) dx, dy = (dx*c - dy*s), (dy*c + dx*s) glEnd()
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()
def _render_background(self): # back_image = pyglet.resource.image('background.jpeg') # back_image.blit(0,0) # image = pyglet.image.load("background.jpeg") # background_sprite = pyglet.sprite.Sprite(image) # background_sprite.draw() gl.glBegin(gl.GL_QUADS) gl.glColor4f(0.9, 0.9, 0.9, 0.9) 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()
def on_draw(): window.clear() gl.glEnable(gl.GL_TEXTURE_2D) gl.glBindTexture(gl.GL_TEXTURE_2D, texture.id) gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA, WIDTH, HEIGHT, 0, gl.GL_BGRA, gl.GL_UNSIGNED_BYTE, data) gl.glBegin(gl.GL_QUADS) gl.glTexCoord2f(0.0, 1.0) gl.glVertex2i(0, 0) gl.glTexCoord2f(1.0, 1.0) gl.glVertex2i(WIDTH, 0) gl.glTexCoord2f(1.0, 0.0) gl.glVertex2i(WIDTH, HEIGHT) gl.glTexCoord2f(0.0, 0.0) gl.glVertex2i(0, HEIGHT) gl.glEnd() text.draw()
def run(self): while not self.has_exit: 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() self.flip()
def render_elevators(self): elevator_width = 50 gl.glBegin(gl.GL_QUADS) for i in range(self.elevator_num): current_floor = self.state[i] - 1 gl.glColor4f(0.3, 0.3, 0.3, 1) gl.glVertex3f( self.screen_width / 2 + (elevator_width * (i + 1)) + (i * 10), 50 + self.floor_padding * current_floor, 0) gl.glVertex3f( self.screen_width / 2 + (elevator_width * (i + 1)) + (i * 10), 50 + self.floor_padding * current_floor + self.floor_padding, 0) gl.glVertex3f( self.screen_width / 2 + (elevator_width * i) + (i * 10), 50 + self.floor_padding * current_floor + self.floor_padding, 0) gl.glVertex3f( self.screen_width / 2 + (elevator_width * i) + (i * 10), 50 + self.floor_padding * current_floor, 0) 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) 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) for i, car in enumerate(self.car): true_speed = np.sqrt( np.square(car.hull.linearVelocity[0]) + np.square(car.hull.linearVelocity[1])) vertical_ind(5, 0.02 * true_speed, (1, 1, 1)) vertical_ind(7, 0.01 * car.wheels[0].omega, (0.0, 0, 1)) # ABS sensors vertical_ind(8, 0.01 * car.wheels[1].omega, (0.0, 0, 1)) vertical_ind(9, 0.01 * car.wheels[2].omega, (0.2, 0, 1)) vertical_ind(10, 0.01 * car.wheels[3].omega, (0.2, 0, 1)) horiz_ind(20, -10.0 * car.wheels[0].joint.angle, (0, 1, 0)) horiz_ind(30, -0.8 * car.hull.angularVelocity, (1, 0, 0)) gl.glEnd() self.score_label.text = "%04i" % self.reward self.score_label.draw() '''
def level_draw(self, camera): gl.glLoadIdentity() gl.glBegin(gl.GL_TRIANGLES) gl.glColor3f(1.0, 1.0, 1.0) secs = 0 necs = 0 for sector in self.sectors: if camera.inCamera(sector.x, sector.y, sector.w, sector.h): secs += 1 for node in sector.nodes: necs += 1 if node.e4: node2 = node.e4.end if node2.e6: node3 = node2.e6.end gl.glColor3f(*TriNode.color[node.mat]) gl.glVertex2f(node.vx - camera.x, node.vy - camera.y) gl.glColor3f(*TriNode.color[node2.mat]) gl.glVertex2f(node2.vx - camera.x, node2.vy - camera.y) gl.glColor3f(*TriNode.color[node3.mat]) gl.glVertex2f(node3.vx - camera.x, node3.vy - camera.y) if node.e5: node2 = node.e5.end if node2.e3: node3 = node2.e3.start gl.glColor3f(*TriNode.color[node.mat]) gl.glVertex2f(node.vx - camera.x, node.vy - camera.y) gl.glColor3f(*TriNode.color[node2.mat]) gl.glVertex2f(node2.vx - camera.x, node2.vy - camera.y) gl.glColor3f(*TriNode.color[node3.mat]) gl.glVertex2f(node3.vx - camera.x, node3.vy - camera.y) gl.glEnd() #print(secs, necs) gl.glBegin(gl.GL_LINES) gl.glColor3f(1.0, 1.0, 1.0) for sector in self.sectors: if camera.inCamera(sector.x, sector.y, sector.w, sector.h): for node in sector.nodes: for edge in node.leftEdgeList(): gl.glVertex2f(edge.start.vx - camera.x, edge.start.vy - camera.y) gl.glVertex2f(edge.end.vx - camera.x, edge.end.vy - camera.y) gl.glEnd() if self.selected: s = self.selected sz = 4 gl.glBegin(gl.GL_QUADS) gl.glColor3f(1.0, 0.0, 0.0) gl.glVertex2d(s.vx - sz - camera.x, s.vy - sz - camera.y) gl.glVertex2d(s.vx - sz - camera.x, s.vy + sz - camera.y) gl.glVertex2d(s.vx + sz - camera.x, s.vy + sz - camera.y) gl.glVertex2d(s.vx + sz - camera.x, s.vy - sz - camera.y) gl.glEnd()
def draw_circle(self, radius=10, res=30, pos_x=0, pos_y=0, color=(1.0, 1.0, 1.0, 1.0), **attrs): points = [] # window start indexing from bottom left x = self.img_height - pos_x y = pos_y for i in range(res): ang = 2 * math.pi * i / res points.append( (math.cos(ang) * radius + y, math.sin(ang) * radius + x)) # draw filled polygon if len(points) == 4: glBegin(GL_QUADS) elif len(points) > 4: glBegin(GL_POLYGON) else: glBegin(GL_TRIANGLES) for p in points: # choose color glColor4f(color[0], color[1], color[2], color[3]) glVertex3f(p[0], p[1], 0) # draw each vertex glEnd() # reset color glColor4f(1.0, 1.0, 1.0, 1.0)
def render_over(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 core_draw(self): super(PointDisplayCanvas, self).core_draw() points,point_colors, linesegs,lineseg_colors = self.extra_points_linesegs gl.glColor4f(0.0,1.0,0.0,1.0) # green point if points is not None: if point_colors is None: point_colors = [ (0,1,0,1) ] * len(points) gl.glBegin(gl.GL_POINTS) for color_4tuple,pt in zip(point_colors,points): gl.glColor4f(*color_4tuple) gl.glVertex2f(pt[0],pt[1]) gl.glEnd() if linesegs is not None: if lineseg_colors is None: lineseg_colors = [ (0,1,0,1) ] * len(linesegs) for color_4tuple,this_lineseg in zip(lineseg_colors,linesegs): gl.glBegin(gl.GL_LINE_STRIP) gl.glColor4f(*color_4tuple) for (x,y) in zip(this_lineseg[0::2],this_lineseg[1::2]): gl.glVertex2f(x,y) gl.glEnd() if self.red_points is not None: gl.glColor4f(1.0,0.0,0.0,0.5) # 50% alpha gl.glBegin(gl.GL_POINTS) for pt in self.red_points: gl.glVertex2f(pt[0],pt[1]) gl.glEnd() gl.glColor4f(1.0,1.0,1.0,1.0) # restore white color
def render_textured(self): gl.glEnable(GL_TEXTURE_2D) gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) gl.glBindTexture(GL_TEXTURE_2D, self.texture.id) gl.glBegin(gl.GL_QUADS) gl.glColor4f(1, 1, 1, 1) gl.glTexCoord2f(-TEXTURE_SCALE, TEXTURE_SCALE) gl.glVertex3f(-PLAYFIELD, +PLAYFIELD, 0) gl.glTexCoord2f(TEXTURE_SCALE, TEXTURE_SCALE) gl.glVertex3f(+PLAYFIELD, +PLAYFIELD, 0) gl.glTexCoord2f(TEXTURE_SCALE, -TEXTURE_SCALE) gl.glVertex3f(+PLAYFIELD, -PLAYFIELD, 0) gl.glTexCoord2f(-TEXTURE_SCALE, -TEXTURE_SCALE) gl.glVertex3f(-PLAYFIELD, -PLAYFIELD, 0) gl.glEnd() gl.glDisable(GL_TEXTURE_2D) gl.glEnable(GL_TEXTURE_2D) gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) gl.glBindTexture(GL_TEXTURE_2D, self.road_texture.id) gl.glBegin(gl.GL_QUADS) k = PLAYFIELD / 20.0 tile_count = len(self.road_poly) tile_corners = ((-1, -1), (1, -1), (1, 1), (-1, 1)) for i in range(len(self.road_poly)): poly, color = self.road_poly[i] gl.glColor4f(color[0], color[1], color[2], 1) for j in range(len(poly)): p = poly[j] gl.glTexCoord2f(*tile_corners[j % 4]) gl.glVertex3f(p[0], p[1], 0) gl.glEnd() gl.glDisable(GL_TEXTURE_2D)
def _circle(self, point, r, color, filled): c = 2 * r * pi iterations = int(c / 2) s = sin(2 * pi / iterations) c = cos(2 * pi / iterations) dx, dy = r, 0 glColor4f(*color) if filled: glBegin(GL_TRIANGLE_FAN) glVertex2f(point.x, point.y) else: glLineWidth(self._line_width) glBegin(GL_LINE_LOOP) glVertex2f(point.x + dx, point.y + dy) for i in range(int(iterations) + 1): glVertex2f(point.x + dx, point.y + dy) dx, dy = (dx * c - dy * s), (dy * c + dx * s) glEnd()
def draw(self): """ Black magic. """ # draw the rect if not self.visible: # lol, don't do anything if not visible. return gl.glLoadIdentity() gl.glBegin(gl.GL_QUADS) gl.glColor3f(*self.color) gl.glVertex2f(self.left, self.top) gl.glVertex2f(self.right, self.top) if self.gradient: gl.glColor3f(*self.minColor) gl.glVertex2f(self.right, self.bottom) gl.glVertex2f(self.left, self.bottom) gl.glEnd()
def plot_box_label_yml(yml_dict, img_w, img_h): for key, value in yml_dict.items(): # the key will print a label print(key) i = 0 for coord in value: if len(coord) is 4: top_left_x_img, top_left_y_img, width, height = coord # Image coordinates # Plot (change coord for openGL), (0,0 is bottom left corner) top_left_x = top_left_x_img top_left_y = img_h - top_left_y_img bottom_left_x = top_left_x bottom_left_y = top_left_y - height plot_box(bottom_left_x=bottom_left_x, bottom_left_y=bottom_left_y, width=width, height=height) label = pyglet.text.Label(key + '_' + str(i), font_name='Times New Roman', font_size=8, x=top_left_x, y=top_left_y + 2, anchor_x='left', anchor_y='bottom') label.draw() gl.glColor4f(*(1, 0, 1, 0.2)) x, y = bottom_left_x, bottom_left_y w = x + width h = y + height 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) i = i + 1
def on_draw(): window.clear() start_button_sprite.scale_x = 0.75 start_button_sprite.scale_y = 0.25 start_button_sprite.draw() reset_button_sprite.scale_x = 0.75 reset_button_sprite.scale_y = 0.25 reset_button_sprite.draw() timeLabelAC3.draw() timeLabelBacktracking.draw() start_label.draw() reset_label.draw() sudoku_label.draw() for i in labels: i.draw() for i in range(1, 10): if i % 3 == 0: glLineWidth(7) else: glLineWidth(1) glBegin(GL_LINES) glVertex2f(0, h_one_third * i // 3) glVertex2f(window.width - w_one_fourth, h_one_third * i // 3) glEnd() for i in range(1, 10): if i % 3 == 0: glLineWidth(7) else: glLineWidth(1) glBegin(GL_LINES) glVertex2f(w_one_fourth * i // 3, 0) glVertex2f(w_one_fourth * i // 3, window.height) glEnd()
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) #ORR data = [val[0] for val in self.road_poly if type(val[1]) == list] inside = [info[0] for info in data] outside = [info[1] for info in data] for obj in inside: gl.glColor4f(0, 0, 1, 1) gl.glVertex3f(obj[0] + 0.5, obj[1] + 0.5, 0) gl.glVertex3f(obj[0] + 0.5, obj[1] - 0.5, 0) gl.glVertex3f(obj[0] - 0.5, obj[1] + 0.5, 0) gl.glVertex3f(obj[0] - 0.5, obj[1] - 0.5, 0) for obj in outside: #rgb(255,255,0) gl.glColor4f(1, 1, 0, 1) gl.glVertex3f(obj[0] + 0.5, obj[1] + 0.5, 0) gl.glVertex3f(obj[0] + 0.5, obj[1] - 0.5, 0) gl.glVertex3f(obj[0] - 0.5, obj[1] + 0.5, 0) gl.glVertex3f(obj[0] - 0.5, obj[1] - 0.5, 0) gl.glEnd()
def _textgrid_lines_gl(self): if self._line_color: if not self._gridlines_dlist: dl_index = glGenLists(1) glNewList(dl_index, GL_COMPILE) glLineWidth(self._line_width) glColor4f(*self._text_box._toRGBA(self._line_color)) glBegin(GL_LINES) for x in self._col_lines: for y in self._row_lines: if x == 0: glVertex2i(x, y) glVertex2i(int(self._size[0]), y) if y == 0: glVertex2i(x, y) glVertex2i(x, int(-self._size[1])) glEnd() glColor4f(0.0, 0.0, 0.0, 1.0) glEndList() self._gridlines_dlist = dl_index glCallList(self._gridlines_dlist)
def polygon(color, pointlist, width=0): """Draw a closed polygon :Parameters: `color` : tuple The colour to use, as a 3- or 4-tuple of floats `pointlist` : list An list of (x,y) tuples for vertices of the polygon `width` : float The line width for the polygon. If zero, polygon 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_POLYGON) for p in pointlist: gl.glVertex2f(*p) 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 createDisplayLists(self): glyph_count = len(self.charcode2unichr) max_tile_width = self.max_tile_width max_tile_height = self.max_tile_height display_lists_for_chars = {} base = glGenLists(glyph_count) for i, (charcode, glyph) in enumerate(self.charcode2glyph.items()): dl_index = base + i uchar = self.charcode2unichr[charcode] # update tex coords to reflect earlier resize of atlas height. gx1, gy1, gx2, gy2 = glyph['texcoords'] gx1 = gx1 / float(self.atlas.width) gy1 = gy1 / float(self.atlas.height) gx2 = gx2 / float(self.atlas.width) gy2 = gy2 / float(self.atlas.height) glyph['texcoords'] = [gx1, gy1, gx2, gy2] glNewList(dl_index, GL_COMPILE) if uchar not in [u'\t', u'\n']: glBegin(GL_QUADS) x1 = glyph['offset'][0] x2 = x1 + glyph['size'][0] y1 = (self.max_ascender - glyph['offset'][1]) y2 = y1 + glyph['size'][1] glTexCoord2f(gx1, gy2), glVertex2f(x1, -y2) glTexCoord2f(gx1, gy1), glVertex2f(x1, -y1) glTexCoord2f(gx2, gy1), glVertex2f(x2, -y1) glTexCoord2f(gx2, gy2), glVertex2f(x2, -y2) glEnd() glTranslatef(max_tile_width, 0, 0) glEndList() display_lists_for_chars[charcode] = dl_index self.charcode2displaylist = display_lists_for_chars