2010-08-11 37 views
1

我碰到原本看起來應該是一個非常簡單的問題,但花了很多很多小時試圖找出並得到無處。如何在iPhone上調整多個UIImageVIews的色調?

我正在使用Apple的GLImageProcessing示例代碼,並將其簡化爲顯示一個帶有調整圖像色調的UISlider的EAGLView。我修改了EAGLView從UIImageView繼承,這樣我就可以更多地控制顯示的圖像。這工作正常。當我嘗試在屏幕上添加第二個圖像時出現問題。理想情況下,我想要顯示背景圖像和其他幾張圖像,並使色相滑塊同時更改所有這些圖像的色調。但是,當我嘗試向屏幕添加第二個圖像時,第一個圖像變成純黑色視圖,而當我移動滑塊時,實際上會變成紫色陰影,但無法響應。新視圖的功能如預期。如果我要添加第三個,前兩個將不起作用,而第三個將正常工作。

我想我想要做的是有一個EAGLView併爲新圖像添加紋理,但似乎沒有任何工作。任何幫助指出我在正確的方向將不勝感激。謝謝!

從ViewController.h。代碼在viewDidLoad方法中添加新的EAGLView子視圖。還有已經在筆尖加載另一種觀點認爲:

- (void)viewDidLoad 
{ 

    UIImage *img = [UIImage imageNamed:@"Image.png"]; 
    EAGLView *newView = [[EAGLView alloc] initWithImage:img texID:0]; 
    self.view2 = newView; 
    self.view2.frame = CGRectMake(110, 110, 100, 100); 
    [self.view addSubview:self.view2]; 
    [newView release]; 

} 

和簡單IBAction爲價值解僱更改:

- (void)sliderAction:(id)sender 
{ 
    [self.imageView drawViewWithSliderValue:self.slider.value]; 
    [self.view2 drawViewWithSliderValue:self.slider.value]; 
} 

從EAGLView.h,這是EAGLView初始化代碼和抽獎代碼。這幾乎是一樣的示例代碼,但它可以讓你通過圖像到initGL:

-(id) initializeEAGLwithTexID:(int)texID{ 
    // Get the layer 
    CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer; 
    eaglLayer.opaque = YES; 
    eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: 
              [NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, 
              kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, 
              nil]; 

    context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];   
    if (!context || ![EAGLContext setCurrentContext:context]) 
    { 
      [self release]; 
      return nil; 
    } 

    // Create system framebuffer object. The backing will be allocated in -reshapeFramebuffer 
    glGenFramebuffersOES(1, &viewFramebuffer[texID]); 
    glGenRenderbuffersOES(1, &viewRenderbuffer[texID]); 
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer[texID]); 
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer[texID]); 
    glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer[texID]); 

    // Perform additional one-time GL initialization 
    UIImage *myImage = self.image; 
    NSLog(@"holding image of width: %f height %f", myImage.size.width, myImage.size.height); 
    CGImageRef imageRef = myImage.CGImage; 
    initGL(imageRef, texID); 
    return self; 
} 

-(id) initWithImage:(UIImage *)image texID:(int)texID{ 
    if((self = [super initWithImage:image])){ 
      self.opaque = YES; 
      self.contentMode = UIViewContentModeScaleToFill; 
      [self initializeEAGLwithTexID:texID]; 

    } 
    return self; 
} 

- (void)drawViewWithSliderValue:(float)value; 
{ 
    drawGL(backingWidth, backingHeight, value, currentID); 
    [context presentRenderbuffer:GL_RENDERBUFFER_OES]; 
} 

從Imaging.c的initGL代碼和drawGL。我修改了處理數組以處理多個紋理的代碼。我比較肯定一堆在這裏創建是不必要的數組,但由於他們沒有傷害任何東西,也沒有試圖消除任何尚未:

void initGL(CGImageRef myImage, int currID) 
{ 
    int i; 

    // Query renderer capabilities that affect this app's rendering paths 
    renderer[currID].extension[APPLE_texture_2D_limited_npot] = 
      (0 != strstr((char *)glGetString(GL_EXTENSIONS), "GL_APPLE_texture_2D_limited_npot")); 
    renderer[currID].extension[IMG_texture_format_BGRA8888] = 
      (0 != strstr((char *)glGetString(GL_EXTENSIONS), "GL_IMG_texture_format_BGRA8888")); 

    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &renderer[currID].maxTextureSize); 

    // Constant state for the lifetime of the app-- position and unit0 are always used 
    glEnableClientState(GL_VERTEX_ARRAY); 
    glEnableClientState(GL_TEXTURE_COORD_ARRAY); 
    glEnable(GL_TEXTURE_2D); 
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 

    // Load image into texture 
    loadTexture(myImage, &Input[currID], &renderer[currID], currID); 

    // Modify quad texcoords to match (possibly padded) image 
    for (i = 0; i < 4; i++) 
    { 
      fullquad[i].s *= Input[currID].s; 
      fullquad[i].t *= Input[currID].t; 
      flipquad[i].s *= Input[currID].s; 
      flipquad[i].t *= Input[currID].t; 
    } 

    // Create 1x1 for default constant texture 
    // To enable a texture unit, a valid texture has to be bound even if the combine modes do not access it 
    GLubyte half[3][4] = {{ 0x80, 0x80, 0x80, 0x80 },{ 0x80, 0x80, 0x80, 0x80 },{ 0x80, 0x80, 0x80, 0x80 }}; 
    glActiveTexture(GL_TEXTURE1); 


    glGenTextures(1, &Half[currID].texID); 
    Half[currID].wide = Half[currID].high = 1; 
    Half[currID].s = Half[currID].t = 1.0; 
    glBindTexture(GL_TEXTURE_2D, Half[currID].texID); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 


    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, half[currID]); 
    glActiveTexture(GL_TEXTURE0); 

    // Remember the FBO being used for the display framebuffer 
    glGetIntegerv(GL_FRAMEBUFFER_BINDING_OES, (GLint *)&SystemFBO[currID]); 

    // Create scratch textures and FBOs 
    glGenTextures(1, &Degen[currID].texID); 
    Degen[currID].wide = Input[currID].wide; 
    Degen[currID].high = Input[currID].high; 
    Degen[currID].s = Input[currID].s; 
    Degen[currID].t = Input[currID].t; 
    glBindTexture(GL_TEXTURE_2D, Degen[currID].texID); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Degen[currID].wide, Degen[currID].high, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); 
    glGenFramebuffersOES(1, &DegenFBO[currID]); 
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, DegenFBO[currID]); 
    glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, Degen[currID].texID, 0); 

    glGenTextures(1, &Scratch[currID].texID); 
    Scratch[currID].wide = Input[currID].wide; 
    Scratch[currID].high = Input[currID].high; 
    Scratch[currID].s = Input[currID].s; 
    Scratch[currID].t = Input[currID].t; 
    glBindTexture(GL_TEXTURE_2D, Scratch[currID].texID); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Scratch[currID].wide, Scratch[currID].high, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); 
    glGenFramebuffersOES(1, &ScratchFBO[currID]); 
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, ScratchFBO[currID]); 
    glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, Scratch[currID].texID, 0); 
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, SystemFBO[currID]); 
} 

void drawGL(int wide, int high, float val, int currID) 
{ 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    glOrthof(0, wide, 0, high, -1, 1); 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
    glScalef(wide, high, 1); 

    glBindTexture(GL_TEXTURE_2D, Input[currID].texID); 

    // Render filtered image to system framebuffer 
    glViewport(0, 0, wide, high); 

    hue(flipquad, val); 

} 

回答

0

也許iPhone OS只支持一個Open GL的觀點在時間?

+0

看來iPhone OS只支持EAGLView的一個實例。但是,它應該允許編輯多個圖像,也許通過使用多個紋理? – 2010-08-18 15:44:20