2012-04-11 70 views
2

我正在開發用於iPhone和iPad的繪畫應用程序[參考來自GLPaint應用程序]。 我正在研究畫筆效果。我想爲我的油漆刷的應用效果如圖image1的iPhone OpenGL ES Paint App刷機效果

enter image description here

我有類似的圖像2

enter image description here

我使用毛刷質地下面的代碼筆觸:

CGImageRef  brushImage; 
CGContextRef brushContext; 
GLubyte   *brushData; 
size_t   width, height; 
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    { 
     brushImage = [UIImage imageNamed:@"[email protected]"].CGImage; 
    } 
    else { 
     brushImage = [UIImage imageNamed:@"flower.png"].CGImage; 
    } 
    width = CGImageGetWidth(brushImage) ; 
    height = CGImageGetHeight(brushImage) ; 
    if(brushImage) { 

     brushData = (GLubyte *) calloc(width * height * 4, sizeof(GLubyte)); 
       brushContext = CGBitmapContextCreate(brushData, width, height, 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); 
    } 
     CGFloat scale; 
     scale = self.contentScaleFactor; 

    glMatrixMode(GL_PROJECTION); 
    CGRect frame = self.bounds; 
    glLoadIdentity(); 
    glOrthof(0, (frame.size.width) * scale, 0, (frame.size.height) * scale, -1, 1); 
    glViewport(0, 0, (frame.size.width) * scale, (frame.size.height) * scale); 
    glMatrixMode(GL_MODELVIEW); 
    glDisable(GL_DITHER); 
    glEnable(GL_BLEND); 
    glEnable(GL_TEXTURE_2D); 
    glEnableClientState(GL_VERTEX_ARRAY); 
    glEnable(GL_BLEND); 
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); 
    glEnable(GL_POINT_SPRITE_OES); 
    glTexEnvf(GL_POINT_SPRITE_OES, GL_COORD_REPLACE_OES, GL_TRUE); 
    glPointSize(width/kBrushScale); 
    // Define a starting color 
    HSL2RGB((CGFloat) 0.0/(CGFloat)kPaletteSize, kSaturation, kLuminosity, &components[0], &components[1], &components[2]); 
    glColor4f(components[0] * kBrushOpacity, components[1] * kBrushOpacity, components[2] * kBrushOpacity, kBrushOpacity); 

我一直在尋找與不同油漆刷筆畫相關的代碼,但是我找不到任何代碼。幫助我獲得類似於image1的「所需」畫筆描邊。

回答

0

請勿使用GL_POINT_SPRITE_OES模式。通過標準三角形繪製精靈。然後它需要將精靈紋理座標綁定到目標輸出座標並使精靈紋理可重複。 假設精靈紋理尺寸是32x32。默認的紋理座標是rect {{0,0},{1.0,1.0}}。對於在{x,y}位置繪製精靈,需要使用基於rect {{(x%32)/32.0, (y%32)/32.0},{1.0, 1.0}}的精靈紋理座標。這樣可以防止精靈內容模糊。