Compiling and running Off Screen Mesa (OSMesa) demo (osdemo.c) on mac os x

Alec Jacobson

October 09, 2012

weblog/

I installed mesa on my mac using macports:

sudo port install mesa

But was sad to find out that the off screen renderer (the whole reason I got it), didn't work out of the box. The problem seems to be that the things aren't meshing well with the GLU implementation. I tried to compile the mesa demo file

osdemo.c with:

gcc -o osdemo osdemo.c -I/opt/local/include/ -L/opt/local/lib/ -lOSMesa -lglu

which compiled but produced a funny result:

mesa wrong output with bad glu implementation

This picture looked ok at first, but then I realized that there should be a green cone and a blue sphere also in the image. These objects were rendered using glu*() commands in osdemo.c, but some how have had no effect, despite compiling correctly. I also tried linking against the native GLU implementation in the mac os x OpenGL framework and the GLU implementation in my /usr/X11/lib directory. Both compiled but produced runtime errors:

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x000000000000000c
0x00007fff8c8b2a7d in gluQuadricDrawStyle ()
(gdb) bt
#0  0x00007fff8c8b2a7d in gluQuadricDrawStyle ()
#1  0x00000001000011a6 in Cone ()
#2  0x000000010000172d in render_image ()
#3  0x0000000100000e26 in main ()

I finally fixed the problem by reinstalling GLU by itself via mesa's repo:

./autogen.sh
./configure --enable-osmesa --prefix=/usr/local/
make
sudo make install

Now I can compile osdemo again (careful to note that I've places -L/usr/local/lib/ before -L/opt/local/lib/):

gcc -o osdemo osdemo.c -I/opt/local/include/ -L/usr/local/lib/ -L/opt/local/lib/ -lOSMesa -lglu

And when I run it I get the correct picture: correct osdemo output with correct glu implementation

Note: A new version of mesa was just released yesterday and may fix this issue. I don't know yet, because the mesa source does not compile out of the box for mac os x and the macports version has not yet been updated.

Update: Seems this problem still hasn't been fixed in the macports version of mesa-glu. I recently ran into this again where gluPerspective and gluLookAt were producing bogus results.