创建NSOpenGLView时出错

时间:2010-11-07 22:37:00

标签: cocoa macos opengl nsopenglview

我正在尝试使用Cocoa API创建一个OpenGL应用程序,到目前为止一切正常,除了一件事。调整主窗口大小时,GL上下文似乎无法正确更新。

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

主窗口:

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]; 

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视图和视口后,后台缓冲区内容似乎无法正确重新调整。这有什么问题?

感谢您的回复。

1 个答案:

答案 0 :(得分:0)

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

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

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

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