objective-c - 带 NSOpenGLView 的 OpenGL 3.2

标签 objective-c opengl-3 nsopenglview nsopengl

如何在 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 调用,您的应用程序将会崩溃。这是一个 PDF document 3.2 规范,以便您可以看到更改。

关于objective-c - 带 NSOpenGLView 的 OpenGL 3.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11602406/

相关文章:

objective-c - 在方法名称中使用关键字的危险

c++ - 没有实例化的属性除数?

c++ - Qt 5.0.2 和 OpenGL 3+ 渲染问题

c++ - OpenGL 没有按预期绘制

objective-c - 我的 NSOpenGLView 无法工作。我什么都试过了

macos - 缩小窗口后 NSOpenglView 出现在 NSView 前面

cocoa - 使用 ImageKit 和 CGI​​mageDestination 将 NSOpenGLView 像素数据导出为某些图像文件格式时出现问题

objective-c - 在iOS编程中保存/检索数组的最佳方法是什么

ios - 轻量级迁移后在类型为 'package_number_7' 的对象上找不到属性 'User *'

objective-c - 没有显式声明的属性 ivars 会生成哪些名称?