Using FLTK and OpenGL on Mac OS X

For ECS 175 and ECS 177, computer science graphics classes at UC Davis, we were supposed to use FLTK for our GUI and OpenGL. In order to work on my project at home, I decided to try and get it working under MacOS X using XFree86 and XDarwin. I have since found out that X-Windows is not actually required to use FLTK at home since FLTK comes with MacOS resources. However, it is still worth installing one of these, or the new Apple supported X11 for Mac OS X which I am now using.

The following assumes that you are running Mac OS X (examples tested under version 10.2.3), and have installed the Apple Developer Tools which are included with every boxed copy of MacOS X as a separate CD (these include the gcc/g++ compiler you'll need among other things). If your Mac came with OS X already, you may not have received the Developer Tools CD. If this is the case, you may download the tools directly from Apple's website (assuming you have access to a fast internet connection). developer.apple.com/tools/. You will need to sign up for a free Apple Developer Connection (ADC) account to do this.

FLTK was pretty easy to get working - I modified the FLTK hello program to also display a button and text field, hello.cpp (although the unmodified one works fine also). After installing FLTK (see their web page for details - it's pretty easy), the following command can be used to compile the hello program:
      fltk-config --compile hello.cpp

Now, to get my project from school working, I had to modify the #includes in my files a little. Apple uses OpenGL/gl.h instead of the standard GL/gl.h. So, in all of my source files, where I had previously had the line
      #include <GL/gl.h>

I replaced it with the following code (thanks to Ben Forsyth for this):
#ifdef __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#else
#include <GL/gl.h>
#include <GL/glu.h>
#endif

I also had to change a few lines in my make file... (again, thanks to Ben Forsyth for this)

LDLIBS = -L/usr/local/lib/ -framework Carbon -framework ApplicationServices -lsupc++
GLDLIBS = -framework AGL -framework OpenGL
LDFLAGS  = $(LDLIBS) $(GLDLIBS) -lfltk_gl -lfltk -lm

Compiling the program now worked under MacOS X, but when running the executable, the window was inactive and could not be brought to the front. Once again, Ben had the answer. A resource fork needs to be added to the executable before OS X will let it come to the front (although I have a feeling there is a way around this since I managed to do it with the hello program - let me know if you figure one out).

To add the resource fork, you use a program called "Rez" which comes on the Developer CD and should be installed with the default developer install. Execute this commmand after compiling where "executable" is the name of your executable file (or you could add it to your makefile):
      /Developer/Tools/Rez -t APPL -o executable /usr/local/include/FL/mac.r

Click here to view a sample Makefile. This was based on the Makefile distributed in ECS 175 at UC Davis in the Fall of 2002. Note, to actually use this Makefile, make sure the tabs are correct since the make utility is very picky about this. It is also possible to set up Apple's Project Builder (now called XCode) IDE to directly build these projects, although that takes some familiarity with Project Builder.

Hopefully some other computer science student out there trying to use OS X will find this useful. My program now compiles and runs at home exactly the same as it does on the linux boxes at school!

Click here for a small screenshot of the result.
Click here to see some other pictures from these classes.

Note, I have since gotten this working directly with Apple's Project Builder (XCode) IDE. This is kind of nice since you don't even need a makefile anymore - it does it for you. It also allows you to use the nice debugger in Project Builder.

Feel free to email me if you have (very simple) questions.

This page is no longer maintained - if the information contained within it is helpful - great - otherwise, continue searching.

email me

home
Hosted by www.Geocities.ws

1