2012-03-14 49 views
4

我写了一个类TestingView是NSOpenGLView的子类,并将其添加到通过Interface Builder中NSOpenGLView没有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 

NSWindow和正常工作。但在我的第二个项目中,我不能使用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作为子视图添加?

+0

我想你应该完成您的问题或删除。 – Mark 2012-03-14 14:43:03

回答

2

我在那一块上砸了几下头。我发现我的照片格式正在发挥作用。这里就是我想要做的:

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

你可以在这里结帐我的全面实施(无界面生成器在所有):https://gitorious.org/glengine/glengine

+0

是的,我曾尝试过你提出的解决方案,它的工作。不过,我有两个视图(并不精确),而TestingView(NSOpenGLView)必须是NSView的子视图。 – pawelropa 2012-03-16 06:16:18