Cocoa屏幕保护程序预览

时间:2012-05-07 13:40:16

标签: cocoa opengl preview screensaver

屏幕保护程序预览有一些问题。这是一个简单的OpenGL屏幕保护程序,将BG清除为某种颜色(图像上的紫色)。它必须彩色填充全屏,它在测试运行中,但在预览时我只看到这个:

Preview box

一些代码:

- (void)initOpengl {
    [[openglView openGLContext] makeCurrentContext];
    glShadeModel(GL_SMOOTH);
    glClearColor(0.5f, 0.3f, 0.8f, 0.5f);

    glClearDepth(1.0f); 
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

- (void)resize:(NSSize)newSize {
    [openglView setFrameSize:newSize];

    [[openglView openGLContext] makeCurrentContext];

    glViewport(0, 0, (GLsizei)newSize.width, (GLsizei)newSize.height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(30.0f, (GLfloat)newSize.width / (GLfloat)newSize.height, 
                   0.1f, 100.0f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    [[openglView openGLContext] update];
    [self setNeedsDisplay:YES];
}

- (void)setFrameSize:(NSSize)newSize {
    [super setFrameSize:newSize];
    [self resize:newSize];
}
- (void)setFrame:(NSRect)frameRect {
    [super setFrame:frameRect];
    [self resize:frameRect.size];
}
- (void)drawRect:(NSRect)rect {
    [super drawRect:rect];

    [[openglView openGLContext] makeCurrentContext];
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    glFlush();
}

- (void)animateOneFrame {
    [self setNeedsDisplay:YES];
}

1 个答案:

答案 0 :(得分:0)

非常愚蠢的错误,问题解决了。 代替     [openglView setFrameSize:newSize]; 我应该用过     [openglView setFrame:...]; 使用适当的参数