SML# Project  Index  Search  Changes  Login

Graphical Animation with Open GL

Using SML# interpoperability you can easily call various C packages. This page shows a simple example code to call OpenGL.

The glut sample (in sample/glut directory of the source distribution) uses OpenGL and GLUT to show a sample 3D animation.

Screenshot

OpenGL and GLUT requires doule data manipuation, and call back functions of many arguments. In SML#, you can call various C functions of various functionalities including those taking callback functions by just declaraing their types, as shown below.

val glMaterialfv =
    DynamicLink.dlsym (libgl, "glMaterialfv")
    : _import (word, word, float * float * float * float) -> unit

val gluLookAt =
    DynamicLink.dlsym (libglu, "gluLookAt")
    : _import (real, real, real, real, real, real, real, real, real) -> unit

val glutDisplayFunc =
    DynamicLink.dlsym (libglut, "glutDisplayFunc")
    : _import (()->unit) -> unit

How to run the sample.

This uses an experimental CConfig library to locate library functions, but it is not perfect and you may need to set environment variables to tell the program the install location of libraries such as OpenGL. Below is some suggestion.

UNIX OS:

env CPPFLAGS=-I/usr/X11R6/include LDFLAGS=-L/usr/X11R6/lib smlsharp cube.sml

Mac OS X:

env LDD='otool -L' smlsharp cube.sml

Cygwin:

env CPPFLAGS=-I/usr/X11R6/include LDFLAGS=-L/usr/X11R6/lib \
    LIBNAME_SUFFIX=.dll LDD=cygcheck ARCSEP='\' smlsharp cube.sml

MinGW on Windows

You can try to run it on MinGW on Windows. The Windows system appears to have OpenGL (perhaps in opengl32.dll, glu32.dll.) You can also get GLUT library from this site. You probably need to hand modify the code libglut.sml. Good luck.

Last modified:2007/04/02 14:30:19
Keyword(s):
References:[Program Examples]