in Mac, Ocaml

Hello OpenGL World in Ocaml

I swear, if I read one more programming tutorial that starts with a recursive factorial function instead of a simple “Hello world” I’ll pray for perpetual nigerian spam on their inboxes. So, I was delighted to try out some Ocaml stuff today that didn’t involve factorials.

Since I like dabbling with data visualization I was happy to find the following snippet:

 let _ =
   ignore( Glut.init Sys.argv );
   Glut.initDisplayMode ~double_buffer:true ();
   ignore (Glut.createWindow ~title:"OpenGL Demo");
   let angle t = 10. *. t *. t in
   let render () =
     GlClear.clear [ `color ];
     GlMat.load_identity ();
     GlMat.rotate ~angle: (angle (Sys.time ())) ~z:1. ();
     GlDraw.begins `triangles;
     List.iter GlDraw.vertex2 [-1., -1.; 0., 1.; 1., -1.];
     GlDraw.ends ();
     Glut.swapBuffers () in
   GlMat.mode `modelview;
   Glut.displayFunc ~cb:render;
   Glut.idleFunc ~cb:(Some Glut.postRedisplay);
   Glut.mainLoop ()

Simple enough, apparently draws something on an OpenGL display. According to a friend of mine I am supposed to be able to compile ocaml code to a native app. Being on Mac OS X I was sceptical. Native OS X apps usually involves drawing arrows between events and boxes in some funky XCode window.

But, lo and behold:

sudo port install ocaml
sudo port install LablGL
ocamlopt -I +lablGL lablglut.cmxa lablgl.cmxa -o ogldemo

…creates an app that opens a window with the spinning triangle on OS X. And compilation is fast! I need to learn more about ocaml…

  1. Thanks for this post, it helped me a lot ! I’ve tried a TON of things (glMlite, CamlGL …) nothing worked and it’s almost impossible to get some informations about ocaml & openGL. Your post is definitely a must read for beginners ! ;)

Comments are closed.