2011-04-28 98 views
0

嘿,我正在爲OpenGLES渲染編寫自定義視圖。當我在界面構建器中查看視圖並將其設置爲我的呈現視圖時,我得到了它的工作,但現在我切換到使用initWithFrame創建視圖。 (注意,我可以設置視圖的背景顏色並查看它我只是不能在OpenGL中渲染任何東西,甚至沒有清晰的顏色)一切似乎都被調用,並且圖層類似乎也沒問題,它僅僅因爲某種原因不起作用除了直接使用InterfaceBuilder創建它之外。有任何想法嗎?OpenGL ES iPhone從IB切換到initWithFrame

我有這樣的設置代碼:

+ (Class) layerClass 
{ 
    return [CAEAGLLayer class]; 
} 

- (id)initWithCoder:(NSCoder*)coder 
{ 
    if (self = [super initWithCoder:coder]) 
    { 
     [self setupView]; 
    } 
    return self; 

} 

- (id)initWithFrame:(CGRect)frame 
{ 
    if (self = [super initWithFrame:frame]) 
    { 
     [self setupView]; 
    } 
    return self; 
} 

- (void)setupView 
{ 
    CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer; 

    eaglLayer.opaque = YES; 
    eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: 
            [NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil]; 

    _context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; 

    if (!_context || ![EAGLContext setCurrentContext:_context]) 
    { 
     NSLog(@"Error could not set context"); 
     [self release]; 
    } 

    _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(renderView:)]; 
    _displayLink.paused = YES; 
    _displayLink.frameInterval = FPS; 

    [_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; 

    [EAGLContext setCurrentContext:_context]; 

    [self setup]; 
} 

然後一些佈局代碼。

- (void)layoutSubviews 
{ 
    [EAGLContext setCurrentContext:_context]; 
    [self destroyBuffers]; 
    if (![self createBuffers]){ 
     NSLog(@"Failed to create framebuffer!"); 
    } 

    [self resumeRendering]; 
} 

和一些渲染代碼:

- (void)renderView:(CADisplayLink*)sender 
{ 
    [EAGLContext setCurrentContext:_context]; 

     glBindFramebuffer(GL_FRAMEBUFFER, _viewFrameBuffer); 

     glClearColor(0.5, 0.5, 0.5, 1.0); 

     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  



     GLenum attachments[] = {GL_DEPTH_ATTACHMENT, GL_COLOR_ATTACHMENT0}; 
     glDiscardFramebufferEXT(GL_READ_FRAMEBUFFER_APPLE, 2, attachments); 


     glBindRenderbuffer(GL_RENDERBUFFER, _viewRenderBuffer); 


    [_context presentRenderbuffer:GL_RENDERBUFFER]; 

} 

我想不通,爲什麼沒有被渲染爲所有相同的事情越來越調用。如果任何人都可以幫助我,那真是太棒了!

回答

0

我也許在這裏猜測太多了,但是從IB切換到initWithFrame有一個省略的步驟,那就是loadView在你的viewController中,這是你實例化你的自定義視圖類而不是標準視圖類的地方。

+0

呃是的我做到了,我使用initWithFrame並將它添加爲子視圖。就像我說過的,如果我設置背景顏色,我可以看到視圖,但我無法做任何opengl渲染,甚至沒有清晰的顏色 – 2011-04-29 20:32:11