iOS OpenGL ES 2.0渲染图像

时间:2014-01-14 20:08:33

标签: ios opengl-es

所以我试图使用OpenGL ES 2.0将多边形渲染到渲染缓冲区,但我从渲染缓冲区获得的图像只是清晰的颜色。当对象在GLKView中时,它们会完美呈现,但在渲染缓冲区中被称为“内部”时则不会。我在stackoverflow上已经阅读了很多其他主题但是找不到解决方案。我怎样才能在缓冲区中绘制内容?这是源代码(某些部分来自堆栈溢出):

@interface PreviewRenderer() {
    EAGLContext* myContext;
    GLuint framebuffer;
    GLuint colorRenderbuffer;

    PolygonShader *polygonShader;
    Polygon *polyg;


    int width;
    int height;
}

@end

@implementation PreviewRenderer
- (id) init
{
    self = [super init];
    if (self)
    {
        myContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
        [EAGLContext setCurrentContext:myContext];
        width = 100;
        height = 100;
        [self setupOpenGL];
        [EAGLContext setCurrentContext:nil];

    }
    return self;
}

-(void) setupOpenGL
{

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

    glGenRenderbuffers(1, &colorRenderbuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8_OES, width, height);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer);

    GLuint depthRenderbuffer;
    glGenRenderbuffers(1, &depthRenderbuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderbuffer);

    GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER) ;
    if(status != GL_FRAMEBUFFER_COMPLETE) {
        NSLog(@"failed to make complete framebuffer object %x", status);
    }       
}

-(UIImage *) renderImage
{
    [EAGLContext setCurrentContext:myContext];
    glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
    glClearColor(0, 1, 0, 1);
    glClear(GL_COLOR_BUFFER_BIT);


    // rendering here
    // code that works when being called in glkview
    polygonShader = [[PolygonShader alloc] initPolygonShader];
    GLKMatrix4 matr = GLKMatrix4MakeOrtho(0, 100, 0, 100, -512, 512);
    BlockPolygon poly;
    poly.type = 0;
    poly.anchorX = 100;
    poly.anchorY = 100;
    poly.glTriangleCount = 1;
    poly.glTriangles = malloc(sizeof(TriangleStruct));
    poly.glTriangles[0].a.x = 0;
    poly.glTriangles[0].a.y = 0;
    poly.glTriangles[0].b.x = 0;
    poly.glTriangles[0].b.y = 100;
    poly.glTriangles[0].c.x = 100;
    poly.glTriangles[0].c.y = 100;
    polyg = [[Polygon alloc] initForPreviewWithBlockPolygon:&poly shader:polygonShader];
    [polyg updateWithMatrix:matr];
    [polyg render];

    // grabbing image from FBO


    GLint backingWidth, backingHeight;

    glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &backingWidth);
    glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &backingHeight);

    NSInteger x = 0, y = 0;
    NSInteger dataLength = width * height * 4;
    GLubyte *data = (GLubyte*)malloc(dataLength * sizeof(GLubyte));

    glPixelStorei(GL_PACK_ALIGNMENT, 4);
    glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);

    CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, data, dataLength, NULL);
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    CGImageRef iref = CGImageCreate(width, height, 8, 32, width * 4, colorspace, kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast,
                                    ref, NULL, true, kCGRenderingIntentDefault);


    UIGraphicsBeginImageContext(CGSizeMake(width, height));
    CGContextRef cgcontext = UIGraphicsGetCurrentContext();
    CGContextSetBlendMode(cgcontext, kCGBlendModeCopy);
    CGContextDrawImage(cgcontext, CGRectMake(0.0, 0.0, width, height), iref);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    free(data);
    CFRelease(ref);
    CFRelease(colorspace);
    CGImageRelease(iref);

    [EAGLContext setCurrentContext:nil];

    return image;
}
@end

如果你想我可以发布其他文件但是因为他们在GLKView中工作我不认为这些文件存在错误。

0 个答案:

没有答案