MonoMac and OpenGL
Feb 25
Cocoa, MonoMac, OpenGL 2 Comments
OpenGL has landed into the MonoMac code base as of last night.
Here is the blurb from the MonoMac team explaining what was done to accomplish this.
MonoMac provides an API compatible interface to the OpenTK GL.* functions. This is accomplished by forking a few files from OpenTK and removing their delegate / context infrastructure and just doing direct pinvokes to OpenGL.framework.
Unsupported extensions and methods have been commented out to avoid runtime exceptions.
With the exception of OpenTK/Graphics/OpenGL/* the rest of the files are a direct copy from OpenTK with the namespaces changed.
This was forked from OpenTK r3066, by Kenneth Pouncey.
What this basically means is that if you have used OpenTK in your project the commands to call the OpenGL routines will remain the same because it is compatible with the OpenTK API.
For example take the following snippet:
GL.ClearColor (NSColor.Clear.UsingColorSpace (NSColorSpace.CalibratedRGB)); GL.Clear (ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.Enable (EnableCap.DepthTest); GL.Hint (HintTarget.LineSmoothHint, HintMode.Nicest); GL.Hint (HintTarget.PolygonSmoothHint, HintMode.Nicest); if (previousTime == 0) previousTime = timeInterval; rotation += 15.0 * (timeInterval - previousTime); GL.LoadIdentity (); double comp = 1 / Math.Sqrt (3.0); GL.Rotate (rotation, comp, comp, comp); drawCube (); GL.Flush (); previousTime = timeInterval; GL.Disable (EnableCap.DepthTest); GL.Hint (HintTarget.LineSmoothHint, HintMode.DontCare); GL.Hint (HintTarget.PolygonSmoothHint, HintMode.DontCare);
There have also been some helper methods added to support NSColor so you can use Cocoa-isms as well. You can see an example of this in the code snippet above.
Getting Started
To get started you will need to compile the MonoMac add-in from source which you can find instructions on doing that here Exploring Cocoa with MonoMac and C# – Chapter 1 – Getting Started – Part 2. this is only until the monomac team update the monomac add-in.
You will then need to add a reference to MonoMac.OpenGL in your source code.
using MonoMac.OpenGL;
You would then setup your application like any other Cocoa application using your choice of NSOpenGLView, creating your own NSOpenGLContext manually or CAOpenGLLayer.
You can find more information about apple’s implementation of opengl at the following locations.
Example Code
I have written a sample program that was used in testing which you can find here in zip form. Download the file OpenGLLayer.zip and open the OpenGLLayer.sln solution provided. Build and run the solution.
You can also access the code from the sample from MonoMac if you have setup your system to compile the MonoMac add-in from source.
There should be more samples soon.
RSS
Twitter
Feb 25, 2011 @ 13:13:01
“This is accomplished by forking a few files from OpenTK and removing their delegate / context infrastructure and just doing direct pinvokes to OpenGL.framework.”
Note that this disables support for OpenGL extensions.
“Unsupported extensions and methods have been commented out to avoid runtime exceptions.”
And this is obviously incorrect, since OpenGL versions between Mac OS X versions (OpenGL support differs even between minor revisions, like 10.6.4 to 10.6.5).
Just a word of warning.