2010-03-23 6 views
0

다른 배경을 사용하도록 GLPaint를 수정 중이므로이 경우 흰색입니다. 어쨌든 그들이 사용하고있는 기존 스탬프는 배경이 검은 색이라고 가정하기 때문에 알파 채널이있는 새로운 배경을 만들었습니다. 내가 캔버스에 그릴 때 그것은 여전히 ​​검은 색입니다. 제가 실제로 그릴 때, 나는 단지 텍스처를 묶어서 작동시킵니다. 이 초기 설정에서 잘못된 점이 있습니다. 여기 알파 채널로 텍스처 그리기가 작동하지 않음 - 검정색을 그립니다.

는 사진 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); DST를 알파는 보통 1

+0

감사 때문에

하나를 뺀 대상 알파가 0이 될 수 나는 것을 알고 있었다, 그러나 나는 실수에. 어쨌든 문제가 미리 곱셈 알파 대 nonpremultiplied로했다 떠났다. PNG 사양은 미리 곱셈되지 않았다고 말하지만, 김프는 미리 곱하기로 저장합니다. 그래서 glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA);를 사용해야합니다. – DevDevDev

관련 문제