freeglut + Windows + HOpenGL + HGLUT

freeglut is a version of glut that is vastly superior to the one typically used with windows. Aside from being Open Source, it interacts well with ghci (see leaveMainLoop) and provides a rhombic dodecahedron primitive. This post walks through the somewhat complicated process of getting Freeglut and the haskell OpenGL and GLUT bindings installed and working on windows. This feat is accomplished using the brand new ghc 6.10.1.

Install mingw & msys

Mingw and msys are required for the compiler and configure scripts. It has been reported to me that cygwin, which provides similar functionality, will not work.

MinGW. First, download the MinGW 5.1.4 installer and save it in a new folder. Execute the file and, when prompted, choose a minimal installation (it is important that you don’t install MinGW make).

MSYS. Download and run the msys 1.0.10 installer. You’ll be prompted with a console. Say yes to post install, reply yes to having MinGW installed, and type in your mingw location (default is C:/MinGW).

To check everything installed correctly, use Start->Programs->MinGW->MSYS->msys to open a prompt. At the prompt, typing gcc --version should show you the version of gcc you have installed.

Compile and Install freeglut

Download freeglut 2.4.0 source distribution and save it to C:\msys\1.0\home\<Your Username> (This is your default msys home directory. If you have your HOME environment variable set for another program, like emacs, your msys home directory is the value of that variable.).

Start the msys window, if you don’t already have one opened, and type the following commands at the prompt. You can paste into the msys window using SHIFT+INSERT or middle click.:

tar -zxf freeglut-2.4.0.tar.gz
cd freeglut-2.4.0/src/
gcc -O2 -c -DFREEGLUT_EXPORTS *.c -I../include
gcc -shared -o glut32.dll *.o -Wl,--enable-stdcall-fixup,--out-implib,libglut32.a -lopengl32 -lglu32 -lgdi32 -lwinmm

Now you should be left with glut32.dll and libglut32.a in that directory. Now move glut32.dll into your windows system32 directory.

cp glut32.dll /c/WINDOWS/system32

libglut32.a needs to be copied to ghc’s gcc-lib directory.

cp libglut32.a /c/ghc/ghc-6.10.1/gcc-lib/

Haskell OpenGL Binding

Download the haskell OpenGL binding from hackage here. At the time of this writing, the latest version was 2.2.1.1. Save it to your msys home directory, then type:

tar -zxf OpenGL-2.2.1.1.tar.gz
cd OpenGL-2.2.1.1
runhaskell Setup configure
runhaskell Setup build
runhaskell Setup install

Haskell GLUT Binding

Download the haskell GLUT binding from hackage here. At the time of this writing, the latest version was 2.1.1.2. Save it to your msys home directory.

You’ll also need to download the glutwin2112 patch (glutwin2112.patch) and save this to the msys home directory.

Then from an msys prompt enter the following:

tar -zxf GLUT-2.1.1.2.tar.gz
cd GLUT-2.1.1.2
patch -p1 < ../glutWin2112.patch
C_INCLUDE_PATH="../freeglut-2.4.0/include/" LIBRARY_PATH="../freeglut-2.4.0/src/" runhaskell Setup configure
C_INCLUDE_PATH="../freeglut-2.4.0/include/" LIBRARY_PATH="../freeglut-2.4.0/src/" runhaskell Setup build
C_INCLUDE_PATH="../freeglut-2.4.0/include/" LIBRARY_PATH="../freeglut-2.4.0/src/" runhaskell Setup install

Testing

Try to save and run the following file (teapots.hs). You should see a shaded sphere when run.

{-
   Light.hs (adapted from light.c which is (c) Silicon Graphics, Inc.)
   Copyright (c) Sven Panne 2002-2005 <sven.panne@aedion.de>
   This file is part of HOpenGL and distributed under a BSD-style license
   See the file libraries/GLUT/LICENSE
 
   This program demonstrates the use of the OpenGL lighting model. A sphere
   is drawn using a grey material characteristic. A single light source
   illuminates the object.
-}
 
import System.Exit ( exitWith, ExitCode(ExitSuccess) )
import Graphics.UI.GLUT
 
myInit :: IO ()
myInit = do
   clearColor $= Color4 0 0 0 0
   shadeModel $= Smooth
 
   materialSpecular Front $= Color4 1 1 1 1
   materialShininess Front $= 50
   position (Light 0) $= Vertex4 1 1 1 0
 
   lighting $= Enabled
   light (Light 0) $= Enabled
   depthFunc $= Just Less
 
display :: DisplayCallback
display = do
   clear [ ColorBuffer, DepthBuffer ]
   renderObject Solid (Sphere' 1 20 16)
   flush
 
reshape :: ReshapeCallback
reshape size@(Size w h) = do
   viewport $= (Position 0 0, size)
   matrixMode $= Projection
   loadIdentity
   let wf = fromIntegral w
       hf = fromIntegral h
   if w <= h
      then ortho (-1.5) 1.5 (-1.5 * hf/wf) (1.5 * hf/wf) (-10) 10
      else ortho (-1.5 * wf/hf) (1.5 * wf/hf) (-1.5) 1.5 (-10) 10
   matrixMode $= Modelview 0
   loadIdentity
 
keyboard :: KeyboardMouseCallback
keyboard (Char '\27') Down _ _ = exitWith ExitSuccess
keyboard _            _    _ _ = return ()
 
main :: IO ()
main = do
   (progName, _args) <- getArgsAndInitialize
   initialDisplayMode $= [ SingleBuffered, RGBMode, WithDepthBuffer ]
   initialWindowSize $= Size 500 500
   initialWindowPosition $= Position 100 100
   createWindow progName
   myInit
   displayCallback $= display
   reshapeCallback $= Just reshape
   keyboardMouseCallback $= Just keyboard
   mainLoop

Troubleshooting

Linker Errors. Pay close attention to the output of the GLUT configure command, it should have a line that looks like:

checking for GLUT library... -lglut32 -lglu32 -lopengl32 

If not, it may have failed without telling you so.

More Resources. The haskell-cafe mailing list is a good place to ask for help. Also this site has some information on getting freeglut+windows working with an older version of ghc.

Acknowledgments

joelhough.com blog for getting me started. Peter Verswyvelen for information on the HOME variable and pasting into the msys window. RayNBow for noting that the libglut32.a must be copied to a ghc directory. newsham and Greg Fitzgerald for noting the issues prompting the need for a patch. And all others who I may have forgotten.

Comments (34)

RayNbowNovember 10th, 2008 at 6:26 am

Thanks for this guide. :)

The test program works fine in GHCi, but it didn’t work when compiled. It did work however after copying libglut32.a to ghc-6.10.1/gcc-lib as told on http://joelhough.com/blog/2008/04/14/haskell-and-freeglut-at-last/ :)

David SankelNovember 10th, 2008 at 10:54 am

Thanks for the note! I’ve updated the post with your suggestion.

Peter VerswyvelenNovember 10th, 2008 at 1:44 pm

Very nice and helpful.

One small typo: “gcc –version” should be “gcc –-version”

ErvinTWNovember 10th, 2008 at 11:08 pm

Thanks! Nice post.

newshamNovember 18th, 2008 at 2:26 pm

I followed your instructions and they worked flawlessly (including the example running). However, if I install Fieltrip and associated reactive packages and then try to build a program with it I get: “Tetris.exe: user error (unknown GLUT call glutSetOption, check for freeglut)”. I verified that the first glut32.dll in my path is the one I just built. Any ideas what is going on here? Is it somehow picking up a non freeglut glut32.dll or libglut.a/libglut32.a?

Greg FitzgeraldNovember 18th, 2008 at 7:00 pm

I’m having the same problem as newsham on both Windows XP and OSX Leopard. The problem can also reproduced by launching the example that comes with reactive-fieldtrip-0.0.3.

David SankelNovember 19th, 2008 at 10:55 am

Thanks Greg and newsham for reporting the problems you’ve encountered. I’ve updated the above post with a patch to be applied to the GLUT release that fixes these problems.

newshamNovember 19th, 2008 at 1:32 pm

Thank you David, works great here now.

Greg FitzgeraldNovember 19th, 2008 at 4:04 pm

Thanks David, the GLUT patch works for me too!

Long ToNovember 23rd, 2008 at 6:17 pm

Do Mingwin and Msys work properly in windows vista?

David SankelNovember 24th, 2008 at 5:33 pm

Long To, as far as I understand the latest release of mingw works fine with vista. For msys, you may need to get development snapshots, but I don’t know for sure.

Long ToNovember 25th, 2008 at 9:05 pm

Dave, Thanks. I am learning Haskell using SOE and having problem installing the book’s software. It uses GLFW library (insteads of glut) which also requires OpenGL binding for Haskell. Your detailed process of installing the freeglut gave me some idea to proceed. Thanks again, and Happy Thanksgiving!

magnumbudFebruary 2nd, 2009 at 11:06 am

hi, i’m getting an error in the haskell opengl binding when i run runhaskell setup install. im getting Setup : CreateDirectory: permission denied.

David SankelFebruary 2nd, 2009 at 1:09 pm

magnambud, try running as administrator on your windows box. I’m guessing there are some permissions issues going on.

magnumbudFebruary 6th, 2009 at 10:09 am

thanks i tried that but for some reason it crashes if i try to run it as admin it says cannot find rxvt.exe and sh.exe binary — aborting.

David GladsteinFebruary 11th, 2009 at 5:01 pm

Installing on XP Pro, the downloaded OpenGL and GLUT files had .tar.tar extensions, not .tar.gz, otherwise the instructions worked perfectly.

I did this install when going from GHC 6.8.3 to 6.10.1 ‘coz the OpenGL libraries don’t come with GHC any more. I noticed one big behavior change: in single buffered mode, antialiased polygons used to draw incredibly slowly (several seconds for a big triangle). Now it’s way fast but not antialiasing.

David GladsteinFebruary 12th, 2009 at 10:54 am

Installing on Windows Vista Ultimate failed due to file/directory permission problems, even though I’m an Administrator. Turning User Account Control off in Control Panel > User Accounts for the duration of the install fixes the problem.

David GladsteinFebruary 12th, 2009 at 10:57 am

The lack of antialiasing (February 11th, 2009 at 5:01 pm ) can be fixed by adjusting the quality settings in the NVIDIA control panel.

magnumbudMarch 3rd, 2009 at 11:50 am

thanks setting the user account control to off worked. does anyone know how to set up the haskell sdl or any tutorials that are out there like this

greatMarch 22nd, 2009 at 3:10 am

How did you guys install minGW whenever I do it on windows it says;

gzread: incomplete block read Error: Failure reading from tarball.

David SankelMarch 23rd, 2009 at 9:30 am

I just ran the installer and it worked for me. You may have a corrupted download. What version of windows are you using?

Movin' OnMarch 30th, 2009 at 4:46 pm

with all due respect….this is a F**king Royal Pain in the *ss.

I followed the instruction to the letter…got it installed…and it worked. Then I did a Windows update….and it no longer worked…

Error message…Could not load GLUT files.

So tried installing it on another Windows partition…only to be met with endless error messages when trying to install

The final one was…runhaskell: command not found

WTF????

downloaded the file again, unzipped it…installed, changed directories….same error message.

Tried with a previous downloaded version and got that brick wall error message again.

So.,..buggy sh*t…it worked yesterday, but does not work today?

Looks like it does work eventually, but is fragile as hell, and will corrupt at the slightest chance.

I tip my hat to the original blogger on this board, as your work and directions are superb….but the components to get this to work consistently leave a lot to be desired.

So much for open source free software.

A royal waste of time!!!!!!!!!!!!!!!!!!!!!!!!

ArnaudApril 7th, 2009 at 6:46 pm

It works flawlessly for me under ghci, however if I try:

$ ghc teapot.hs

I get a lot of undefined references:

teapots.o:fake:(.text+0×37d): undefined reference to GLUTzm2zi1zi1zi2_GraphicsziUIziGLUTziObjects_Teapot_con_info' teapots.o:fake:(.text+0x387): undefined reference toGLUTzm2zi1zi1zi2_GraphicsziUIziGLUTziObjects_renderObject_closure’ teapots.o:fake:(.text+0×394): undefined reference to GLUTzm2zi1zi1zi2_GraphicsziUIziGLUTziObjects_Solid_closure' teapots.o:fake:(.text+0x3db): undefined reference toOpenGLzm2zi2zi1zi1_GraphicsziRenderingziOpenGLziGLziDisplayLists_defineNewList_closure’

Any idea?

David SankelApril 8th, 2009 at 10:03 am

@Arnaud: Did you try copying over the .a file as RayNBow above suggests?

ArnaudApril 8th, 2009 at 5:20 pm

It finally seems that:

ghc –make teapot.hs

works fine.

CangaseiraAugust 13th, 2009 at 7:08 am

It worked for me,thanks(including the example)I followed step by step very nice

david hodgettsAugust 14th, 2009 at 6:28 pm

Many thanks for the how-to. There is just a little thing bothering me: After installation, the teapot.hs programs opens in GHCI, however when I hit ESC. I get the following error message: : interrupted : warning: too many hs_exit()s followed by GHCI quitting. I was wondering if this is normal behaviour. When I compile, it seems to quit gracefully.

david hodgettsAugust 16th, 2009 at 5:17 pm

I spoke too quickly. I can gracefully return to GHCI with a “MainLoopReturns” in a “actionOnWindowClose” callback. Sorry for the noise. and thanks again.

HarmenAugust 25th, 2009 at 5:16 am

Thanks, works. Going to haskell OpenGL-2.3.0.0 (and all the required packages) does not work for me. But OpenGL-2.2.x.x will mostly do the same for me atm :)

IshenlyAugust 28th, 2009 at 5:22 pm

Hi people, thanks for the post, but, the .exe generated don´t work (don´t show any). In the compilation process all go OK, but when i ran the binary tell me this:

appname: user error (unknown GLUT entry glutInit)

And I try openGL in a very little program, very basical, this program will be show a window; but the message is the same. I’m a very novice in Hadkell in my head (because this) is a trouble.

Thanks a lot. XD

Jerzy KarczmarczukNovember 19th, 2009 at 1:19 pm

I had no problems until today. Since my students will work with Haskell OpenGL, I tried to recompile everything following blindly the instructions above ; the versions changed, some dependencies (StateVar, Tensor, etc. have been resolved), fine. And then Light reports: Light: user error (unknown GLUT entry glutInit) I imported freeglut compiled elsewhere. The same error. I changed to some glut32.dll which works (actually, from the Raincat game). Works. Got glut32.dll from somewhere else, searching the Web. Works.

Windows XP Pro, absolutely standard. The glut32.dll put in the example folder. freeglut-2.4.0; GLUT-2.2.2.0; GLURaw-1.1.1.0; OpenGLRaw-1.1.0.1; OpenGl-2.4.0.1

My understanding fails here. Something happened to freeglut? Impossible… Some special initialization needed?? Do I have to lookup the sources?…

Thanks. JK

rtpersonDecember 29th, 2009 at 1:02 pm

I’m getting a similar problem — in August of this year, I was able to install no problem. It seems that ever since OpenGL-2.3.x.x, these instructions have simply stopped working. Like Jerzy, I installed glut32.dll from other sources, and the “unknown GLUT entry glutInit” error went away. Now, however, after building with “ghc –make Light.hs,” I get the following: Light: user error (unknown OpenGL extension entry glClearColor, check for OpenGL 3.1).

I made sure my NVIDIA drivers supported OpenGL 3.1. I also tried a cbits hack (the only advice I could find on the web) from http://old.nabble.com/OpenGLRaw-on-Windows-Vista-td25264556.html. Nada.

I may try to downgrade, or I may try to contact one of the RainCat developers to see what version of HOpenGL they developed against. But honestly, I think the real solution is to develop on Linux, where you don’t have to deal with the hassle of absent underlying libraries.

AndersJanuary 21st, 2013 at 3:34 pm

Followed the guide to the point. The result was a blank window, with no info at all to tell me what could possibly be up. Alongside the trouble with getting the files from sites that apear to be constantly on the verge of crashing, I’m getting the distinct feeling that the Haskell ecosystem is just complete crap. I mean why isn’t this just a case of “download this file, press enter, and there you go!” why the need to patch things to get them to work, why haven’t whatever problems there are just been fixed, and why isn’t there some sort of package management that can keep track of all of this stuff.

Leave a comment

Your comment