我在屏幕保护程序预览方面遇到了一些问题。这是一个简单的 OpenGL 屏幕保护程序,将背景清除为某种颜色(图像上的紫色)。它必须全屏填充颜色,并且在测试运行中确实如此,但在预览时我只看到这个:
一些代码:
- (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];
}