简体   繁体   中英

CoreGraphics Image render from OpenGL has black background

I am unable to render an image from an OpenGL context with a transparent background in CoreGraphics.

The rendered image has a black background.

This is the draw code

   GLint default_frame_buffer = 0;
   glGetIntegerv(GL_FRAMEBUFFER_BINDING, &default_frame_buffer);
   if (default_frame_buffer == 0) {
       target = createBMGLRenderTarget(width, height);
       setFiltering(target, BMGL_BilinearFiltering);
       glBindFramebuffer(GL_FRAMEBUFFER, target->framebuffer);
   }

   glViewport(0, 0, width, height);

   if (background) {
       CGFloat red, green, blue, alpha;
       [background getRed:&red green:&green blue:&blue alpha:&alpha];
       glClearColor(red, green, blue, alpha);
   } else {
       glClearColor(0.f, 0.f, 0.f, 0.f);
   }
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

   glEnable(GL_BLEND);
   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

   drawer.draw()

   glFlush();
   glFinish();

   GLubyte *data = (GLubyte *)malloc(width * height * 4);
   glPixelStorei(GL_PACK_ALIGNMENT, 4);
   glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);

   CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, data, width * height * 4, NULL);
   CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
   CGImageRef imgRef = CGImageCreate(width, height, 8, 32, width * 4, colorspace, kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast, ref, NULL, NO, kCGRenderingIntentDefault);

   UIGraphicsBeginImageContextWithOptions(size, YES, 0.0);
   CGContextRef cgContext = UIGraphicsGetCurrentContext();
   CGContextSetBlendMode(cgContext, kCGBlendModeCopy);
   CGContextDrawImage(cgContext, CGRectMake(0, 0, size.width, size.height), imgRef);
   UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext(); 
   free(data);
   CGDataProviderRelease(ref);
   CGColorSpaceRelease(colorspace);
   CGImageRelease(imgRef);

I have tried to specifically set opaque to false as well as different blend modes but it still adds a black background, to the original clear image. I am able to set the GLKView to have a transparent background, but rendering the image and then drawing its contents into a CGImage doesn't work.

Does anyone know why this is?

I believe your problem is in how to create UIImage from raw RGBA data. To confirm this you may check what your data is on a pixel you know to be transparent. Like data[pixelIndex*4 + 3] should be zero where you expect it to be transparent. If it is not transparent then the issue is on openGL part.

Anyway the most probable reason your image is not transparent is you are premultiplying alpha using kCGImageAlphaPremultipliedLast . Try using kCGBitmapByteOrder32Big|kCGImageAlphaLast .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM