OpenGL 3.2 w/ NSOpenGLView
Asked Answered
C

1

7

How would I create a core profile in a custom implementation of NSOpenGLView? Which method should I override and what code should I put there?

So far I have this code:

// Header File
#import <Cocoa/Cocoa.h>

@interface TCUOpenGLView : NSOpenGLView

@end

// Source File
#import "TCUOpenGLView.h"
#import <OpenGL/gl.h>

@implementation TCUOpenGLView

- (void)drawRect:(NSRect)dirtyRect {
    glClear(GL_COLOR_BUFFER_BIT);
    glFlush();
}

@end
Contain answered 22/7, 2012 at 17:24 Comment(0)
D
12

Apple has a sample code project called GLEssentials that shows exactly how to do this (note that it is a Mac OS X and iOS sample code project).

Essentially you will need to subclass NSOpenGLView (the NSGLView class in the sample code) and implement the awakeFromNib method using the following:

- (void) awakeFromNib
{   
    NSOpenGLPixelFormatAttribute attrs[] =
    {
        NSOpenGLPFADoubleBuffer,
        NSOpenGLPFADepthSize, 24,
        // Must specify the 3.2 Core Profile to use OpenGL 3.2
        NSOpenGLPFAOpenGLProfile,
        NSOpenGLProfileVersion3_2Core,
        0
    };

    NSOpenGLPixelFormat *pf = [[[NSOpenGLPixelFormat alloc] initWithAttributes:attrs] autorelease];

    if (!pf)
    {
        NSLog(@"No OpenGL pixel format");
    }

    NSOpenGLContext* context = [[[NSOpenGLContext alloc] initWithFormat:pf shareContext:nil] autorelease];

    [self setPixelFormat:pf];

    [self setOpenGLContext:context];
}

Also remember that if you use any OpenGL API calls that have been removed from the 3.2 API your app will crash. Here is a PDF document of the 3.2 spec so you can see the changes.

Douty answered 13/8, 2012 at 14:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.