5

Without using a multi-platform library such as GLUT or SDL.

In C (no Objective-C), how can I create a window, with a valid OpenGL context that I can draw to?

I want to be able to compile it from command line, so no XCode drag and drop stuff, please!

user2868331
  • 333
  • 4
  • 10
  • 5
    The phrases "in C (no Objective-C)" and "Preferably using Cocoa" are contradictory. – Ken Thomases Mar 16 '15 at 01:38
  • You can write your own code that uses C, but you will have to wrap part of it around Cocoa. This is how any modern framework written in C or C++ that creates a core profile context on OS X works. The amount of Objective-C you actually have to get your hands dirty with is miniscule. I am very vocal about disliking Objective-C and I was able to tolerate it to wrap NSOpenGL :P – Andon M. Coleman Mar 16 '15 at 02:15
  • Yes, it is clear that I don't understand much of what I'm talking about. So please give me a complete answer rather than vague comments. – user2868331 Mar 16 '15 at 16:16
  • @AndonM.Coleman It actually seems technically possible to use Cocoa from C++. I found an example here when I researched it some time ago: http://stackoverflow.com/a/14083212. But that was pretty much the only material I found about it, and it was not clear how viable it was for even just slightly more complex cases. In the end, wrapping the Objective-C seemed like the lesser of two evils. – Reto Koradi Mar 17 '15 at 01:58
  • 1
    @user2868331: Well the question itself is kind of vague to be honest. You can easily create an OpenGL window on OS X using AGL (a C-based Framework) and Carbon. However, you lose 64-bit support doing this and you will also be stuck in OpenGL 2.1 forever. Apple has deprecated all of the Frameworks that used to make this possible in favor of Cocoa. If you are happy with GL 2.1, you can even use GLX instead of AGL. It would really help if you explained what your requirements are (I assumed you were in a position where you did not want to limit yourself to GL 2.1, so I didn't get into specifics). – Andon M. Coleman Mar 17 '15 at 02:26

1 Answers1

3

There are few assumptions for properly understanding your question:

  1. There is an API in which your app as a whole is being developed, such as Cocoa API. Suppose this is what you meant by asking for “Preferably using Cocoa”.

  2. The basic OpenGL drawing routines are plain-C, no matter if wrapped in GLUT, Cocoa or whatsoever.

  3. There’s no such method as “Creating a window with OpenGL context” in Cocoa API, AFAIK. You first have to create a window as such, be it programmatically or be it using the Interface Builder.

You can use the Interface Builder (in Xcode) for setting up the window in which the OpenGL drawing should be displayed. The following is not a working code, it’s just a simplest case walk-through. You must then declare an interface, such as:

@interface myOpenGLView :NSOpenGLView
{
@public
//declare public members
@private
//declare private members
}
//declare properties and methods
- (void) drawRect: (NSRect) bounds;
@end

myOpenGLView *myView;

which is subclassing NSOpenGLView. Habitually this is written into a separate class .m and .h files, not in the AppDelegate.

Upon dragging the OpenGLView from the Objects Palette into your window make sure to declare it in Xcode right pane (Utilities area) as Custom Class myOpenGLView. Up to here I can’t imagine getting away without Objective-C. However,inside a method body, you still deal with traditional-style OpenGL-C.

-(void) drawRect: (NSRect) bounds
{   
glEnable(GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);    
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
drawSomething();
glFlush();    
}

The method which updates the drawing should not draw explicitly, but call the following method instead:

[self.myView setNeedsDisplay: YES];

Of course there’s much more to it, depending on the complexity of what you’re after. There are few simple examples which may help in starting your learning with OpenGL in OS X, such as: https://developer.apple.com/library/mac/documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/opengl_drawing/opengl_drawing.html

or this: Creating an OpenGL View without Interface Builder

or even this: https://www.youtube.com/watch?v=7ModmPsxhug

but there’s nothing zippy or snappy about creating OpenGL views in Cocoa. It takes a bit of learning patience which pays off. I hope this can help.

Community
  • 1
  • 1
user3078414
  • 1,942
  • 2
  • 16
  • 24