Skip to content

Instantly share code, notes, and snippets.

@dharmatech
Created February 20, 2010 22:30
Show Gist options
  • Save dharmatech/309955 to your computer and use it in GitHub Desktop.
Save dharmatech/309955 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 500.0 500.0 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
//////////////////////////////////////////////////////////////////////
let mouse_button = ref (-1) ;
let mouse_state = ref (-1) ;
let mouse_x = ref (-1) ;
let mouse_y = ref (-1) ;
mouse_func (button , state , x , y) = ()
when
put mouse_button button ;
put mouse_state state ;
put mouse_x x ;
put mouse_y y ;
end ;
let mouse_func_cb =
fclos mouse_func FFI_DEFAULT_ABI void_t (sint_t , sint_t , sint_t , sint_t ) ;
GLUT::MouseFunc mouse_func_cb ;
motion_func (x , y) = ()
when
put mouse_x x
put mouse_y y
end ;
let motion_func_cb = fclos motion_func FFI_DEFAULT_ABI void_t (sint_t , sint_t) ;
GLUT::MotionFunc motion_func_cb ;
passive_motion_func (x , y) = ()
when
put mouse_x x
put mouse_y y
end ;
let passive_motion_func_cb =
fclos passive_motion_func FFI_DEFAULT_ABI void_t (sint_t , sint_t) ;
GLUT::PassiveMotionFunc passive_motion_func_cb ;
//////////////////////////////////////////////////////////////////////
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 = 100 ;
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