创建 NSOpenGLView 时出错

发布于 2024-10-01 05:37:53 字数 2214 浏览 0 评论 0原文

我正在尝试使用 Cocoa API 创建一个 OpenGL 应用程序,现在一切正常,除了一件事。当调整主窗口大小时,GL 上下文似乎没有正确更新。

这是用于设置窗口和渲染上下文的代码。

主窗口:

m_window = [[NSWindow 分配] initWithContentRect:window_rect 样式掩码:( NSClosableWindowMask | NSMiniaturizedWindowMask | NSMiniaturizedWindowMask NSTitleWindowMask | NSTitleWindowMask | NSTitleWindowMask NSResizableWindowMask ) backing:NSBackingStoreBuffered defer:YES];

window_title = [NSString stringWithCString:m_window_title->c_str() 
                                  encoding:NSUTF8StringEncoding];

window_color = [NSColor colorWithCalibratedRed:m_window_color->getRed()
                                         green:m_window_color->getGreen()
                                          blue:m_window_color->getBlue()
                                         alpha:m_window_color->getAlpha()];

if ( m_centered )
{
    [m_window center];
}

[m_window setTitle:window_title];
[m_window setBackgroundColor:window_color];
[m_window setOneShot:YES];
[m_window setOpaque:YES];
[m_window setDelegate:window_delegate];
[m_window setAcceptsMouseMovedEvents:YES]; 

OpenGL 视图:

NSOpenGLPixelFormatAttribute attrs[] = {NSOpenGLPFADoubleBuffer, NSOpenGLPFAWindow, NULL};

pixel_format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];

m_view = [[NSOpenGLView alloc] initWithFrame:[m_window contentRectForFrameRect:[m_window frame]]
                                 pixelFormat:pixel_format];
[pixel_format release];

GLint dim[2] = {GB_DEF_WINDOW_WIDTH, GB_DEF_WINDOW_HEIGHT};    
CGLContextObj ctx = (CGLContextObj) [[m_view openGLContext] CGLContextObj];
CGLSetParameter(ctx, kCGLCPSurfaceBackingSize, dim);
CGLEnable (ctx, kCGLCESurfaceBackingSize);

[[m_view openGLContext] makeCurrentContext];
[m_window setContentView:m_view]; 

这应该可以工作,但是当调整窗口大小以及更新 OpenGL 视图和视口后,后台缓冲区内容似乎不会正确地重新缩放。这有什么问题吗?

感谢您的回复。

I'm trying to create an OpenGL application using Cocoa API, and by now everything works fine, except for one thing. When the main window is resized the GL context seems not be updated properly.

Here's is the code used to setup window and render context.

Main window:


m_window = [[NSWindow alloc] initWithContentRect:window_rect
styleMask:( NSClosableWindowMask |
NSMiniaturizableWindowMask |
NSTitledWindowMask | NSResizableWindowMask )
backing:NSBackingStoreBuffered defer:YES];

window_title = [NSString stringWithCString:m_window_title->c_str() 
                                  encoding:NSUTF8StringEncoding];

window_color = [NSColor colorWithCalibratedRed:m_window_color->getRed()
                                         green:m_window_color->getGreen()
                                          blue:m_window_color->getBlue()
                                         alpha:m_window_color->getAlpha()];

if ( m_centered )
{
    [m_window center];
}

[m_window setTitle:window_title];
[m_window setBackgroundColor:window_color];
[m_window setOneShot:YES];
[m_window setOpaque:YES];
[m_window setDelegate:window_delegate];
[m_window setAcceptsMouseMovedEvents:YES]; 

OpenGL view:


NSOpenGLPixelFormatAttribute attrs[] = {NSOpenGLPFADoubleBuffer, NSOpenGLPFAWindow, NULL};

pixel_format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];

m_view = [[NSOpenGLView alloc] initWithFrame:[m_window contentRectForFrameRect:[m_window frame]]
                                 pixelFormat:pixel_format];
[pixel_format release];

GLint dim[2] = {GB_DEF_WINDOW_WIDTH, GB_DEF_WINDOW_HEIGHT};    
CGLContextObj ctx = (CGLContextObj) [[m_view openGLContext] CGLContextObj];
CGLSetParameter(ctx, kCGLCPSurfaceBackingSize, dim);
CGLEnable (ctx, kCGLCESurfaceBackingSize);

[[m_view openGLContext] makeCurrentContext];
[m_window setContentView:m_view]; 

This should work, however when the window is resized and after updating OpenGL view and the viewport, the back buffer content seems not to be rescaled correctly. What's wrong with that?

Thanks for your replies.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

誰認得朕 2024-10-08 05:37:53

以下控制后台缓冲区的大小:

GLint dim[2] = {GB_DEF_WINDOW_WIDTH, GB_DEF_WINDOW_HEIGHT};

如何更改这些将影响后台缓冲区的大小。检查您的代码以设置这些值。当您调整大小时,假设您使窗口变薄,那么您必须更改宽度值,使其具有与窗口相同的纵横比。

请记住,后台缓冲区的内容不需要与显示它们的最终视图的大小相同。如果存在差异,则缓冲区的内容将被缩放以适应。如果纵横比不同,您可能会出现奇怪的拉伸。

The following controls the size of the back buffer:

GLint dim[2] = {GB_DEF_WINDOW_WIDTH, GB_DEF_WINDOW_HEIGHT};

How you change these will affect the size of the back buffer. Check your code for setting these values. When you resize, say you make the window thinner, then you'll have to change your width value so that it has the same aspect ratio as the window.

Remember that the contents of the back buffer don't need to be the same size as the final view where they are displayed. If there is a difference then the contents of the buffer will be scaled to fit. If the aspect ratios are different you could end up with strange stretching.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文