def draw(self): with self.style: VG.set(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE) old_matrix = VG.get_matrix() VG.translate(*self.body.position) VG.rotate(math.degrees(self.body.angle)) self.path.draw(VG_STROKE_PATH) VG.load_matrix(old_matrix)
def draw(self): mat = VG.get_matrix() VG.translate(*self.chassis.body.position) VG.rotate(math.degrees(self.chassis.body.angle)) ## self.chassis_path.draw(VG_STROKE_PATH | VG_FILL_PATH) VG.load_matrix(mat) p = VG.Path() points = self.chassis.get_points() p.move_to(points[0]+self.chassis.offset) for point in points[1:]: p.move_to(point+self.chassis.offset) p.close() p.style = VG.Style(fill_paint=VG.ColorPaint((1.0, 0.0,0.0))) p.draw(VG_FILL_PATH) VG.load_matrix(mat) VG.translate(*self.wheel1.body.position) VG.rotate(math.degrees(self.wheel1.body.angle)) self.wheel_path.draw(VG_STROKE_PATH | VG_FILL_PATH) VG.load_matrix(mat) VG.translate(*self.wheel2.body.position) VG.rotate(math.degrees(self.wheel2.body.angle)) self.wheel_path.draw(VG_STROKE_PATH | VG_FILL_PATH) VG.load_matrix(mat)
def build_path(self, string, index, ctp): ctp_x, ctp_y = ctp path = VG.Path() vertical = False with VG.push([1,0,0,0,1,0,0,0,1]): #TODO: vertical and bidi text. last_glyph = None for char in string: ctp_x, ctp_y = self._transform(ctp_x, ctp_y, index) glyph = self.font.get_glyph(char) subpath = self.font.get_path_for_glyph(glyph) #Flip it. with VG.push([1,0,0,0,-1,0,0,0,1]): subpath = subpath.transform() VG.translate(ctp_x, ctp_y) if self.font.face.has_kerning and last_glyph is not None: kerning = self.font.face.get_kerning(last_glyph.index, glyph.index, FT_KERNING_UNSCALED) VG.translate(self.font.scale*kerning[0], self.font.scale*kerning[1]) ctp_x += self.font.scale*kerning[0] ctp_y += self.font.scale*kerning[1] if index < len(self.trot)-1: VG.translate(ctp_x, ctp_y) VG.rotate(self.rotate[index]) VG.translate(-ctp_x, -ctp_y) last_glyph = glyph subpath.transform(path) ctp_x += self.font.scale*glyph.advance[0] ctp_y += self.font.scale*glyph.advance[1]*vertical VG.load_matrix([1,0,0,0,1,0,0,0,1]) return path, ctp
def rotate_about(p, angle): VG.translate(p[0], p[1]) VG.rotate(angle) VG.translate(-p[0], -p[1])
def main(width, height): pygame.init() pygame.display.gl_set_attribute(pygame.GL_STENCIL_SIZE, 2) pygame.display.gl_set_attribute(pygame.GL_MULTISAMPLEBUFFERS, 1) pygame.display.gl_set_attribute(pygame.GL_MULTISAMPLESAMPLES, 4) screen = pygame.display.set_mode((width, height), pygame.OPENGL | pygame.DOUBLEBUF) pygame.display.set_caption("Flower test") VG.create_context((width, height)) VG.set(VG_CLEAR_COLOR, (0.0, 0.0, 0.0, 1.0)) vera = Font("data/fonts/Vera.ttf", 32) message = vera.build_path("Hold down LMB to create flowers") message.style = VG.Style(fill_paint=VG.ColorPaint((1.0, 1.0, 1.0, 0.7))) doc = parse_svg("data/svg/flower.svg") flower = doc.getroot() (x,y), (w,h) = flower.bounds() cx,cy = x+w/2.0, y+h/2.0 particles = [] to_remove = [] clock = pygame.time.Clock() dt = 0 running = True while running: for e in pygame.event.get(): if e.type == pygame.QUIT: running = False elif e.type == pygame.KEYDOWN: if e.key == pygame.K_ESCAPE: running = False if pygame.mouse.get_pressed()[0]: x,y = pygame.mouse.get_pos() m = random.randint(-60, 60) angle = random.randint(0,359) pos = (x-cx, height-y-cy) vel = (m*math.cos(math.radians(angle)), m*math.sin(math.radians(angle))) rot = random.randint(-30, 30) scale = random.randint(8, 12)/10.0 particles.append([pos, vel, random.randint(0,359), rot, scale]) VG.clear((0,0), (width, height)) for particle in particles: VG.load_identity() pos, vel, angle, rot, scale = particle VG.translate(pos[0]+cx, pos[1]+cy) VG.scale(scale, scale) VG.rotate(angle) VG.translate(-cx, -cy) flower.draw() particle[0] = (pos[0]+vel[0]*dt/1000.0,pos[1]+vel[1]*dt/1000.0) particle[2] += rot * dt/1000.0 if particle[0][0] + w < 0 or particle[0][1] + h < 0: to_remove.append(particle) elif particle[0][0] - w > width or particle[0][1] - h > height: to_remove.append(particle) particles = [particle for particle in particles if particle not in to_remove] del to_remove[:] VG.load_identity() message.draw(VG_FILL_PATH) dt = clock.tick(30) pygame.display.flip()
def render(self): if pe.vg_mode == 1: smack = 1 / 2048.0 glScale(smack, smack, 1.0) # VG.set(VG_STROKE_JOIN_STYLE, VG_JOIN_ROUND) VG.set(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE) VG.load_identity() VG.scale(1.0 / smack, 1.0 / smack) p = VG.Path() p.move_to((0, 0)) VG.set(VG_STROKE_LINE_WIDTH, 0.01) # VGU.rect(p, (0,0), (pe.q1,pe.q2)) # VGU.rect(p, (0,0), (x,y)) # VGU.arc(p, (0,0), (pe.q1,pe.q2), # 0, 2*pi, # 0xF100) # VGU.arc(p, (0,0), (.5,.5), 0, 90, 0xF100) # r = pe.q1 # p.move_to((0,r)) # p.arc_to((r,0), r, r, 0, False, False) # paint = VG.ColorPaint((0.0, 1.0, 1.0, 1.0)) # VG.set_paint(paint, VG_STROKE_PATH) # p.draw(VG_STROKE_PATH); # self.wheel(NUM_SEGMENTS, DUTY_CYCLE, OUTER_RADIUS, INNER_RADIUS, LINE_WIDTH, # 0, 1, 1) RADIUS = 0.2 * (sin(pe.time) + 1.0) / 2.0 * 3.0 COUNT = 10 SIZE = 0.2 BIAS = pe.time / COUNT * 2 * pi for i in range(0, COUNT): saved = VG.get_matrix() angle = 2 * pi * (float(i) / COUNT) + BIAS VG.translate(RADIUS * cos(angle), RADIUS * sin(angle)) VG.scale(SIZE, SIZE) VG.rotate(pe.time * 2 * pi * 10.0 + float(i) / COUNT * 2 * pi * 2.0) self.wheel(5, DUTY_CYCLE, OUTER_RADIUS, INNER_RADIUS, LINE_WIDTH, float(COUNT), 1, 1) VG.load_matrix(saved) return GRID_X = 5 GRID_Y = 5 idx = 0 seed(int(pe.time)) step = pe.time - int(pe.time) phase = sin(step * pi) for j in range(0, GRID_Y): for i in range(0, GRID_X): saved = VG.get_matrix() VG.translate((i + 0.5) / GRID_X * 2 - 1, (j + 0.5) / GRID_Y * 2 - 1) VG.scale(2.0 / GRID_X, 2.0 / GRID_Y) # if random() < .1: # VG.scale(1.0+phase,1.0+phase) VG.scale(pe.bass + 1.0, pe.treb + 1.0) VG.rotate(float(i) / GRID_X * 360.0 * pe.time) # awesome # VG.translate(sin(pe.time+i/GRID_Y),0) segments = min(j + 1, GRID_Y - j) flag = True if ((j + 2) % (i + 2)) and ((i + 2) % (j + 2)) else False self.wheel( segments, DUTY_CYCLE, OUTER_RADIUS, INNER_RADIUS, LINE_WIDTH, float(i) / GRID_X, sin(float(i) / GRID_X * pi) + sin(pe.time) * 2 * pi, 1, ) VG.load_matrix(saved)