F# + OpenGL: a cross-platform sample

Laurent Le Brun has a very nice blog post on cross-platform graphics programming with F# and OpenGL.

I love OpenGL but, until now, I used it only in C++ (plus one quick test in OCaml). I wanted to try it with F#. As OpenGL is a portable library, the result should be cross-platform; that's what I checked. I found OpenTK, which is the library that wraps OpenGL in .NET (there is also OpenAL and OpenCL). Of course, the documentation and the samples are C#-oriented, but they are very easy to translate. I was happy to see that the project is very active, and there are many examples, some of them including the latest OpenGL extensions (e.g. geometry shaders). I really enjoyed OpenTK API: it's cleaner than standard OpenGL, typesafe (int values are replaced by enums), and uses overloaded functions to simplify the code. I didn't compare the speed of OpenTK with a C++ OpenGL application, but I believe it's very fast as the bindings stay close to OpenGL, and .NET (and Mono) generates fast code.

Here is my F# translation of the Quickstart code: game.fs. You just need to get OpenTK.dll and you'll get:

I started to play with this sample in F# interactive. I opened the window in a separate thread, I changed the display dynamically in fsi, thanks to mutable functions. That was really comfortable, as if I was using a dynamic scripting language... but with great performance and static typing! A few hours later, I was able to make a nice-looking OpenGL application (you'll see it later). The nice thing is that the application was cross-platform. I tried it on Windows, Linux and Mac, it worked everywhere, without changing a single line. It seems like F# is ready to make great cross-platform 3D applications!

View article...

Comments (3)

  1. anonymous says:


    Too bad many people will probably avoid F# (for cross platform code) as long as it's not available under Ms-Pl or something similar. Any news on this?

  2. Dax Fohl says:

    Nice.  Wasn't able to get dynamic scripting to work for some reason though; waiting eagerly for the next installment.  I am still hoping for a feature in the not-too-far-distant future that will allow me to send F# commands via serial port to a CE device, and have that CE device respond to them real-time.  This seems like only a short step from that ability.

  3. honu says:

    BerkeleyX March 2013


    CS184.1x teaches the Foundations of Computer Graphics … OpenGL

Skip to main content