2014-09-25 67 views
2

我使用CADisplayLink render-loop回調來渲染一系列帶有openGL的圖像紋理。 稱爲CADisplayLink的第一個回調後,我只是得到這些錯誤輸出無法綁定CADisplayLink中的EAGLDrawable呈現循環

Failed to bind EAGLDrawable: <CAEAGLLayer: 0x946bb40> to GL_RENDERBUFFER 2 
Failed to make complete framebuffer object 8cd6 

我設置的渲染&幀緩衝,並呼籲glFramebufferRenderbuffer在控制器的viewDidLoad階段,glCheckFramebufferStatus的回報是在該階段的罰款。

這是我正在使用的代碼。

//GLKViewController.m 
typedef struct { 
    GLKVector3 positionCoords; 
    GLKVector2 textureCoords; 
}SceneVertex; 

static const SceneVertex vertices[] = 
{ 
    {{-1.0f, -1.0f, 0.0f}, {0.0f, 0.0f}}, // lower left corner 
    {{ 1.0f, -1.0f, 0.0f}, {1.0f, 0.0f}}, // lower right corner 
    {{-1.0f, 1.0f, 0.0f}, {0.0f, 1.0f}}, // upper left corner 
    {{ 1.0f, 1.0f, 0.0f}, {1.0f, 1.0f}}, 
}; 

@interface myViewController() //glkViewController 
@property (nonatomic) GLuint renderBuffer; 
@property (nonatomic) GLuint frameBuffer; 
@property (nonatomic) GLuint glBuffer; 

@property (nonatomic) int renderWidth; 
@property (nonatomic) int renderHeight; 

@property(strong, nonatomic) CADisplayLink* displayLink; 
@property(strong, nonatomic) EAGLContext* context; 
@end 

@implementation myViewController 

-(void)setupBuffers 
{ 

    glGenFramebuffers(1, &_frameBuffer); 
    glBindFramebuffer(GL_FRAMEBUFFER, _frameBuffer); 

    glGenRenderbuffers(1, &_renderBuffer); 
    glBindRenderbuffer(GL_RENDERBUFFER, _renderBuffer); 
    [self.context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer*)self.view.layer]; 
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, 
           GL_RENDERBUFFER, _renderBuffer); 

    glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &_renderWidth); 
    glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &_renderHeight); 

    if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { 
     NSLog(@"AAfailed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER)); 
    } 
    glGenBuffers(1,&_glBuffer); 
    glBindBuffer(GL_ARRAY_BUFFER, _glBuffer); 
    glBufferData(
       GL_ARRAY_BUFFER, // Initialize buffer contents 
       sizeof(vertices), // Number of bytes to copy 
       vertices,   // Address of bytes to copy 
       GL_STATIC_DRAW); // Hint: cache in GPU memory 
} 


-(void)loadView 
{ 
    _context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; 
    self.view = [[myView alloc] initWithFrame:[[UIScreen mainScreen] bounds] context:_context]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [EAGLContext setCurrentContext:self.context]; 
    [self setupBuffers]; 

    _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(render)]; 
    [_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
    _displayLink.frameInterval = 1; 
} 

- (void)render 
{ 
    myView *view = (myView*)self.view; 
    NSData *image = [view getOneImage]; //if nil, return or sleep&reget; 

    glBindFramebuffer(GL_FRAMEBUFFER, _frameBuffer); 
    glBindRenderbuffer(GL_RENDERBUFFER, _renderBuffer); 
    glClearColor(0.0f, 1.0f, 0.0f, 1.0f); 
    glClear(GL_COLOR_BUFFER_BIT); 
    glViewport(0, 0, _renderWidth, _renderHeight); 

    GLuint texture = -1; 
    glGenTextures(1, &texture); 

    glActiveTexture(GL_TEXTURE0); 
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 
    glBindTexture(GL_TEXTURE_2D, texture); 

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 

    glTexImage2D(
       GL_TEXTURE_2D, 0,   /* target, level */ 
       GL_RGB,     /* internal format */ 
       _renderWidth, _renderHeight, 0,   /* width, height, border */ 
       GL_RGB, GL_UNSIGNED_BYTE, /* external format, type */ 
       image.bytes      /* pixels */ 
       ); 

    glBindBuffer(GL_ARRAY_BUFFER,_glBuffer); 
    glEnableVertexAttribArray(GLKVertexAttribPosition); 
    glVertexAttribPointer(GLKVertexAttribPosition, // Identifies the attribute to use 
          3,       // number of coordinates for attribute 
          GL_FLOAT,     // data is floating point 
          GL_FALSE,     // no fixed point scaling 
          sizeof(SceneVertex),  // total num bytes stored 
          NULL+offsetof(SceneVertex, positionCoords)); 

    glBindBuffer(GL_ARRAY_BUFFER, _glBuffer); 
    glEnableVertexAttribArray(GLKVertexAttribTexCoord0); 
    glVertexAttribPointer(GLKVertexAttribTexCoord0, // Identifies the attribute to use 
          2,       // number of coordinates for attribute 
          GL_FLOAT,     // data is floating point 
          GL_FALSE,     // no fixed point scaling 
          sizeof(SceneVertex),  // total num bytes stored per vertex 
          NULL+offsetof(SceneVertex, textureCoords)); 

    glDrawArrays(GL_TRIANGLES, 0, 3); 
    glDrawArrays(GL_TRIANGLES, 1, 4); 


    glBindFramebuffer(GL_FRAMEBUFFER, 0); 
    glBindRenderbuffer(GL_RENDERBUFFER, 0); 

    [_context presentRenderbuffer:GL_RENDERBUFFER]; 
    glFlush(); 
    glDeleteTextures(1, &texture); 
} 
@end 

//GLKView 
@implementation myView 
+ (Class)layerClass { 
    return [CAEAGLLayer class]; 
} 
- (id)initWithFrame:(CGRect)frame context:(EAGLContext *)context 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     self.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888; 

     self.context = context; 
     CAEAGLLayer *layer= (CAEAGLLayer *)self.layer; 
     self.images = [[NSMutableArray alloc] init]; 
     layer.opaque = YES; 
     layer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: 
             [NSNumber numberWithBool:YES], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil]; 

     } 
    return self; 
} 

@end 


//APPDelegate 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 
    self.window.backgroundColor = [UIColor whiteColor]; 

    self.window.rootViewController = [[QCPViewController alloc]init]; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 
+0

我沒有立即看到的問題,但也有可能離開,以避免它。任何你不使用GLKView的理由?它看起來並不像你在做任何需要手動幀緩衝管理的事情。 – rickster 2014-09-25 14:03:00

+0

我之前使用GCD&GLKView手動調用[view diplay]在單獨的線程中按需渲染圖像,但由於多線程情況下的openglcontext,我也沒有弄明白。 – user3682618 2014-09-25 14:49:04

回答

0

我正在經歷同樣的事情。我正在創建並從CADisplayLink的回調中添加SCNView。這是非常滯後的,它不可見,並在控制檯中顯示錯誤。當我只是說dispatch_async(dispatch_get_main_queue(), ^{ /* code */ });然後一切都很好。

1

當您嘗試繪製當前不在屏幕上的openGL視圖時,會發生這種情況。就我而言,視圖並未安裝在我的故事板中的當前大小類中。

如果使用的是故事板,仔細檢查該視圖安裝在目前的規模類

  1. 選擇在故事板
  2. 轉到開放GL視圖屬性檢查

enter image description here

  1. 驗證視圖是否安裝在所有尺寸的類中,如

enter image description here