Objective-c – Would like to learn how to use OpenGL within Objective-C, but have no idea where to start. Help?


I have zero experience with OpenGL and a small amount of experience with Objective-C, but I'm fairly decent with C++. What resources should I be looking at to start learning how to use OpenGL within Objective-C?

I read somewhere at some point that starting out with NSOpenGLView is a good start.

Best Solution

Honestly, you're probably not going to need to know much Objective-C for dealing with OpenGL, just C. OpenGL is C-based, so you don't need to learn anything new, language-wise, to deal with it. Objective-C knowledge is only really necessary when you plan on using Cocoa to build up your interface. Even then, the language is not hard to pick up if you're coming from a solid C / C++ background.

I highly recommend the book iPhone 3D Programming that Matt's first resource is based on. Even though you're asking about desktop OpenGL, and this book covers OpenGL ES, much is shared between the two APIs. The book does a great job of starting with simple concepts and fundamentals, and then building to more advanced topics like environment mapping and custom shaders. The author uses C++ as his base language for the book, so you should be familiar with even the most complex code he shows. OpenGL ES is effectively a subset of OpenGL, so almost everything translates across to the desktop.

Within a desktop Cocoa application, you have two ways of presenting OpenGL content: NSOpenGLView and CAOpenGLLayer. The former is an older NSView subclass that you can customize to place your rendering code within. The latter is a Core Animation CALayer that also acts as an OpenGL rendering target, but it give you a little more flexibility in how you can overlay other items on top of the OpenGL content. Getting the display set up for your OpenGL rendering will not take a lot of effort, with most of your time being spent on your OpenGL code.

You might want to pick apart some of Apple's sample applications, such as GLSL Showpiece, Cocoa OpenGL, GLEssentials, and CubePuzzle, among the other OpenGL examples they have in the developer center.

Related Question