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...