Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Starfox

macrumors 6502
Original poster
Apr 7, 2011
256
9
What's the proper way to programmatically instantiate and add an OpenGL view to a window? I'm doing a simple GUI application but I'd like to create it programmatically so I can ask for Core 3.2 profile and fallback to 2.x if that's not available.
 
Same issue here

Looks like we're trying to figure out the same thing.

I've got a simple project where I just want a window with an NSOpenGLView covering the entire window contentView colored blue and a triangle displayed. The window displays, but I see no NSOpenGLView content.

I've got my project here: http://www.2shared.com/file/XEUDnkaJ/binaryinsomnia.html

Hopefully, I'm not threadjacking. I think we, basically, have the same question.

Thanks!

----------

BTW, I used the code found in https://github.com/beelsebob/Cocoa-GL-Tutorial to help me get started on my as-of-yet-flawed project.
 
I've got a simple project where I just want a window with an NSOpenGLView covering the entire window contentView colored blue and a triangle displayed. The window displays, but I see no NSOpenGLView content.

That is not a simple project if you're starting out with Cocoa/OpenGL integration.

Try my sample project here which just displays a black background and a red triangle:
http://code.google.com/p/kaincode/source/browse/#svn/trunk/CocoaGL

Then build upon that slowly.

----------

What's the proper way to programmatically instantiate and add an OpenGL view to a window? I'm doing a simple GUI application but I'd like to create it programmatically so I can ask for Core 3.2 profile and fallback to 2.x if that's not available.

Assuming you're setting up the 3.2 vs 2.x check via NSOpenGLPixelFormat, its initWithAttributes: will return nil if it can't initialize with the attributes given, so you could create a method to check for that and revert to 2.x settings if the 3.2 attributes fail.

An OpenGL view is an NSView, so it's added to a window just like any other view: create the window, create the OpenGL view, and set it as the window's content view, then show the window.
 
My problem isn't initializing OpenGL etc. - my problem is that I want to programmatically (in applicationDidFinishLaunching:) add an NSOpenGLView* to my window. I know how to alloc and init it with pixel format attributes, but now that I have this NSOpenGLView* and NSWindow*, how can I "insert" the view inside the window?
 
[...] now that I have this NSOpenGLView* and NSWindow*, how can I "insert" the view inside the window?


If the NSOpenGLView is to be the sole content in the window then:
Code:
window.content = openGLView; // or [window setContent:openGLView];

If there is to be other content in the window, then you'd added it as subview of a view already in the window.
Code:
[viewAlreadyInWindow addSubview:openGLView];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.