Help with NSOpenGL: Window resizing
I've created a basic NSView subclass that creates an NSOpenGLContext and attatches it to itself. This works great, except that resizing the view is totally broken. Anything outside the view's original frame is filled with random garbage. When I minimize the window to the dock and restore it, it has fixed itself... until I resize it again, of course. I'm calling glViewport every frame, so I really don't know what the problem is. There must be something really simple I'm forgetting to do, but I've been trying to fix this for three days and I've simply run out of ideas. Here's the code.
PHP Code:
#import "GLView.h"
#import <OpenGL/gl.h>
#import <OpenGL/glu.h>
@implementation GLView
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (!self) {
NSLog(@"Hello, my name is GLView, and I don't exist.");
return nil;
}
NSOpenGLPixelFormatAttribute attrs[] =
{
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAAccelerated,
NSOpenGLPFADepthSize, 16,
NSOpenGLPFASingleRenderer,
NSOpenGLPFAScreenMask, CGDisplayIDToOpenGLDisplayMask(kCGDirectMainDisplay),
NSOpenGLPFANoRecovery,
0
};
NSOpenGLPixelFormat *pf = [[[NSOpenGLPixelFormat alloc] initWithAttributes:attrs] autorelease];
context = [[NSOpenGLContext alloc] initWithFormat:pf shareContext:nil];
[NSTimer scheduledTimerWithTimeInterval:0.0
target:self
selector:@selector(draw:)
userInfo:nil
repeats:YES];
return self;
}
- (void)draw:(id)sender {
[self setNeedsDisplay:YES];
}
- (void)drawRect:(NSRect)rect {
static BOOL firstRender = YES;
if(firstRender) {
[context setView:self];
firstRender = NO;
}
[context makeCurrentContext];
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
glClearColor(0.0, 0.5, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.25, 0.25, 0.0);
glVertex3f(0.75, 0.25, 0.0);
glVertex3f(0.75, 0.75, 0.0);
glVertex3f(0.25, 0.75, 0.0);
glEnd();
glViewport([self bounds].origin.x, [self bounds].origin.y, [self bounds].size.width, [self bounds].size.height);
[context flushBuffer];
}
- (void)dealloc {
[context release];
}
@end
You shouldn't need to call Viewport every frame, only when the window is resized. (also, you should call it before you draw anything).
You need to call -update on your OpenGL context when the view it's attached to resizes. See the NSOpenGLContext docs for more info...
You need to call -update on your OpenGL context when the view it's attached to resizes. See the NSOpenGLContext docs for more info...
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| multiple nsopengl subviews problem | NelsonMandella | 0 | 1,827 |
Nov 9, 2009 09:44 PM Last Post: NelsonMandella |
|
| When to create custom OpenGL view instead of subclass NSOpenGL view | Coyote | 37 | 16,904 |
Oct 20, 2009 08:16 PM Last Post: Coyote |
|
| bug in OpenGL setup for NSOpenGL subclass (I think) | backslash | 2 | 3,259 |
Jul 18, 2007 01:10 PM Last Post: backslash |
|
| Copying from OpenGL window to other window | Ingemar | 7 | 4,066 |
Nov 4, 2006 03:50 AM Last Post: OneSadCookie |
|
| resizing offscreen drawable from 2nd thread | arekkusu | 11 | 4,793 |
Feb 8, 2005 09:09 AM Last Post: TomorrowPlusX |
|

