Using OpenGL Utilities with Xamarin.Mac

In porting the following code from an Objective-C sample in Chapter 35 of Big Nerd Ranch’s Cocoa Programming Guide┬áto Xamarin.Mac, I noted that some methods are unavailable in the OpenTK binding available in the Xam.Mac SDK, notably the utility methods found in Open GL Utilities, or GLUT:

gluPerspective (60.0, baseRect.Size.Width/baseRect.Size.Height, 0.2, 7);
gluLookAt(radius * Math.Sin(theta), 0, radius * Math.Cos(theta), 0, 0, 0, 0, 1, 0);
glutSolidTorus(0.3, 0.9, 35, 31);
glutSolidCone(1, 1, 17, 17);

The above four methods are found in two different libraries that are already on your Mac OS X system in the directory /System/Library/Frameworks/. If the method prefix is glu then you need the OpenGL.dylib in the Libraries directory of OpenGL.framework. If the prefix is glut then you will need the GLUT.dylib. However you won’t find a GLUT.dylib in GLUT.framework/Libraries. There is no Libraries directory in that framework. But you can use plain old GLUT that is in the Versions/Current directory, and you might as well do the same for the OpenGL library.

You can use P/Invoke to access these methods. You can see the signature you need for the P/Invoked methods in the man pages for the above methods in Terminal, e.g.:

Jons-iMac:~ $ man gluLookAt
NAME
       gluLookAt - define a viewing transformation

C SPECIFICATION
       void gluLookAt( GLdouble eyeX,
                       GLdouble eyeY,
                       GLdouble eyeZ,
                       GLdouble centerX,
                       GLdouble centerY,
                       GLdouble centerZ,
                       GLdouble upX,
                       GLdouble upY,
                       GLdouble upZ )

 

Don’t worry about the GLdouble type, just use double instead. See below the fold for the full code to P/Invoke those methods from the Open GL Utilities.

const string GluLib =
        "/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL";
const string GlutLib = 
       "/System/Library/Frameworks/GLUT.framework/Versions/Current/GLUT";

[DllImport(GluLib)]
static extern void gluPerspective( 
         double fovy,
         double aspect,
         double zNear,
	 double zFar );

[DllImport(GluLib)]
static extern void gluLookAt ( 
	double eyeX,
	double eyeY,
	double eyeZ,
	double centerX,
	double centerY,
	double centerZ,
	double upX,
	double upY,
	double upZ );

[DllImport(GlutLib)]
static extern void glutSolidTorus (
	double innerRadius,
	double outerRadius,
	int nsides, 
	int rings);

[DllImport(GlutLib)]
static extern void glutSolidCone (
	double bottom, 
	double height,
	int slices, 
	int stacks);

 

And then call the methods like normal:

gluPerspective (60.0, baseRect.Size.Width/baseRect.Size.Height, 0.2, 7);
gluLookAt(radius * Math.Sin(theta), 0, radius * Math.Cos(theta), 0, 0, 0, 0, 1, 0);
glutSolidTorus(0.3, 0.9, 35, 31);
glutSolidCone(1, 1, 17, 17);

I have placed all of the code that I have ported from the tutorials in Big Nerd Ranch’s Cocoa Programming Guide to my github account here:

For the example above, the exact project is the Gliss project.

Leave a Reply

Your email address will not be published. Required fields are marked *