objective-c – 没有Interface Builder的NSOpenGLView

我写了一个类TestingView,它是NSOpenGLView的子类,并通过Interface Builder将它添加到NSWindow

@implementation TestingView

- (void)prepareOpenGL 
{
    [super prepareOpenGL];

    glGenRenderbuffers( NumRenderbuffers, renderbuffer ); 
    glBindRenderbuffer( GL_RENDERBUFFER, renderbuffer[Color] ); 
    glRenderbufferStorage( GL_RENDERBUFFER, GL_RGBA, 1024, 768 );

    glBindRenderbuffer( GL_RENDERBUFFER, renderbuffer[Depth] ); 
    glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, 1024, 768);
    glGenFramebuffers( 1, &framebuffer ); 
    glBindFramebuffer( GL_DRAW_FRAMEBUFFER, framebuffer );
    glFramebufferRenderbuffer( GL_DRAW_FRAMEBUFFER,
                          GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,
                          renderbuffer[Color] );
    glFramebufferRenderbuffer( GL_DRAW_FRAMEBUFFER,
                          GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, renderbuffer[Depth] );
    glEnable( GL_DEPTH_TEST );
}

- (void)show 
{   
    glBindFramebuffer( GL_READ_FRAMEBUFFER, framebuffer ); 
    glBindFramebuffer( GL_DRAW_FRAMEBUFFER, 0 ); 
    glViewport(0, 0, self.window.frame.size.width, self.window.frame.size.height); 
    glClearColor( 0.0, 0.0, 0.0, 0.0 );
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glBlitFramebuffer( 0, 0, 1024, 768, 0, 0, 1024, 768, GL_COLOR_BUFFER_BIT, GL_NEAREST );
    glSwapAPPLE();

}

- (void)draw 
{
    /* Prepare to render into the renderbuffer */ 
    glBindFramebuffer( GL_DRAW_FRAMEBUFFER, framebuffer ); 
    glViewport( 0, 0, 1024, 768 );

    /* Render into renderbuffer */ 
    glClearColor( 1.0, 1.0, 0.0, 1.0 ); 
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); 
}

@end

它工作正常.但是在我的第二个项目中我不能使用Interface Builder,所以我这样做,

TestingView *testingView = [[TestingView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
[self.view addSubview:testingView];

它不起作用. OpengGLContext无法正确初始化.我也试过了

GLuint pixAttribs[] =
{
    NSOpenGLPFAWindow,         // choose among pixelformats capable of rendering to windows
    NSOpenGLPFAAccelerated,    // require hardware-accelerated pixelformat
    NSOpenGLPFADoubleBuffer,   // require double-buffered pixelformat
    NSOpenGLPFAColorSize, 24,  // require 24 bits for color-channels
    NSOpenGLPFAAlphaSize, 8,   // require an 8-bit alpha channel
    NSOpenGLPFADepthSize, 24,  // require a 24-bit depth buffer
    NSOpenGLPFAMinimumPolicy,  // select a pixelformat which meets or exceeds these requirements
    0
};

NSOpenGLPixelFormat *pixFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:pixAttribs];
NSOpenGLContext *context = [[NSOpenGLContext alloc] initWithFormat:pixFormat shareContext:nil];
TestingView *testingView = [[TestingView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768) pixelFormat:pixFormat];
testingView.openGLContext = context;

和OpenGL仍然无法正常工作.如何在不使用Interface Builder的情况下将NSOpenGLView添加为子视图?

最佳答案 我对那个人砸了几下头.我发现我的pixformat挡路了.这是我要做的:

// Assuming you have a self.window initialized somewhere
TestingView *testingView = [[TestingView alloc] initWithFrame:self.window.frame pixelFormat:nil];
[self.window setContentView:testingView];

你可以在这里查看我的完整实现(根本没有Interface Builder):https://gitorious.org/glengine/glengine

点赞