1- load_library :glvideo
1+ load_libraries :glvideo , :corners
22include_package 'gohai.glvideo'
33
4- # placeholder for vector coordinates
5- Vect = Struct . new ( :x , :y )
64# placeholder for image sources
75ImageSource = Struct . new ( :video , :image )
86
9- attr_reader :sources , :sel , :video , :corners , :quads , :last_mouse_move , :sel_corner
7+ attr_reader :sources , :sel , :video , :corners , :quads , :last_mouse_move
108RES = 5 # number of subdivisions (e.g. 5 x 5)
119
1210def setup
1311 @last_mouse_move = 0
14- @sel_corner = -1
1512 no_cursor
1613 @video = GLMovie . new ( self , data_path ( 'launch2.mp4' ) )
1714 video . loop
1815 image = load_image ( data_path ( 'checkerboard.png' ) )
1916 @sources = ImageSource . new ( video , image )
2017 @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- ]
18+ @corners = Corners . new ( width , height , sources . image . width / 2 , sources . image . height / 2 )
2719 @quads = create_mesh ( sel , corners , RES )
2820end
2921
3022def draw
3123 background ( 0 )
32- video . read if ( sel . respond_to? ( :read ) && video . available )
24+ video . read if sel . respond_to? ( :read ) && video . available
3325 # 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 )
26+ if corners . selected? && ( pmouse_x != mouse_x || pmouse_y != mouse_y )
27+ corners . set_corner ( mouse_x , mouse_y )
3628 # this improves performance, but will be replaced by a
3729 # more elegant way in a future release
3830 @quads = [ ]
@@ -41,28 +33,28 @@ def draw
4133 end
4234
4335 # display
44- quads . map { |quad | shape ( quad ) } if ! quads . empty?
36+ quads . map { |quad | shape ( quad ) } unless quads . empty?
4537 # hide the mouse cursor after two seconds
46- if ( pmouse_x != mouse_x || pmouse_y != mouse_y )
38+ if pmouse_x != mouse_x || pmouse_y != mouse_y
4739 cursor
4840 @last_mouse_move = millis
49- elsif ( !last_mouse_move . zero? && 2000 < millis - last_mouse_move )
41+ elsif !last_mouse_move . zero? && 2000 < millis - last_mouse_move
5042 no_cursor
5143 @last_mouse_move = 0
5244 end
5345end
5446
5547def mouse_pressed
5648 corners . each_with_index do |corner , i |
57- return @sel_corner = i if dist ( mouse_x , mouse_y , corner . x , corner . y ) < 20
49+ return corners . set_index ( i ) if dist ( mouse_x , mouse_y , corner . x , corner . y ) < 20
5850 end
5951 # no corner? then switch texture
6052 @sel = sel . respond_to? ( :loop ) ? sources . image : sources . video
61- quads = create_mesh ( sel , corners , RES )
53+ @ quads = create_mesh ( sel , corners , RES )
6254end
6355
6456def mouse_released
65- @sel_corner = - 1
57+ corners . set_index ( - 1 )
6658end
6759
6860def create_mesh ( tex , corners , res )
@@ -86,9 +78,9 @@ def create_mesh(tex, corners, res)
8678 sh . normal ( 0 , 0 , 1 )
8779 point = warp_perspective . map_dest_point ( x * x_step , y * y_step )
8880 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 )
81+ point = warp_perspective . map_dest_point ( ( x + 1 ) * x_step , y * y_step )
9082 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 )
83+ point = warp_perspective . map_dest_point ( ( x + 1 ) * x_step , ( y + 1 ) * y_step )
9284 sh . vertex ( point . get_x . to_f , point . get_y . to_f , 0 , ( x + 1 ) . to_f / res , ( y + 1 ) . to_f / res )
9385 point = warp_perspective . map_dest_point ( x * x_step , ( y + 1 ) * y_step )
9486 sh . vertex ( point . get_x . to_f , point . get_y . to_f , 0 , x . to_f / res , ( y + 1 ) . to_f / res )
0 commit comments