If you do not refuse to use the C ++ library, you should try PNGwriter ! It records pixel images by pixels and their RGB values. As the PNGwriter starts to form the left corner, while glReadPixels () starts from the bottom left, your code looks like this:
GLfloat* OpenGLimage = new GLfloat[nPixels]; glReadPixels(0.0, 0.0, width, height,GL_RGB, GL_FLOAT, OpenGLimage); pngwriter PNG(width, height, 1.0, fileName); size_t x = 1; // start the top and leftmost point of the window size_t y = 1; double R, G, B; for(size_t i=0; i<npixels; i++) { switch(i%3) //the OpenGLimage array look like [R1, G1, B1, R2, G2, B2,...] { case 2: B = (double) pixels[i]; break; case 1: G = (double) pixels[i]; break; case 0: R = (double) pixels[i]; PNG.plot(x, y, R, G, B); if( x == width ) { x=1; y++; } else { x++; } break; } } PNG.close();
PS. I also tried libgd, but it seems to only convert one image file (to hard disk or memory) to a different image format. But I think this is still useful when you want to convert many PNG files to GIF format to create GIF animations.
source share