Search code examples
c++opengl-esopengl-es-2.0egl

Difference from eglCreatePbufferSurface and eglCreatePixmapSurface with OpenGL ES(EGL)


I am having a problem where I need to some off-screen work with opengl es2 by software rendering(Only has CPU, no GPU). The question is can I use pbuffer without GPU? Also, how to directly save to a png file after drawing something. Please help and give me a demo.


Solution

  • First, use EGL to create an off-screen buffer:

    eglCreatePbufferSurface(display, config, PBufAttribs); 
    

    Then read the buffer:

       GLint size;
       size = esContext->width * esContext->height * 4;
       GLubyte *data = (GLubyte*)malloc(size);
       glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
       glReadPixels(0,0,esContext->width,esContext->height,GL_RGB,GL_UNSIGNED_BYTE,data);
    

    The last save to pixel buffer to a bmp file. (reminder: In 24 bit bmp image, the order is BGR, not RGB; So need to switch the image data from BGR to RGB.)