2010-03-23 115 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