Created
February 22, 2010 02:30
-
-
Save dharmatech/310717 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Original version in Processing: | |
// | |
// http://www.openprocessing.org/visuals/?visualID=323 | |
using math , ffi , GL , GLU , GLUT , glamour ; | |
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 $$ | |
(mouse_button , mouse_state , mouse_x , mouse_y , mouse_func_cb , 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_button , mouse_state , mouse_x , mouse_y , cb0 , cb1) = mouse ; | |
////////////////////////////////////////////////////////////////////// | |
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