|
| 1 | +load_library :glvideo |
| 2 | +include_package 'gohai.glvideo' |
| 3 | + |
| 4 | +# placeholder for vector coordinates |
| 5 | +Vect = Struct.new(:x, :y) |
| 6 | +# placeholder for image sources |
| 7 | +ImageSource = Struct.new(:video, :image) |
| 8 | + |
| 9 | +attr_reader :sources, :sel, :video, :corners, :quads, :last_mouse_move, :sel_corner |
| 10 | +RES = 5 # number of subdivisions (e.g. 5 x 5) |
| 11 | + |
| 12 | +def setup |
| 13 | + @last_mouse_move = 0 |
| 14 | + @sel_corner = -1 |
| 15 | + no_cursor |
| 16 | + @video = GLMovie.new(self, data_path('launch2.mp4')) |
| 17 | + video.loop |
| 18 | + image = load_image(data_path('checkerboard.png')) |
| 19 | + @sources = ImageSource.new(video, image) |
| 20 | + @sel = sources.video |
| 21 | + @corners = [ |
| 22 | + Vect.new(width / 2 - 100, height / 2 - 100), |
| 23 | + Vect.new(width / 2 + 100, height / 2 - 100), |
| 24 | + Vect.new(width / 2 + 100, height / 2 + 100), |
| 25 | + Vect.new(width / 2 - 100, height / 2 + 100) |
| 26 | + ] |
| 27 | + @quads = create_mesh(sel, corners, RES) |
| 28 | +end |
| 29 | + |
| 30 | +def draw |
| 31 | + background(0) |
| 32 | + video.read if (sel.respond_to?(:read) && video.available) |
| 33 | + # regenerate mesh if we're dragging a corner |
| 34 | + if (sel_corner != -1 && (pmouse_x != mouse_x || pmouse_y != mouse_y)) |
| 35 | + corners[sel_corner] = Vect.new(mouse_x, mouse_y) |
| 36 | + # this improves performance, but will be replaced by a |
| 37 | + # more elegant way in a future release |
| 38 | + @quads = [] |
| 39 | + Java::JavaLang::System.gc # this is generally not recommended |
| 40 | + @quads = create_mesh(sel, corners, RES) |
| 41 | + end |
| 42 | + |
| 43 | + # display |
| 44 | + quads.map { |quad| shape(quad) } if !quads.empty? |
| 45 | + # hide the mouse cursor after two seconds |
| 46 | + if (pmouse_x != mouse_x || pmouse_y != mouse_y) |
| 47 | + cursor |
| 48 | + @last_mouse_move = millis |
| 49 | + elsif (!last_mouse_move.zero? && 2000 < millis - last_mouse_move) |
| 50 | + no_cursor |
| 51 | + @last_mouse_move = 0 |
| 52 | + end |
| 53 | +end |
| 54 | + |
| 55 | +def mouse_pressed |
| 56 | + corners.each_with_index do |corner, i| |
| 57 | + return @sel_corner = i if dist(mouse_x, mouse_y, corner.x, corner.y) < 20 |
| 58 | + end |
| 59 | + # no corner? then switch texture |
| 60 | + @sel = sel.respond_to?(:loop) ? sources.image : sources.video |
| 61 | + quads = create_mesh(sel, corners, RES) |
| 62 | +end |
| 63 | + |
| 64 | +def mouse_released |
| 65 | + @sel_corner = -1 |
| 66 | +end |
| 67 | + |
| 68 | +def create_mesh(tex, corners, res) |
| 69 | + transform = PerspectiveTransform.get_quad_to_quad( |
| 70 | + 0, 0, tex.width, 0, # top left, top right |
| 71 | + tex.width, tex.height, 0, tex.height, # bottom right, bottom left |
| 72 | + corners[0].x, corners[0].y, corners[1].x, corners[1].y, |
| 73 | + corners[2].x, corners[2].y, corners[3].x, corners[3].y |
| 74 | + ) |
| 75 | + warp_perspective = WarpPerspective.new(transform) |
| 76 | + x_step = tex.width.to_f / res |
| 77 | + y_step = tex.height.to_f / res |
| 78 | + quads = [] |
| 79 | + (0...res).each do |y| |
| 80 | + (0...res).each do |x| |
| 81 | + texture_mode(NORMAL) |
| 82 | + sh = create_shape |
| 83 | + sh.begin_shape(QUAD) |
| 84 | + sh.no_stroke |
| 85 | + sh.texture(tex) |
| 86 | + sh.normal(0, 0, 1) |
| 87 | + point = warp_perspective.map_dest_point(x * x_step, y * y_step) |
| 88 | + sh.vertex(point.get_x.to_f, point.get_y.to_f, 0, x.to_f / res, y.to_f / res) |
| 89 | + point = warp_perspective.map_dest_point((x+1) * x_step, y * y_step) |
| 90 | + sh.vertex(point.get_x.to_f, point.get_y.to_f, 0, (x + 1).to_f / res, y.to_f / res) |
| 91 | + point = warp_perspective.map_dest_point((x+1) * x_step, (y + 1) * y_step) |
| 92 | + sh.vertex(point.get_x.to_f, point.get_y.to_f, 0, (x + 1).to_f / res, (y + 1).to_f / res) |
| 93 | + point = warp_perspective.map_dest_point(x * x_step, (y + 1) * y_step) |
| 94 | + sh.vertex(point.get_x.to_f, point.get_y.to_f, 0, x.to_f / res, (y + 1).to_f / res) |
| 95 | + sh.end_shape |
| 96 | + quads[y * res + x] = sh |
| 97 | + end |
| 98 | + end |
| 99 | + quads |
| 100 | +end |
| 101 | + |
| 102 | +def settings |
| 103 | + full_screen(P2D) |
| 104 | +end |
0 commit comments