添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

首先说一下做这个功能的原因,是在应用里,有一个渲染线程,负责渲染出图,将最终的画面以texture形式传递给另一个线程,后者会再进行一些处理,然后渲染到屏幕上。那么在最后显示出来画面偶尔有花屏撕裂的现象,这样就需要确认问题出在哪一步。所以想要在中间把texture保存成图片检查一下。

实现这个功能主要依赖函数 glReadPixels(),它可以把gpu里的像素值读取到cpu内存中,这样才有可能保存下来。

另外一个关键点是,如何把RGBA格式的像素值写成BMP格式的文件。

接下来说实现步骤,首先需要绑定framebuffer,以及要读取的texture。

glBindFramebuffer(GL_FRAMEBUFFER, m_uSpareFbId);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureId, 0);

然后 glReadBuffer(), glReadPixels(), 这里以宽高为 1920 * 1920 为例。

        int width = 1920;
        int height = 1920;
        glReadBuffer ( GL_COLOR_ATTACHMENT0 );
        GLint readType, readFormat;
        GLubyte *pixels;
        glGetIntegerv ( GL_IMPLEMENTATION_COLOR_READ_TYPE, &readType );
        glGetIntegerv ( GL_IMPLEMENTATION_COLOR_READ_FORMAT, &readFormat );
        unsigned int bytesPerPixel = 0;
        switch ( readType )
            case GL_UNSIGNED_BYTE:
            case GL_BYTE:
                switch ( readFormat )
                    case GL_RGBA:
                        bytesPerPixel = 4;
                        break;
                    case GL_RGB:
                    case GL_RGB_INTEGER:
                        bytesPerPixel = 3;
                        break;
                    case GL_RG:
                    case GL_RG_INTEGER:
                    case GL_LUMINANCE_ALPHA:
                        bytesPerPixel = 2;
                        break;
                    case GL_RED:
                    case GL_RED_INTEGER:
                    case GL_ALPHA:
                    case GL_LUMINANCE:
                        bytesPerPixel = 1;
                        break;
                    default:
                        break;
                break;
            case GL_FLOAT:
            case GL_UNSIGNED_INT:
            case GL_INT:
                switch ( readFormat )
                    case GL_RGBA:
                    case GL_RGBA_INTEGER:
                        bytesPerPixel = 16;
                        break;
                    case GL_RGB:
                    case GL_RGB_INTEGER:
                        bytesPerPixel = 12;
                        break;
                    case GL_RG:
                    case GL_RG_INTEGER:
                        bytesPerPixel = 8;
                        break;
                    case GL_RED:
                    case GL_RED_INTEGER:
                    case GL_DEPTH_COMPONENT:
                        bytesPerPixel = 4;
                        break;
                    default:
                        break;
                break;
            case GL_HALF_FLOAT:
            case GL_UNSIGNED_SHORT:
            case GL_SHORT:
                switch ( readFormat )
                    case GL_RGBA:
                    case GL_RGBA_INTEGER:
                        bytesPerPixel = 8;
                        break;
                    case GL_RGB:
                    case GL_RGB_INTEGER:
                        bytesPerPixel = 6;
                        break;
                    case GL_RG:
                    case GL_RG_INTEGER:
                        bytesPerPixel = 4;
                        break;
                    case GL_RED:
                    case GL_RED_INTEGER:
                        bytesPerPixel = 2;
                        break;
                    default:
                        break;
                break;
            case GL_FLOAT_32_UNSIGNED_INT_24_8_REV: // GL_DEPTH_STENCIL
                bytesPerPixel = 8;
                break;
                // GL_RGBA, GL_RGBA_INTEGER format
            case GL_UNSIGNED_INT_2_10_10_10_REV:
            case GL_UNSIGNED_INT_10F_11F_11F_REV: // GL_RGB format
            case GL_UNSIGNED_INT_5_9_9_9_REV: // GL_RGB format
            case GL_UNSIGNED_INT_24_8: // GL_DEPTH_STENCIL format
                bytesPerPixel = 4;
                break;
            case GL_UNSIGNED_SHORT_4_4_4_4: // GL_RGBA format
            case GL_UNSIGNED_SHORT_5_5_5_1: // GL_RGBA format
            case GL_UNSIGNED_SHORT_5_6_5: // GL_RGB format
                bytesPerPixel = 2;
                break;
            default:
                break;
        pixels = ( GLubyte* ) malloc ( width * height * bytesPerPixel );
        glReadPixels ( 0, 0, width, height, readFormat, readType, pixels );

然后就可以把数据写到图片文件里,最后记得清内存。

SaveBmp(frameCount,pixels,width,height);
free(pixels);
frameCount ++;

转换bmp文件的实现方法如下,关键就是文件头部信息的填写,填写错误的话最后就会打不开图片文件,详细规则还未深入研究。

这里还存在一点问题,原格式是rgba,最后查看图片发现颜色变了,应该是显示的bgra,还没弄明白,有大佬路过还望不吝赐教!

#define FILENAME_MAX 64
#define CAMERA_DUMP_FRM_LOCATION "/sdcard/rgb/"
    static int frameCount = 0;
    typedef struct /**** BMP file header structure ****/
        unsigned int bfSize; /* Size of file */
        unsigned short bfReserved1;      /* Reserved */
        unsigned short bfReserved2;      /* ... */
        unsigned int   bfOffBits;        /* Offset to bitmap data */
    } BITMAPFILEHEADER;
    typedef struct                       /**** BMP file info structure ****/
        unsigned int   biSize;           /* Size of info header */
        int            biWidth;          /* Width of image */
        int            biHeight;         /* Height of image */
        unsigned short biPlanes;         /* Number of color planes */
        unsigned short biBitCount;       /* Number of bits per pixel */
        unsigned int   biCompression;    /* Type of compression to use */
        unsigned int   biSizeImage;      /* Size of image data */
        int            biXPelsPerMeter;  /* X pixels per meter */
        int            biYPelsPerMeter;  /* Y pixels per meter */
        unsigned int   biClrUsed;        /* Number of colors used */
        unsigned int   biClrImportant;   /* Number of important colors */
    } BITMAPINFOHEADER;
    void SaveBmp(uint32_t frameid, unsigned char *rgbbuf, int width, int height) {
        BITMAPFILEHEADER bfh;
        BITMAPINFOHEADER bih;
        /* Magic number for file. It does not fit in the header structure due to alignment requirements, so put it outside */
        unsigned short bfType = 0x4d42;
        bfh.bfReserved1 = 0;
        bfh.bfReserved2 = 0;
        bfh.bfSize = 2 + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + width * height * 4;
        bfh.bfOffBits = 0x36;
        bih.biSize = sizeof(BITMAPINFOHEADER);
        bih.biWidth = width;
        bih.biHeight = height;
        bih.biPlanes = 1;
        bih.biBitCount = 32;
        bih.biCompression = 0;
        bih.biSizeImage = bih.biWidth * bih.biHeight * bih.biBitCount;
        bih.biXPelsPerMeter = 5000;
        bih.biYPelsPerMeter = 5000;
        bih.biClrUsed = 0;
        bih.biClrImportant = 0;
        char buf[FILENAME_MAX];
        memset(buf, 0, sizeof(buf));
        snprintf(buf, sizeof(buf), CAMERA_DUMP_FRM_LOCATION "frame_%dX%d_%d.bmp",
                 width, height, frameid);
        LOG(" jjj: %s,%d buf : %s", __FUNCTION__, __LINE__, buf);
        int file_fd = open(buf, O_RDWR | O_CREAT, 0644);
        if (file_fd < 0) {
            printf("Could not write file\n");
            return;
        /*Write headers*/
        write(file_fd, &bfType, sizeof(bfType));
        write(file_fd, &bfh, sizeof(bfh));
        write(file_fd, &bih, sizeof(bih));
        ssize_t result = write(file_fd, rgbbuf, width * height * 4);
        LOG(" jjj: %s,%d : written to file %s, result %ld", __FUNCTION__, __LINE__, buf,
            result);
        close(file_fd);
				
int [] oldFboId = new int[1]; GLES20.glGetIntegerv(GLES20.GL_FRAMEBUFFER_BINDING, IntBuffer.wrap(oldFboId)); int [] framebuffers = new int[1]; GLES20.g...
今天我们先简单介绍Windows中常用的BMP文件格式,然后讲OpenGL的像素操作。虽然看起来内容可能有点多,但实际只有少量几个知识点,如果读者对诸如”显示BMP图象”等内容比较感兴趣的话,可能不知不觉就看完了。 像素操作可以很复杂,这里仅涉及了简单的部分,让大家对OpenGL像素操作有初步的印象。 学过多媒体技术的朋友可能知道,计算机保存图象的方法通常有两种:一是”矢量图”,一是”...
static void save_texture(int texture_id, bool is_2d, int width, int height, std::string file_name) { GLint old_fbo; glGetIntegerv(GL_FRAMEBUFFER_BINDING, &old_fbo); GLuint tmp_fbo; glGenFramebuffers(1, &tmp_fbo); glBindFramebuffer(GL_FRAMEBUFFER, t