Apple GLPaint修改问题

时间:2011-08-12 04:44:39

标签: iphone objective-c ipad opengl-es paint

我按照Apple的 GLPaint 绘图应用程序尝试修改它。在示例代码中,他们使用简单的 particle.png 进行绘制。

Before modification

我的问题是我想使用我选择的其他图像进行绘图。乍一看似乎很容易用某些<替换“particle.png” strong>“finger.png”但它没有用..当我用“finger.png”替换“particle.png”时,我得到了一些东西像这样:

After replacing particle.png

我的“finger.png”图片看起来像这样:

enter image description here

链接:http://developer.apple.com/library/ios/#samplecode/GLPaint/Listings/Classes_PaintingView_m.html#//apple_ref/doc/uid/DTS40007328-Classes_PaintingView_m-DontLinkElementID_6

部分代码:

- (id)initWithCoder:(NSCoder*)coder 
{

CGSize          myShadowOffset = CGSizeMake (0,  0);


 NSMutableArray*     recordedPaths;
 CGImageRef          brushImage;
 CGContextRef     brushContext;
 GLubyte               *brushData;
 size_t               width, height;


      // 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

   ////--------------------Modification--------------------------------///////    

        brushImage = [UIImage imageNamed:@"finger.png"].CGImage;  


  ////--------------------Modification--------------------------------///////     



     // Get the width and height of the image
      width = CGImageGetWidth(brushImage);
      height = CGImageGetHeight(brushImage);
      NSLog(@"%f%f",(CGFloat)width, (CGFloat)height);
      // 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) 
    {
           // Allocate  memory needed for the bitmap context
           brushData = (GLubyte *) calloc(width * height * 4, sizeof(GLubyte));
           // Use  the bitmatp creation function provided by the Core Graphics framework. 
           brushContext = CGBitmapContextCreate(brushData, width, height, 8, width * 4, CGImageGetColorSpace(brushImage), kCGImageAlphaPremultipliedLast);
           // After you create the context, you can draw the  image to the context.
           CGContextDrawImage(brushContext, CGRectMake(0.0, 0.0, (CGFloat)width, (CGFloat)height), brushImage);
           // You don't need the context at this point, so you need to release it to avoid memory leaks.

        CGContextRelease(brushContext);
           // Use OpenGL ES to generate a name for the texture.
           glGenTextures(1, &brushTexture);
           // Bind the texture name. 
           glBindTexture(GL_TEXTURE_2D, brushTexture);
           // Set the texture parameters to use a minifying filter and a linear filer (weighted average)
           glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
           // Specify a 2D texture image, providing the a pointer to the image data in memory
           glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, brushData);
           // Release  the image data; it's no longer needed
        free(brushData);
  }

我不明白为什么我会这样画画。任何人都可以指出我需要做出哪些其他更改才能使此应用程序像以前一样工作?我不是OpenGL的专家,所以任何帮助或建议都将受到赞赏..

2 个答案:

答案 0 :(得分:3)

如果我没记错的话,你必须把图像变成透明的白色才能使它工作。如果你的蓝色周围有透明度,它会将整个图片显示为不透明。

我选择了标准的Apple GLPaint应用程序。我用在Photoshop中制作的finger.png替换了particle.png。它是64x64 RGB 8位。整个图像是透明的,除了我直接从你的blue finger.png复制的白色污迹。这是模拟器中的输出: enter image description here

答案 1 :(得分:0)

有点晚了,但我发现如果你在PaintingView.h中更改#define kBrushScale,你会得到有趣的效果。尝试更改为.25,.5。 .75 1.0等...

相关问题