macos - 如何以编程方式以特定分辨率渲染全屏 openGL?

标签 macos cocoa opengl

我正在开发一个 OSX/Cocoa 图形应用程序(出于性能原因),当用户选择“全屏”模式时,我希望以 640x480 渲染。就其值(value)而言,内容是使用 openGL 绘制的自定义 NSView。

我知道,与其实际更改用户的分辨率,不如更改后备缓冲区(如另一个 SO 问题的解释: Programmatically change resolution OS X )。

根据该建议,我最终使用以下两种方法(见下文)在全屏和窗口之间切换。问题是,当我进入全屏时,内容确实以 640x480 渲染,但没有缩放(IE 看起来好像我们停留在窗口分辨率并“缩放”到渲染的 640x480 角)。

我可能在这里遗漏了一些明显的东西 - 我想我可以根据实际的屏幕分辨率将渲染翻译为“居中”,但这似乎过于复杂?

- (void)goFullscreen{

  // Bounce if we're already fullscreen
  if(_isFullscreen){return;}

  // Save original size and position
  NSRect frame = [self.window.contentView frame];
  original_size = frame.size;
  original_position = frame.origin;

  NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                         [NSNumber numberWithBool:NO],NSFullScreenModeAllScreens,
                         nil];

  // In lieu of changing resolution, we set the backbuffer to 640x480
  GLint dim[2] = {640, 480};
  CGLSetParameter([[self openGLContext] CGLContextObj], kCGLCPSurfaceBackingSize, dim);
  CGLEnable ([[self openGLContext] CGLContextObj], kCGLCESurfaceBackingSize);

  // Go fullscreen!
  [self enterFullScreenMode:[NSScreen mainScreen] withOptions:options];
  _isFullscreen=true;

}


- (void)goWindowed{

  // Bounce if we're already windowed
  if(!_isFullscreen){return;}

  // Reset backbuffer
  GLint dim[2] = {original_size.width, original_size.height};
  CGLSetParameter([[self openGLContext] CGLContextObj], kCGLCPSurfaceBackingSize, dim);
  CGLEnable ([[self openGLContext] CGLContextObj], kCGLCESurfaceBackingSize);

  // Go windowed!
  [self exitFullScreenModeWithOptions:nil];
  [self.window makeFirstResponder:self];
  _isFullscreen=false;

}

更新

现在要做一些类似于下面 datenwolf 建议的事情,但不使用 openGL(对于非 gl 内容有用)。

// Render into a specific size
renderDimensions = NSMakeSize(640, 480);
NSImage *drawIntoImage = [[NSImage alloc] initWithSize:renderDimensions];
[drawIntoImage lockFocus];
[self drawViewOfSize:renderDimensions];
[drawIntoImage unlockFocus];
[self syphonSendImage:drawIntoImage];

// Resize to fit preview area and draw
NSSize newSize = NSMakeSize(self.frame.size.width, self.frame.size.height);
[drawIntoImage setSize: newSize];
[[NSColor blackColor] set];

[self lockFocus];
[NSBezierPath fillRect:self.frame];
[drawIntoImage drawAtPoint:NSZeroPoint fromRect:self.frame operation:NSCompositeCopy fraction:1];
[self unlockFocus];

最佳答案

使用附加了所需目标分辨率纹理的 FBO,并以所述分辨率渲染到该 FBO/纹理。然后切换到主帧缓冲区并使用之前渲染的纹理绘制全屏四边形。使用您最喜欢的任何放大滤镜。如果您想发挥更大的作用,您可以在片段着色器中实现 Lancosz/sinc 插值器来升级中间纹理。

关于macos - 如何以编程方式以特定分辨率渲染全屏 openGL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18468030/

相关文章:

使用 xdebug 的 PHPStorm 本地调试永远不会捕获并且无法验证

cocoa - 鼠标悬停时更改 NSTextField 背景

objective-c - 查询核心数据中多个子实体类型的所有对象

opengl - 什么时候应该调用 glGetError?

c++ - 用 0x 作为十六进制值的前缀

java - 如何通过导出的 app.jar 在 Mac OSX 上加载 native JOGL .jnilib 扩展?

objective-c - 如何在 Mac OS X 中过滤和覆盖文本输入

objective-c - Cocoa-touch 和 UIButton 内容

objective-c - Obj-C、NSTimer 循环调度,正确的模式?

c++ - Hook OpenGL 帮助(4 - 5 个问题)