PDA

View Full Version : Programmatically adding OpenGL view to window




Starfox
Nov 29, 2011, 09:20 AM
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.



sherpa78
Nov 29, 2011, 10:25 AM
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.

kainjow
Nov 29, 2011, 08:29 PM
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%2Ftrunk%2FCocoaGL

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.

sherpa78
Nov 29, 2011, 11:23 PM
That is not a simple project if you're starting out with Cocoa/OpenGL integration.


I'm finding that this is the case, actually. :)

Thanks for the link to your code! It will definitely help me.

Starfox
Nov 30, 2011, 02:15 AM
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?

jiminaus
Nov 30, 2011, 02:29 AM
[...] 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:

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.

[viewAlreadyInWindow addSubview:openGLView];

Starfox
Nov 30, 2011, 03:02 AM
Thanks, that worked perfectly!