Sample GL code for Mac that doesn't use glut

I’m buying a Mac for my niece and it should arrive in a few days. Before giving it to her I’ll get some chance to play with it for about a week and I thought I should use the opportunity to add Mac support to my framework. So now I’m looking for some sample code I could use as a starting point to learn how to setup OpenGL. I want code that’s interfacing the Mac GL system directly and not some kind of abstraction like glut. Unfortunately, I’ve been googling like mad for several hours and pretty much everything I find it glut based. Does anyone have know of any simple example code that just opens a window, creates a GL context and maybe renders a triangle or something along those lines and is not using glut?

I was trying to do that too some time ago!

Basically you have five options:

  1. Use Cocoa. That uses Java or Objective C ( this one can be a very strange language if you come from C/C++ btw! Hopefully you can mix C/C++ with Objective C in the same way you can mix C++ and MC++, that’s called Objective C++ btw ). Think in Cocoa like the Apple’s version of .NET. You can use very easy the Interface Builder and use the pre-built OpenGL Cocoa control to init an OpenGL viewport. I don’t recommend you that to start though, specially if you don’t know Objective-C well.

To see a very simple Cocoa+Interface Builder(NIB files) go to:

To learn the basics of Objective-C see:

  1. Carbon. The old C Macintosh API. Looks like MFC/Win32 API. Can be an option. You can use the aglXXXXX calls to setup the render context, choose pixel format, etc… See…/uid/TP30001068

Just create a new window using this…CH206-TPXREF149

and then init OpenGL with the aglXXXX functions.

You can use the Interface Builder with Carbon too but, in that case, it won’t generate code like does with Cocoa ( will be reduced to a simple GTK Glade/Gazpacho workflow without code generation, and you will need to add the event/signals manually ).

  1. GLUT. ( I know, you don’t want it! ).

  2. The MacOSX 10.4 DVD allows you to install X11. Use the GLX as if you were in linux, works perfectly! ( in fact you can use the glxinfo and glxgears in MacOSX to test OpenGL! )

  3. Java. MacOSX comes with a superfast JVM and Xcode supports it plenty. Just use any existing OpenGL wrapper.

For more information and examples see:

Btw, MacOSX comes with a nice “Visual Studio” incorporated. Is called XCode and comes with GCC 4 ( C, C++, Java and Objective C ), an integrated debugger and the Interface Builder to make nice Windows-onClick-button applications. More info about XCode at

and about the Interface Builder:


Personally I would just use GLX in the same manner than linux . Just press over that “X11” icon in your MacOSX /Applications tools folder.

Hope it helps ( you own me a nice Rad2900 and the source code of your new Global Illumination DX10 app in

I recognize those FPS numbers Mr.Humus!

 :p      )

X11 is not installed by default, there have been small issues with OpenGL in it in the past, and it’s very un-Mac-like. I definitely wouldn’t advise touching it with a barge pole.

Cocoa is definitely the easiest route. There’s lots of code all over the place; a personal plug would be

Mac ports of your stuff would be much appreciated :smiley:

Originally posted by santyhamer:
Basically you have five options:
Well, it’ll have to work with my existing code, so it’s C++, so that excluded 1 and 5, and I don’t want to use glut so that option is out too. I’ve considered the X11 option too, but since that’s not installed by default, I think AGL is what I need. Thanks for the links, they are very useful.

Originally posted by santyhamer:
[b]I recognize those FPS numbers Mr.Humus!

:stuck_out_tongue: [/b]
Guilty as charged, but I can’t comment on it at this time.

Originally posted by OneSadCookie:
Thanks! Your SimpleAGL sample looks exactly like what I need. I don’t have the system yet, so I haven’t tried it yet, but it looks like I good starting point.

Originally posted by OneSadCookie:
Mac ports of your stuff would be much appreciated :smiley:
I hope to make the framework compatible so that all future demos would compile fine on Mac just automagically, like most things do for Linux. I just make a quick test once I’m done and usually I don’t need to make any changes for Linux. I’m not keeping the system though, so chances are not all demos will work all the time since I can’t test, but hopefully it’ll work out fine most of the time.

I’d really recommend Cocoa over AGL. As has been said, you can freely mix ObjC, C, and C++ depending on your precise needs.

AGL is icky, ancient, and relies on QuickDraw which is deprecated in Tiger, and isn’t even present for 64-bit code in Leopard.

GameEngineII running on the Mac:

Got everything working, except one minor detail. I can’t seem to find any way to catch the Command-Q event. RunApplicationEventLoop() quits, but that’s not enough given the way my framework work, it’ll just restart and create a new window since the application didn’t set done=true.

According to the docs, you should “install an AppleEvent handler for the kAEQuitApplication event handler if it needs to check for unsaved documents or do other cleanup before quitting”.

No idea how to do that, sorry :expressionless:

Thanks, got that working too. So much easier when I know exactly what to google for. Now all issues should be ironed out. :slight_smile:

I’ve uploaded the updated framework to my website if anyone’s interested in trying to compile any of my demos. You’ll have to create your own xcode projects though since I’ve already packed down the machine in the box and won’t be able to create any project files for Mac in the future either since I won’t have a machine anymore. But it should be quick enough to just include the neccesary framework files (just look in the Makefile for Linux as a reference if neccesary, except you use MacBase.cpp instead of LinuxBase.cpp). And you have to ensure the app runs from the project path rather than “build/debug” or whatever it’s by default. Actually, that’s one issue remaining. If anyone knows a way to find the path of the current .exe on the Mac that would be very useful.

Now I just have to hope my Mac code won’t break anytime soon because it would be hard to fix. :stuck_out_tongue:

Now that it’s going, it’ll be much easier for someone else to maintain… if you can find a willing victim :wink:

CFBundleCopyExecutableURL will find the executable URL (but you probably want CFBundleCopyResourcesDirectoryURL or CFBundleCopyBundleURL instead).

The most “mac-ish” way to do this will be using Cocoa.

There’s a class in Cocoa called NSOpenGLView that does most of the initial setup for you. There are a couple of good sample apps out there. Try this one, from Apple’s website:

It creates a window with an NSOpenGLView in it, then draws a shaded cube which rotates, driven by a timer. It also supports mouse code for manually rotating/panning/zooming in on the object, plus live update as you resize the window. Its a good starting point.

Duncan C

I had done some OpenGL before in Linux, and now I want to port some of my apps in Mac, basically want to replace Glut with Cocoa interface. I have leopard installed and the latest Xcode, but I have a hard time following the official OpenGL tut from apple’s site, specifically this one,…inkElementID_15
I get stuck @ the step of creating a subclass of NSOpenGLview. For life of me, I can’t find the Classes tab on the menubar of interface builder when I choose NSOpenGLview class on the mainmenu.nib. I suspect that tutorial is for older version of Xcode/interface builder, does anyone know how to subclass an existing class in this newer interface builder. Thanks a bunch!


You might want to post general Cocoa questions like this to the Cocoa mailing list (

The way I’d suggest doing this is to create a shell of your class that defines the custom NSOpenGLView. Define your class myOpenGLView as a subclass of OpenGLView in the header. The header would look something like this:

@interface myOpenGLView : NSOpenGLView
//custom stuff goes here

Then, in interface builder, at the top level, click on “files owner” and select the menu item “read class files” from the file menu. Navigate to the header file you created above, and select it.

That teaches IB about the custom classes you’ve defined. Now, when you create a custom view object, select it and go to the “identity inspector” in the tools menu (or press command 6 in Interface Builder version 3.0). You can then change the class of your custom view object to be a “myOpenGLView” object (or whatever you called it.)

Thanks Duncan,
I will try that and next time I will make sure to separate Cocoa specific questions from this forum. :slight_smile:

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.