With OpenGL ES faced for the first time, so there are questions.

The main question: how to create a context on RGBA using OpenGL ES to correctly display transparency in different programs?

I'm trying to figure out the code where there is a problem with correctly saving the png image.


The situation is as follows:

When creating a picture with a transparent background in the application, the created picture after saving to png is displayed in some viewers (on Windows) with a black background, and in some normally.

The transparency of the created images in paint.net is displayed correctly where the pngs I created are displayed with a black background.


I sin on the fact that somewhere there is a flaw in saving the image and there is a problem with the correct preservation of alpha channels (although I could be wrong).

About saving:

- (UIImage *)renderToImage { uint w = (uint)self.frame.size.width; uint h = (uint)self.frame.size.height; NSInteger myDataLength = w * h * 4; GLubyte * buffer = (GLubyte *)malloc((size_t) myDataLength); GLubyte * buffer2 = (GLubyte *)malloc((size_t) myDataLength); __block UIImage * image; runOnMainThread (^{ glFlush(); glBindFramebufferOES(GL_FRAMEBUFFER_OES, _frameBuffer); glBindRenderbufferOES(GL_RENDERBUFFER_OES, _renderBuffer); glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, buffer); for (int y = 0; y < h; y++) { int count = w * 4; for (int x = 0; x < count; x++) { buffer2[(h - 1 - y) * w * 4 + x] = buffer[y * 4 * w + x]; } } CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); CGContextRef bitmapContext = CGBitmapContextCreate(buffer2, w, h, 8, 4 * w, colorSpaceRef, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault); CGColorSpaceRelease(colorSpaceRef); CGImageRef imageRef = CGBitmapContextCreateImage(bitmapContext); CGContextRelease(bitmapContext); free(buffer); free(buffer2); image = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); }); return image; 

}

 - (NSData *)saveImgChanges { DDLogError(@"%s -- NOT IMPLEMENTED", __FUNCTION__); __block NSData * updatedImageData = nil; runOnMainThread (^{ @autoreleasepool { UIImage * originalImage = [[UIImage alloc] initWithData:[self modifiedAttachmentData]]; CGRect imageRect = CGRectZero; imageRect.size = originalImage.size; UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, [UIScreen mainScreen].scale); [originalImage drawInRect:imageRect]; [originalImage release]; PR_AttachPageInfo * pageInfo = [_attachPagesInfo lastObject]; PR_DrawView * drawView = nil; for (PR_DrawView * dv in _drawViews) { if (dv.tag == [self indexOfPageView:pageInfo.topLevelView]) { drawView = dv; break; } } if (drawView != nil && drawView.hasContent) { UIImage * image = [drawView renderToImage]; [image drawInRect:imageRect]; } UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext(); updatedImageData = UIImagePNGRepresentation(newImage); UIGraphicsEndImageContext(); } }); return updatedImageData; 

}

Be kind, tell me where the flaw may lurk?

What can cause such problems?

Could they be due to the cant when creating this image?

How can I create context on RGBA?

  • So nevertheless, clarify the question, is it about OpenGL or about viewing PNG in different programs or how to create context on RGBA? - Kromster
  • It is about how to create a context on RGBA using OpenGL ES to correctly display transparency in different programs. - Vasya
  • These are 2 different unrelated problems. - Kromster
  • Tell me in this case - what could be the problem with the incorrect display of transparency? - Vasya
  • The transparency of the created images in paint.net is displayed correctly where the pngs I created are displayed with a black background. - Vasya

2 answers 2

  1. We check the original RGBA data, save it as AAA and check that the alpha is correct.

  2. If the source data is correct, then the case is in the library where you save the PNG. It can be seen that it uses a format that is not compatible with all viewers (I used it when using ImageCatalyst, some pngs were obtained with different backgrounds in different programs).

Dig in the direction of the settings of the preservation or library library.

    As it turned out - the problem was with the viewer.

    Problem solved.

    There are no problems with code using OpenGL ES.

    • Well, mark your answer as correct. - kelin September