iDevGames Forums

Full Version: NSView OpenGL Oddity
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

I encountered this strange behavior that I've searched the Internet for days without finding a good answer.

Here is my OpenGL embedding scenario:

I have this NSView, say viewB, which is set to render an OpenGL scene. [NSOpenGLContext setView: viewB].

This View is put into another view, say viewA. [viewA addSubview: viewB].

when viewB is well within the bounds of viewA. The openGL scene is rendered correctly.

when viewB is partially outside the top or the right borders of viewA, the openGL scene is also rendered correctly with correct clipping of viewB by viewA.

However, when viewB is partially outside the bottom or the left borders of viewA, I see a strange offset of the OpenGL scene or viewport that put the scene origin within the bounds of viewA. ( as opposed to the expected behavior where scene origin is outside viewA and only the right/top half of the scene is visible.)

Has anyone seen this before? is it something simple that I've overlooked.

Appreciate any help. I am a newbie to both cocoa and opengl programming.
Where do you call glViewport, and what do you pass it?
glViewport( 0, 0, width of viewB, height of viewB);

This is called on each resize event of viewB.
NSRect r = [viewB bounds];
glViewport(0, 0, r.size.width, r.size.height);
viewB is moved programmatically using

[viewB setFrame:NSMakeRect(-25, -30, width, height)];

and glViewport is set using

glViewport( 0, 0, width, height );

where width and height are given by external routine that manages the view size.
(viewB's dimension is driven by user's interactions with the program and are kept in
a global variables.)

Basically, I let uses drag a rectangle representing viewB around viewA.

When the user double click on that rectangle, I displays the view

[viewB setFrame:NSMakeRect(-25, -30, width, height)];
glViewport( 0, 0, width, height );
[viewB setHidden:NO];
side note:

This is done in a secondary thread while both views were created on the main thread.
Confirmed independently that this happens. Not sure why or what to do about it yet.
This is a bug (or maybe a "misfeature") in NSOpenGLView. You can reproduce it trivially simply by dragging an NSOpenGLView around in Interface Builder.

I filed Radar 11239780: NSOpenGLView clips surface when origin is outside window on your behalf, but you should probably file a bug too so you have some means of tracking it.

You can work around this behavior by altering the arguments to glViewport (and possibly your projection) to compensate.
Layer-backed NSOpenGLViews do not have this problem.
Thank you for spending time looking into this issue.

I am not familiar with Layer-backed NSView.
Setting NSView setWantsLayer to YES and the scene is rendered as garbage from frame buffer.
Reference URL's