2011-08-12 87 views
0

我跟着蘋果的GLPaint油漆應用程序,並試圖修改它..在示例代碼中,他們使用簡單的particle.png來繪製。蘋果GLPaint修改問題

Before modification

我的問題是我想用我的一些選擇的其他圖像繪製。乍一看似乎很容易與一些「finger.png」更換「particle.png」,但沒有奏效。當我更換「particle.png」「finger.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的專家,所以任何幫助或建議將不勝感激..

回答

3

如果我沒有記錯,你必須使圖像白色透明,以使其工作。如果周圍有透明藍色,則會將整個圖片顯示爲不透明。

我拿了標準的Apple GLPaint應用程序。我用Photoshop中製作的finger.png替換了particle.png。它是64x64 RGB 8位。整個圖像是透明的,除了我從你的blue finger.png直接複製的白色污跡。這裏是模擬器的輸出: enter image description here

+0

@cdshader:謝謝你的建議..也許你是對的,但讓我更具體。當我使用** finger.png **時,我在觸摸/輕按上獲得那些方形圖像**(用紅色橢圓標記)**。那是因爲您所說的透明度? –

+1

我相信是的,你看到的是整個圖像的全部。我會想象你的finger.png是一個與你的實心方塊大小相同的方形圖像。 – cdasher

+0

不,我得到的圖像的形狀確切的是它在圖片中的樣子,但仍然沒有成功。 –

0

這有點晚,但我發現如果你改變了PaintingView.h中的#define kBrushScale,你會得到有趣的效果。嘗試改爲.25,.5。 .75 1.0等...

+0

感謝您的回覆..但是那個項目不幸被終止..所以我不記得我那時做了什麼。但我一定會試着在短時間內對它進行調查。:-) –