2

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.

Ortuman
  • 21
  • 1

1 Answers1

0

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.

No one in particular
  • 2,682
  • 2
  • 17
  • 20