pawelropa
pawelropa

Reputation: 1439

NSOpenGLView without Interface Builder

I've written a class TestingView which is a subclass of NSOpenGLView, and added it to NSWindow through Interface Builder

@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

and it works fine. However in my second project I can not use Interface Builder, so I do it like this,

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

and it doesn't work. OpengGLContext doesn't initialize properly. I also tried

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;

and OpenGL still doesn't work. How can I add NSOpenGLView as a subview without using Interface Builder?

Upvotes: 4

Views: 1807

Answers (1)

Sergio Moura
Sergio Moura

Reputation: 4942

I smashed my head a few times on that one. I found out that my pixformat was in the way. Here's what I'd do:

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

You can checkout my full implementation (no Interface Builder at all) here: https://gitorious.org/glengine/glengine

Upvotes: 2

Related Questions