• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 在 CADisplayLink 渲染循环中绑定(bind) EAGLDrawable 失败

[复制链接]
菜鸟教程小白 发表于 2022-12-13 02:14:32 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我使用 CADisplayLink 渲染循环回调来渲染一系列带有 openGLes 的图像纹理。 在调用 CADisplayLink 的第一个回调后,我得到了这些错误输出

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

我在controller的viewDidLoad阶段设置了renderBuffer&frameBuffer并调用glFramebufferRenderbuffer,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 fromDrawableCAEAGLLayer*)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 selectorselector(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)initWithFrameCGRect)frame contextEAGLContext *)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)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)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;
}



Best Answer-推荐答案


当您尝试绘制当前不在屏幕上的 openGL View 时会发生这种情况。在我的例子中,我的 Storyboard中的当前尺寸类中没有安装 View 。

如果您使用 Storyboard,请仔细检查 View 是否安装在当前大小类中

  1. 在 Storyboard 中选择打开的 GL View
  2. 转到属性检查器

enter image description here

  1. 验证 View 是否安装在所有尺寸类别中,如下所示

enter image description here

关于ios - 在 CADisplayLink 渲染循环中绑定(bind) EAGLDrawable 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26039275/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap