注册 登录
主题 : 如何对OpenGLES开启采样后进行截屏
级别: 侠客
UID: 34173
精华: 0
发帖: 79
可可豆: 738 CB
威望: 718 点
在线时间: 470(时)
注册时间: 2010-10-17
最后登录: 2018-07-18
0 楼:  发表于: 2018-07-11 15:03    发自: Web Page
来源于 一般提问 分类

如何对OpenGLES开启采样后进行截屏   

管理提醒: 本帖被 SkyHarute 执行提前操作(2018-07-11)
在OpenGLES开发中使用了多重采样的技术,现在想对OpenGLES所在的View进行截屏,以下是测试代码。
现在的问题是调用glReadPixels()后,没有获取到data,data始终为null。已经卡主很久了,求教大家!


        glBindFramebuffer(GL_READ_FRAMEBUFFER_APPLE, sampleFramebuffer);
        glBindFramebuffer(GL_DRAW_FRAMEBUFFER_APPLE, defaultFrameBuffer);
        glResolveMultisampleFramebufferAPPLE();
        glBindFramebuffer(GL_FRAMEBUFFER, defaultFrameBuffer);
        
        GLint backingWidth;
        GLint backingHeight;
        glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &backingWidth);
        glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &backingHeight);
        
        NSInteger x = 0, y = 0, width2 = backingWidth, height2 = backingHeight;
        NSInteger dataLength = width2 * height2 * 4;
        GLubyte *data = (GLubyte*)malloc(dataLength * sizeof(GLubyte));
        // Read pixel data from the framebuffer
//        glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
        CHECK_GL_ERROR_DEBUG();
        glReadPixels((GLint)x, (GLint)y, (GLsizei)width2, (GLsizei)height2, GL_RGBA, GL_UNSIGNED_BYTE, data);
        GLenum attachments[] = {GL_COLOR_ATTACHMENT0, GL_DEPTH_ATTACHMENT   };
        glDiscardFramebufferEXT(GL_READ_FRAMEBUFFER_APPLE, 2, attachments);
        CHECK_GL_ERROR_DEBUG();
        // Create a CGImage with the pixel data
        // If your OpenGL ES content is opaque, use kCGImageAlphaNoneSkipLast to ignore the alpha channel
        // otherwise, use kCGImageAlphaPremultipliedLast
        CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, data, dataLength, NULL);
        CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
        CGImageRef iref = CGImageCreate(width2, height2, 8, 32, width2 * 4, colorspace, kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast,
                                        ref, NULL, true, kCGRenderingIntentDefault);
        
        // OpenGL ES measures data in PIXELS
        // Create a graphics context with the target size measured in POINTS
        NSInteger widthInPoints, heightInPoints;
        if (NULL != UIGraphicsBeginImageContextWithOptions) {
            // On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
            // Set the scale parameter to your OpenGL ES view's contentScaleFactor
            // so that you get a high-resolution snapshot when its value is greater than 1.0
            CGFloat scale = self.contentScaleFactor;
            widthInPoints = width2 / scale;
            heightInPoints = height2 / scale;
            UIGraphicsBeginImageContextWithOptions(CGSizeMake(widthInPoints, heightInPoints), NO, scale);
        }
        else {
            // On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
            widthInPoints = width2;
            heightInPoints = height2;
            UIGraphicsBeginImageContext(CGSizeMake(widthInPoints, heightInPoints));
        }
        
        CGContextRef cgcontext = UIGraphicsGetCurrentContext();
        
        // UIKit coordinate system is upside down to GL/Quartz coordinate system
        // Flip the CGImage by rendering it to the flipped bitmap context
        // The size of the destination area is measured in POINTS
        CGContextSetBlendMode(cgcontext, kCGBlendModeCopy);
        CGContextDrawImage(cgcontext, CGRectMake(0.0, 0.0, widthInPoints, heightInPoints), iref);
        
        // Retrieve the UIImage from the current context
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
        
        // Clean up
        free(data);
        CFRelease(ref);
        CFRelease(colorspace);
        CGImageRelease(iref);
级别: 圣骑士

状态: 连续签到 - [62天]
UID: 510329
精华: 0
发帖: 702
可可豆: 1014 CB
威望: 886 点
在线时间: 1025(时)
注册时间: 2015-09-30
最后登录: 2018-11-21
1 楼:  发表于: 2018-07-11 15:51    发自: Web Page
http://www.khronos.org/registry/OpenGL-Refpages/es1.1/xhtml/,应该是参数设置不对,自己研究一下~
级别: 新手上路
UID: 657909
精华: 0
发帖: 10
可可豆: 10 CB
威望: 10 点
在线时间: 4(时)
注册时间: 2018-07-11
最后登录: 2018-07-14
2 楼:  发表于: 2018-07-12 10:56    发自: Web Page
游戏y源码测试地址:https://mp.weixin.qq.com/s/i3nz_4ymkbeRytun03CA8Q  

本站内容均为本站转发,已尽可能注明出处。因未能核实来源或转发内容图片有权利瑕疵的,请及时联系本站,本站会第一时间进行修改或删除。 QQ : 3442093904

描述
快速回复

关注本帖(如果有新回复会站内信通知您)

发帖、回帖都会得到可观的积分奖励。查看论坛积分规则

按"Ctrl+Enter"直接提交
    顶部