2010-03-23 116 views
0

我正在修改GLPaint以使用不同的背景,所以在这种情况下它是白色的。无论如何,他们正在使用的现有邮票假设背景是黑色的,所以我用一个alpha通道做了一个新的背景。当我在画布上画画时,它仍然是黑色的,给出了什么?当我真正画画时,我只是将纹理绑定起来,并且它起作用。这种初始化有些问题。用alpha通道绘制纹理不起作用 - 绘制黑色

这里是照片alt text

- (id)initWithCoder:(NSCoder*)coder 
{ 
    CGImageRef brushImage; 
    CGContextRef brushContext; 
    GLubyte *brushData; 
    size_t width, height; 

    if (self = [super initWithCoder:coder]) 
    { 
     CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer; 

     eaglLayer.opaque = YES; 
     // In this application, we want to retain the EAGLDrawable contents after a call to presentRenderbuffer. 
     eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: 
             [NSNumber numberWithBool:YES], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil]; 

     context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1]; 

     if (!context || ![EAGLContext setCurrentContext:context]) { 
      [self release]; 
      return nil; 
     } 

     // Create a texture from an image 
     // First create a UIImage object from the data in a image file, and then extract the Core Graphics image 
     brushImage = [UIImage imageNamed:@"test.png"].CGImage; 

     // Get the width and height of the image 
     width = CGImageGetWidth(brushImage); 
     height = CGImageGetHeight(brushImage); 

     // Texture dimensions must be a power of 2. If you write an application that allows users to supply an image, 
     // you'll want to add code that checks the dimensions and takes appropriate action if they are not a power of 2. 

     // Make sure the image exists 
     if(brushImage) 
     { 
      brushData = (GLubyte *) calloc(width * height * 4, sizeof(GLubyte)); 
      brushContext = CGBitmapContextCreate(brushData, width, width, 8, width * 4, CGImageGetColorSpace(brushImage), kCGImageAlphaPremultipliedLast); 
      CGContextDrawImage(brushContext, CGRectMake(0.0, 0.0, (CGFloat)width, (CGFloat)height), brushImage); 
      CGContextRelease(brushContext); 
      glGenTextures(1, &brushTexture); 
      glBindTexture(GL_TEXTURE_2D, brushTexture); 
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, brushData); 
      free(brushData); 
     } 

     //Set up OpenGL states 
     glMatrixMode(GL_PROJECTION); 
     CGRect frame = self.bounds; 
     glOrthof(0, frame.size.width, 0, frame.size.height, -1, 1); 
     glViewport(0, 0, frame.size.width, frame.size.height); 
     glMatrixMode(GL_MODELVIEW); 

     glDisable(GL_DITHER); 
     glEnable(GL_TEXTURE_2D); 
     glEnable(GL_BLEND); 
     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_DST_ALPHA); 
     glEnable(GL_POINT_SPRITE_OES); 
     glTexEnvf(GL_POINT_SPRITE_OES, GL_COORD_REPLACE_OES, GL_TRUE); 
     glPointSize(width/kBrushScale); 
    } 
    return self; 
} 

回答

0

你行的位置: glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_DST_ALPHA);需要为: glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

一减目的地阿尔法可以是0,因为DST字母通常为1

+0

谢谢,我知道,但我不小心留下在。反正这个问题是预乘阿尔法与nonpremultiplied。 PNG规范说它们不是预乘,但是GIMP将它们保存为前缀。所以你需要使用glBlendFunc(GL_ONE,GL_ONE_MINUS_SRC_ALPHA); – DevDevDev 2010-03-24 18:52:21