开发者

Apple GLPaint modification problem

I followed Apple's GLPaint paint application and tried to modify it.. In sample code they have used simple particle.png to draw.

Apple GLPaint modification problem

My question is i want to use some other image of my choice for drawing. At first sight it seems very easy to replace "particle.png" with some "finger.png" but it did not work.. When I replaced "particle.png" with "finger.png" , I got something like this :

Apple GLPaint modification problem

My "finger.png" image looks somthing like this :

Apple GLPaint modification problem

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

Partial Code:

- (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_T开发者_运维问答EXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, brushData);
           // Release  the image data; it's no longer needed
        free(brushData);
  }

I do not understand why I am getting drawing like this. Can anyone point me out that what other changes I need to make to make this application work as before?? I am not expert at OpenGL so any help or suggestion will be appreciated..


If I remember correctly, you have to make the image white on transparent in order for it to work. If you have blue with transparency around it, it will show the entire picture as opaque.

I took the standard Apple GLPaint app. I replaced particle.png with a finger.png that I made in Photoshop. It is 64x64 RGB 8 bits. The entire image is transparent except for a white smudge which I copied directly from your blue finger.png. Here is the output in the simulator:

Apple GLPaint modification problem


It's a bit late, but I find if you change #define kBrushScale in PaintingView.h, you get interesting effects. Try changing to .25, .5. .75 1.0 etc...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜