【问题标题】:iPhone OpenGL ES Paint App Brush EffectiPhone OpenGL ES Paint App 笔刷效果
【发布时间】:2012-04-23 13:24:03
【问题描述】:

我正在为 iPhone 和 iPad 开发绘画应用程序 [参考 GLPaint 应用程序]。 我正在研究画笔效果。我想为我的绘画应用程序获得画笔效果,如 Image1 所示

我的笔触类似于图 2

我正在为画笔纹理使用以下代码:

CGImageRef      brushImage;
CGContextRef    brushContext;
GLubyte         *brushData;
size_t          width, height;
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        brushImage = [UIImage imageNamed:@"flower@2x.png"].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 的“所需”笔触。

【问题讨论】:

    标签: iphone objective-c ios opengl-es xcode4


    【解决方案1】:

    不要使用GL_POINT_SPRITE_OES 模式。通过标准三角形绘制精灵。然后它需要将精灵纹理坐标绑定到目标输出坐标并使精灵纹理可重复。 假设精灵纹理大小为 32x32。默认纹理坐标为 rect {{0,0},{1.0,1.0}}。在{x,y}位置绘制精灵,需要使用基于矩形{{(x%32)/32.0, (y%32)/32.0},{1.0, 1.0}}的精灵纹理坐标。这样可以防止精灵内容弄脏。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多