iDevGames Forums
NSView OpenGL Oddity - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: NSView OpenGL Oddity (/thread-9982.html)



NSView OpenGL Oddity - mfdesigner - Apr 12, 2012 11:17 AM

Hi,

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.


RE: NSView OpenGL Oddity - OneSadCookie - Apr 12, 2012 11:27 AM

Where do you call glViewport, and what do you pass it?


RE: NSView OpenGL Oddity - mfdesigner - Apr 12, 2012 11:34 AM

glViewport( 0, 0, width of viewB, height of viewB);

This is called on each resize event of viewB.


RE: NSView OpenGL Oddity - OneSadCookie - Apr 12, 2012 01:28 PM

via
Code:
NSRect r = [viewB bounds];
glViewport(0, 0, r.size.width, r.size.height);
?


RE: NSView OpenGL Oddity - mfdesigner - Apr 12, 2012 01:53 PM

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];


RE: NSView OpenGL Oddity - mfdesigner - Apr 12, 2012 02:09 PM

side note:

This is done in a secondary thread while both views were created on the main thread.


RE: NSView OpenGL Oddity - OneSadCookie - Apr 12, 2012 02:28 PM

Confirmed independently that this happens. Not sure why or what to do about it yet.


RE: NSView OpenGL Oddity - OneSadCookie - Apr 12, 2012 02:52 PM

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.


RE: NSView OpenGL Oddity - mfdesigner - Apr 12, 2012 08:20 PM

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.


RE: NSView OpenGL Oddity - OneSadCookie - Apr 13, 2012 10:59 AM

http://stackoverflow.com/questions/7133290/setwantslayer-causes-distortion-to-nsopenglview