Help with NSOpenGL: Window resizing

Tycho
Unregistered
 
Post: #1
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,
        
NSOpenGLPFADepthSize16,
        
NSOpenGLPFASingleRenderer,
        
NSOpenGLPFAScreenMaskCGDisplayIDToOpenGLDisplayMask(kCGDirectMainDisplay),
        
NSOpenGLPFANoRecovery,
        
0
    
};
    
    
NSOpenGLPixelFormat *pf = [[[NSOpenGLPixelFormat allocinitWithAttributes:attrsautorelease];
    
    
context = [[NSOpenGLContext allocinitWithFormat: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.01.00.01.0, -1.01.0);

    
glClearColor(0.00.51.01.0);
    
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
    
    
glBegin(GL_POLYGON);
        
glColor3f(1.00.00.0);
        
glVertex3f(0.250.250.0);
        
glVertex3f(0.750.250.0);
        
glVertex3f(0.750.750.0);
        
glVertex3f(0.250.750.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 
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
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...
Quote this message in a reply
Tycho
Unregistered
 
Post: #3
Thanks, that's exactly what I needed.

I simplified the original code somewhat before I posted it here, so that's why it's so strange (calling glViewport every frame, and calling it after I draw my geometry).
Quote this message in a reply
Post Reply