Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSOpenGLView subclass not resizing with window

I've been trying to get the NSOpenGLView to resize correctly when resizing the window, but I keep getting weird behaviour which I cannot explain. It looks like so:

Original (What it looks like at the beginning):

After resize:

This is my resize code:

- (void)reshape
{
    [super reshape];

    CGLLockContext([[self openGLContext] CGLContextObj]);

    NSRect viewRectPoints = [self bounds];

#if SUPPORT_RETINA_RESOLUTION

    NSRect viewRectPixels = [self convertRectToBacking:viewRectPoints];

#else //if !SUPPORT_RETINA_RESOLUTION

    NSRect viewRectPixels = viewRectPoints;

#endif // !SUPPORT_RETINA_RESOLUTION

    [self resizeWithWidth:viewRectPixels.size.width
                  AndHeight:viewRectPixels.size.height];

    CGLUnlockContext([[self openGLContext] CGLContextObj]);
}

- (void) resizeWithWidth:(GLuint)width AndHeight:(GLuint)height
{
    glViewport(0, 0, width, height);

    m_width = width;
    m_height = height;
}

and I'm using: "glViewport(0, 0, m_width, m_height);" whenever I call glDrawArrays();

Can anyone help?

like image 482
user4719016 Avatar asked Feb 23 '26 14:02

user4719016


1 Answers

When resizing the window you should also set the projection matrix to reflect the new dimensions. You didn't post your setup/drawing code, but usually that looks something like:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(...); // or glFrustum(), glOrtho(), gluOrtho2D(), ...
glMatrixMode(GL_MODELVIEW);
like image 195
Max Smolens Avatar answered Feb 25 '26 03:02

Max Smolens



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!