objective-c – OpenGL 3.2 w / NSOpenGLView

如何在NSOpenGLView的自定义实现中创建核心配置文件?我应该覆盖哪种方法以及我应该在哪些代码添加

到目前为止,我有这个代码

// 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

解决方法

Apple有一个名为 GLEssentials的示例代码项目,它显示了如何执行此操作(请注意,它是一个Mac OS X和iOS示例代码项目).

基本上,您将需要子类化NSOpenGLView(示例代码中的NSGLView类)并使用以下方法实现awakeFromNib方法

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

另请记住,如果您使用已从3.2 API中删除的任何OpenGL API调用,您的应用程序将崩溃.这是3.2规范的PDF document,因此您可以看到更改.

相关文章

对象的传值与返回说起函数,就不免要谈谈函数的参数和返回值...
从实现装饰者模式中思考C++指针和引用的选择最近在看...
关于vtordisp知多少?我相信不少人看到这篇文章,多半是来自...
那些陌生的C++关键字学过程序语言的人相信对关键字并...
命令行下的树形打印最近在处理代码分析问题时,需要将代码的...
虚函数与虚继承寻踪封装、继承、多态是面向对象语言的三大特...