Reputation: 1149
I have been researching OpenGL programming on Mac OS X. While a fair amount of information on the topic exists for those programming in plain C, C++, and even Python, very little is out there for Objective-C/Cocoa developers, and much what exists is horrendously out of date (we're talking 2002-2006, an eternity ago in technology). Because of this, it's difficult to find accurate, up-to-date information, leading me to pose this question: Through what route do Obj-C/Cocoa developers most often make use of OpenGL (specifically in 3D rendering) these days?
Also, I am led to believe that there are no Objective-C/Cocoa bindings for much of OpenGL. Is this true?
I can learn another language if necessary, but I would much prefer to stick to Obj-C if at all possible, allowing me to focus time spent learning OpenGL rather than an entire programming language (C++ looks particularly intimidating).
Upvotes: 0
Views: 2435
Reputation: 154
Since Objective-C is a superset of C, you can freely include/call C codes in/from Objective-C code. Similarly, Objective-C++ (with .mm extension) can allow C++ modules in Onjective-C++. I believe this is the main reason Cocoa does not provide a wrapper for OpenGL API.
I would keep separate the whole OpenGL part from a Cocoa project using MVC design. That is, write a Model component containing all OpenGL codes written in C/C++, and make Controllere and View components within Cocoa, for example, NSOpenGLView can be Controller and View component.
The main advantage of MVC is Model component (OpenGL part) is completely independent from operating systems, and it can be reusable in other platforms without any modification. The disadvantage is you need to use both C/C++ and Objectivie-C in Cocoa environment.
I have an example of OpenGL GUI program using MVC design (with source codes). The same OpenGL module is used on both Windows and Mac version.
Upvotes: 1
Reputation: 1365
As of now, there is no extensive Objective-C binding to OpenGL. If you have an Apple developer account, I would recommend you check out this year's WWDC videos to find out if this is changing in the very near future.
If you want to check out how to use OpenGL 3.2 from Objective-C/Cocoa, have a look at the tutorial I created a while ago.
Upvotes: 2
Reputation: 5539
As far I know there's no Obj-C wrapper for OpenGL functions so you are forced to use C API. Besides, trying to wrap it in objects would be useless because writing 3D graphics is all about performance, and using C functions is much faster than using objects' methods.
Objective-C is based on C so you can freely use C OpenGL API in your Cocoa applications. As for binding, in Cocoa you can subclass NSOpenGLView to bind to OpenGL. You can override methods such as:
- (void)prepareOpenGL;
to generate textures, set drawing states, etc.
- (void)reshape;
to update your projection when view is resized.
And most important, you can override:
- (void)drawRect:
to do your custom drawing.
Upvotes: 3