Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: scrolling an openGL view (with an NSScrollView)



On Monday, December 9, 2002, at 05:24 AM, Tom Davie wrote:

I think I've found a solution here, I just want to check that I am barking
up the right tree.

I am attemptying to scroll an NSOpenGLView (subclass of), the total area of
which is greater than 2048x2048. My first solution (as explained here) was
to change the viewport as I scrolled, however this limited me to 2048x2048,
so I have thought up a slightly different solution.

First I create an NSScrollView which contains a dummy NSView (which does no
rendering at all), I then put an OpenGL view over the top of the scroll view
(so that only the scroll bars show). In the OpenGL view I translate before
drawing (using glTranslate (xPan, yPan, 0.0)) and according to how much my
view is zoomed and translated I change the frame rect of the dummy NSView
behind the openGL view.

Is this a sensible approach, or is there a simpler solution?

Is 2048 the size in pixels or in gl units? If it's the latter, this reply may be garbage (I'm assuming it's the former).

I have working code for a scrolled OpenGL view in an incomplete application which you are welcome to look over. The essence of it is here:

- (void)drawRect:(NSRect)rect {

float viewLeft, viewRight, viewBottom, viewTop;
NSRect frameRect, clipViewRect;

// make sure the context knows to where it is rendering
if( contextIsSet == NO ) {
[context setView:self];
contextIsSet = YES;
}
[context makeCurrentContext];
[context update];
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

/* we define the drawing region as the percentage of the volume
which is visible in the current frame of the NSClipView (the "content" view) */
frameRect = [self frame];
clipViewRect = [clipView bounds];
viewLeft = (clipViewRect.origin.x-(frameRect.size.width*0.5)) * GL_UNITS_PER_PIXEL*zoom;
viewRight = viewLeft + clipViewRect.size.width*GL_UNITS_PER_PIXEL*zoom;
viewBottom = (clipViewRect.origin.y-(frameRect.size.height*0.5)) * GL_UNITS_PER_PIXEL*zoom;
viewTop = viewBottom + clipViewRect.size.height*GL_UNITS_PER_PIXEL*zoom;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho( viewLeft-viewOffset, viewRight-viewOffset, viewBottom+viewOffset, viewTop+viewOffset, 1.0, yon);
glViewport(0.0, 0.0, (GLsizei)clipViewRect.size.width, (GLsizei)clipViewRect.size.height);

// draw the content here

[context flushBuffer];
}

The above snippet is from the view which displays OpenGL content. "clipview" is just the superview, which is the clip view (class NSClipView) of a scroll view (NSScrollView). You have to set it in code -- it's not visible in IB. In IB I changed the class of the content view of the scroll view (which is visible) into this class -- there is no dummy view. The scrollview is adjusted using zoom in and out buttons which will enlarge the initial view size from 800x600 (pixels) to up to 16x that, without a problem. I only ever draw in the visible region. I was using an orthographic camera, but I can't see any reason you can't use a perspective camera. I haven't tried it.

I was going to point you in the direction of the whole code, but I think it would take a long time to sort out what you need from the other junk I've got in there. Even now I'm afraid I don't remember all the details of how I got this to work, and it's a lot messier than I wish it were. Given specific questions I can probably work out answers.

Brent Gulanowski
--
Mac game development news and discussion
http://www.idevgames.com
_______________________________________________
mac-opengl mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/mac-opengl
Do not post admin requests to the list. They will be ignored.

References: 
 >scrolling an openGL view (with an NSScrollView) (From: "Tom Davie" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.