OpenGL using SOIL to produce jpg only the image size is incorrect

134 views Asked by At

I am using OpenGL to as part of a drawing application for iOS. When the user finishes drawing the texture is saved to a jpg using SOIL. The image appears correct in the view, when saved the output is oriented correctly (after inverting the image) but the image is scaled to a smaller size (based on device.. higher res devices produce smaller images) than what I am expecting.

The process I am following is:

unsigned char *pixels = (unsigned char*)malloc( 4* imgwidth* imgheight);
glBindTexture(GL_TEXTURE_2D, 0);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, imgwidth, imgheight);
glPixelStorei (GL_UNPACK_ALIGNMENT, 1); 
glPixelStorei (GL_UNPACK_SKIP_ROWS, 1);
glPixelStorei (GL_UNPACK_SKIP_PIXELS, 1);
glReadPixels(0, 0, imgwidth, imgheight, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *) pixels);
invertPixels(pixels);
int result;
result = SOIL_save_image_quality(imagepath, SOIL_SAVE_TYPE_JPG, d, imgheight, 4, pixel_data, 99);
delete [] pixel;

I have analyzed the inversion algorithm to death and am confident its doing its job, without it the image is the same size but upside down. What could be causing this? Do I need to rescale my texture?

1

There are 1 answers

3
beshio On

I think this is related to contentsScale of your CAEAGLLayer (or contentScaleFactor of your GLKView). You can check the value of your application, which should be either 1.0, 2.0 or 3.0. The 1.0 gives us poor quality on retina displays. When we create textures for retina, we need to scale the size with contentScale of CAEAGLLayer for expected results. I mean we need to create larger size textures if not 1.0. I haven't used SOIL, but the opposite should be true. Therefore, I think, to get the image size you expect, you need to scale your images.

Note as noted here, we can change the contentsScale to trade quality for performance. My OpenGL app always sets contentScale to 2.0 even for iphone7+ for better rendering performance. So, don't hard code like iphone7 is 2.0 or iphone7+ is 3.0.