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 ogldemo.ml -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…

One comment

  1. Nicolas says at 2008-12-03 18:12:

    Hello there, glad you’re interested in using OCaml with OpenGL.
    Actually, I’m also an OCaml/OpenGL newbie, and I’ve been writing some tutorials/posts about using these language/libraries to visualize Radiohead’s HoC music video.
    You might find these posts interesting, I guess:
    http://blog.thejit.org/2008/12/02/using-ocaml-to-visualize-radioheads-hoc-music-video-part-2/

    Your blog is very interesting :)

Leave a comment

You can use some HTML elements. You know which they are.

Additional comments powered by BackType