Skip to content

Instantly share code, notes, and snippets.

@dharmatech
Created February 22, 2010 02:35
Show Gist options
  • Save dharmatech/310719 to your computer and use it in GitHub Desktop.
Save dharmatech/310719 to your computer and use it in GitHub Desktop.
// Original version in Processing:
//
// http://www.openprocessing.org/visuals/?visualID=323
using math , ffi , GL , GLU , GLUT ;
GLUT::Init {0} NULL ;
GLUT::InitDisplayMode ( GLUT::DOUBLE or GLUT::RGB or GLUT::DEPTH ) ;
GLUT::InitWindowSize 500 500 ;
GLUT::CreateWindow "Flexi Line" ;
//////////////////////////////////////////////////////////////////////
// reshape
//////////////////////////////////////////////////////////////////////
let width = ref 500 ;
let height = ref 500 ;
reshape_func (w,h) = ()
when
GL::Viewport 0 0 w h ;
GL::MatrixMode GL::PROJECTION ;
GL::LoadIdentity ;
GL::Ortho 0.0 (double w) (double h) 0.0 (-1000.0) 1000.0 ;
put width w ;
put height h ;
end ;
let reshape_func_cb = fclos reshape_func FFI_DEFAULT_ABI void_t (sint_t , sint_t) ;
GLUT::ReshapeFunc reshape_func_cb ;
//////////////////////////////////////////////////////////////////////
// mouse
//////////////////////////////////////////////////////////////////////
mouse =
GLUT::MouseFunc mouse_func_cb $$
GLUT::MotionFunc motion_func_cb $$
{button => mouse_button ,
state => mouse_state ,
x => mouse_x ,
y => mouse_y ,
cb0 => mouse_func_cb ,
cb1 => motion_func_cb}
when
mouse_func_cb =
fclos mouse_func FFI_DEFAULT_ABI void_t (sint_t , sint_t , sint_t , sint_t);
motion_func_cb = fclos motion_func FFI_DEFAULT_ABI void_t (sint_t , sint_t);
end
with
mouse_func (button , state , x , y) =
put mouse_button button $$
put mouse_state state $$
put mouse_x x $$
put mouse_y y ;
motion_func (x , y) = put mouse_x x $$ put mouse_y y ;
end
when
mouse_button = ref(-1);
mouse_state = ref(-1);
mouse_x = ref(-1);
mouse_y = ref(-1);
end;
let mouse_data = mouse ;
get_mouse elt = get (mouse_data!elt) ;
//////////////////////////////////////////////////////////////////////
// mouse_xy () = { (get mouse_x) , (get mouse_y) } ;
mouse_xy () = { get_mouse x , get_mouse y } ;
//////////////////////////////////////////////////////////////////////
square n = n*n ;
a::matrix + b::matrix = zipwith (+) a b ;
a::matrix - b::matrix = zipwith (-) a b ;
v::matrix * n = map (*n) v ;
v::matrix / n = map (/n) v ;
norm v = sqrt ( square ( v!0 ) + square ( v!1 ) ) ;
normalize v = v / (norm v) ;
//////////////////////////////////////////////////////////////////////
point_update (pos,vel) =
(
pos + vel
,
(
if (get_mouse state) == GLUT::DOWN
then
( vel + normalize( mouse_xy() - pos )
*
(if (get_mouse button) == GLUT::LEFT_BUTTON then 1 else -1) )
*
0.98
else vel
)
)
when GL::Vertex2d (pos!0) (pos!1) ; end ;
let N = 5000 ;
let points =
ref [ ( { i/N * (get width) , (get height)/2 } , { 0 , 0 } ) | i = 0..N ] ;
//////////////////////////////////////////////////////////////////////
// display
//////////////////////////////////////////////////////////////////////
display _ = ()
when
GL::MatrixMode GL::MODELVIEW ;
GL::LoadIdentity ;
GL::ClearColor 0.0 0.0 0.0 1.0 ;
GL::Clear (GL::DEPTH_BUFFER_BIT or GL::COLOR_BUFFER_BIT) ;
GL::Color3f 1.0 1.0 1.0 ;
GL::Begin GL::LINE_STRIP ;
put points (map point_update (get points)) ;
GL::End ;
GLUT::SwapBuffers ;
end ;
let display_cb = fclos display FFI_DEFAULT_ABI void_t void_t ;
GLUT::DisplayFunc display_cb ;
//////////////////////////////////////////////////////////////////////
// timer
//////////////////////////////////////////////////////////////////////
let timer_cb = fclos timer FFI_DEFAULT_ABI void_t sint_t
with
timer n = ()
when
GLUT::PostRedisplay;
GLUT::TimerFunc 40 timer_cb n;
end;
end;
GLUT::TimerFunc 40 timer_cb 0;
//////////////////////////////////////////////////////////////////////
GLUT::MainLoop ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment