Orbifx's logarion 🌙

OpenGL with OCaml
programming > ocaml

Creating a setup for OpenGL development with OCaml

Before going through this, for those unfamiliar with OpenGL, spend some time learning the basics from "getting started" resources on OpenGl.org.

OpenGL's API is implemented in C, so some OCaml bindings need to be used. I chose to use Tgls, because the other couple of packages available are outdated. Also I have been very satisfied with other packages by Daniel Bünzli.

To get started, install tgls (OpenGL bindings) and tsdl (SDL bindings for the tests) with opam.:

opam install tgls tsdl

One has to visit the project's repository on Github to discover there are actually example programs. For my initial test I used trigl3.ml, which I copied into my own directory from the respective opam directory (find it using opam config var tgls:doc). There is also trigl4.ml for those privileged with newer graphics cards and OpenGL support (see Issues below).

To build my tests I used:

ocamlfind ocamlopt -o test -linkpkg -package tgls.tgl3,tsdl,result src/trigl3.ml

I had to add the result package, otherwise it wouldn't compile for 4.03. At this point the program compiled and generated a psychedelic triangle.

Going forward make sure to read about the naming conventions used by the Tgls bindings and check out the example programs. The key to learning how to use Tgls is to learn how to use OpenGL's C API, because all that Tgls does is wrap the C functions with OCaml ones.


One problem I run into was the error:

INFO: Could not create GL context: GLXBadFBConfig

This means version incompatibility between the Context being loaded and what the system provides (see Stack Overflow). That's because I initially copies trigl4.ml which uses a higher context profile version. Use trigl4.ml only if your driver supports it; check with glxinfo | grep version.