在OpenGL屏幕保护程序中使用HiDPI渲染(Retina)

时间:2012-12-27 00:09:03

标签: objective-c opengl xcode4.5 retina-display screensaver

几年来,我一直在使用Chris移植到Mac OS X的Noof屏幕保护程序:

Skoroworld

我在10.8下运行良好并且已经使用高分辨率艺术品和其他一些调整更新到prefs面板的项目以使事情正常工作但是我似乎无法弄清楚如何使它能够使用更高分辨率的渲染Retina显示屏上像素加倍的东西。下面的代码似乎是在2880x1800处绘制窗口,但它仍然是像素加倍。

我已经粘贴了我认为代码的相关部分。


CODE

static void
reshape_noof(int w, int h)
{
     glViewport(0, 0, w, h);
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 if (w <= h) {
      wd = 1.0;
      ht = (GLfloat) h / (GLfloat) w;
      glOrtho(0.0, 1.0,
                0.0, 1.0 * (GLfloat) h / (GLfloat) w,
                -16.0, 4.0);
 } else {
      wd = (GLfloat) w / (GLfloat) h;
      ht = 1.0;
      glOrtho(0.0, 1.0 * (GLfloat) w / (GLfloat) h,
                0.0, 1.0,
                -16.0, 4.0);
 }
 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();

 glClearColor( 0.0, 0.0, 0.0, 1.0 );
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}

SNIP

// ScreenSaverView methods

- (id)initWithFrame:(NSRect)frame isPreview:(BOOL)isPreview
{
self = [super initWithFrame:frame isPreview:isPreview];
if (self) {
      [self loadSettings];
    [self setAnimationTimeInterval:1.0/delay];

      NSRect newFrame = frame;
      newFrame.origin.x = 0.0;
      newFrame.origin.y = 0.0;

      glView = [[NSOpenGLView alloc] initWithFrame:newFrame pixelFormat:[NSOpenGLView defaultPixelFormat]];
    if( glView ) {
        [self setAutoresizesSubviews:YES];
           [self addSubview:glView];
      }
 }

return self;
}

- (void)startAnimation
{
[super startAnimation];

 // Load our settings
 [self loadSettings];

 // Turn off the cursor; this doesn't always seem to happen on Tiger. :-(
 if( ![self isPreview] ) [NSCursor hide];

 firstRun = YES;

 // Prepare OpenGL.
 [[glView openGLContext] makeCurrentContext];
[self setWantsBestResolutionOpenGLSurface:YES];
[self convertRectToBacking:[self bounds]];
// Get view dimensions in pixels
NSRect backingBounds = [self convertRectToBacking:[self bounds]];

GLsizei backingPixelWidth  = (GLsizei)(backingBounds.size.width),
backingPixelHeight = (GLsizei)(backingBounds.size.height);
 float w = backingPixelWidth;
 float h = backingPixelHeight;
 reshape_noof( w, h );

 // from init_noof
 glEnable(GL_LINE_SMOOTH);
 glShadeModel(GL_FLAT);
 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 int i;
 for (i = 0; i < N_SHAPES; i++)
      initshapes(i);

 //[self animateOneFrame];
 glClearColor( 0.0, 0.0, 0.0, 0.0 );
 glClear( GL_COLOR_BUFFER_BIT );
 glFlush();
}

- (void)stopAnimation
{
 // Turn on the cursor.
 if( ![self isPreview] ) [NSCursor unhide];

[super stopAnimation];
}

- (void)animateOneFrame
{
 int i;

 if( firstRun ) {
      // Make **** sure our view port is correct.
    [self setWantsBestResolutionOpenGLSurface:YES];
    [self convertRectToBacking:[self bounds]];
    // Get view dimensions in pixels
    NSRect backingBounds = [self convertRectToBacking:[self bounds]];

    GLsizei backingPixelWidth  = (GLsizei)(backingBounds.size.width),
    backingPixelHeight = (GLsizei)(backingBounds.size.height);

    // Set viewport
      reshape_noof( backingPixelWidth, backingPixelHeight );
      firstRun = NO;
 }

 gravity( -2.0 );
 for( i = 0; i < N_SHAPES; i++ ) {
      motionUpdate( i );
      colorUpdate( i );
      drawleaf( i );
 }

 glFinish();
}

1 个答案:

答案 0 :(得分:2)

初始化[self setWantsBestResolutionOpenGLSurface:YES];

后,我会将行NSOpenGLView移到右侧
glView = [[NSOpenGLView alloc] initWithFrame:newFrame pixelFormat:[NSOpenGLView defaultPixelFormat]];
[glView setWantsBestResolutionOpenGLSurface:YES];

至少我的屏幕保护程序是这样的,它似乎有效。