2012-03-14 79 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