2011-10-29 50 views
4

我遇到了使用GLKit設置PNG圖像的問題。GLKit&添加色彩到紋理

我有我加載到應用程序,然後用它來創建一個紋理白色PNG圖像:

UIImage *image = [ UIImage imageNamed:@"brushImage" ]; 
NSError *error = nil; 
texture_m = [[ GLKTextureLoader textureWithCGImage:image.CGImage options:nil error:&error] retain ]; 
if (error) { 
    NSLog(@"Error loading texture from image: %@",error); 
} 

紋理沒有任何錯誤。

但是當我要渲染混合紋理激活的顏色似乎被忽略,我得到一個白色圖像。當我在OpenGL1中做這件事時,我沒有任何問題,圖像將拾取在glColor4f()中定義的顏色。這裏是我的渲染代碼:

-(void)render{ 
    if (texture_m != nil) { 
     effect_m.texture2d0.enabled = GL_TRUE; 
     effect_m.texture2d0.envMode = GLKTextureEnvModeReplace; 
     effect_m.texture2d0.target = GLKTextureTarget2D; 
     effect_m.texture2d0.name = texture_m.name; 
    } 

    [effect_m prepareToDraw]; 

    glClear(GL_COLOR_BUFFER_BIT); 


    GLfloat squareVertices[] = { 
     50, 50, 
     150, 50, 
     50, 150, 
     150, 150 
    }; 
    GLfloat squareTexture[] = { 
     0, 0, 
     1, 0, 
     0, 1, 
     1, 1 
    }; 


    glColor4f(1, 0, 0, 1); 

    glEnableVertexAttribArray(GLKVertexAttribPosition); 
    glEnableVertexAttribArray(GLKVertexAttribTexCoord0); 
    glEnable(GL_BLEND); 
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); 


    glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, squareVertices); 
    glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 0, squareTexture); 

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); 

    glDisable(GL_BLEND); 
    glDisableVertexAttribArray(GLKVertexAttribPosition); 
    glDisableVertexAttribArray(GLKVertexAttribTexCoord0); 
} 

任何人可以幫助解決這個問題 感謝

回答

2

我已成功地解決我的問題,在這裏是解決方案

-(void)render 
{ 
if (texture_m != nil) { 

      effect_m.texture2d0.enabled = GL_TRUE; 

      // here you need to env mode to GLKTextureEnvModeModulate rather than GLKTextureEnvModeReplace 

      effect_m.texture2d0.envMode = GLKTextureEnvModeModulate; 
      effect_m.texture2d0.target = GLKTextureTarget2D; 
      effect_m.texture2d0.name = texture_m.name; 
    } 

     // then here I have added the tint colour to the GLKBaseEffect class as constant colour which I imagine replaces the calls to glColor4f for OpenGL1.1 

    effect_m.useConstantColor = YES; 
    float alphaValue = 0.7; 
    GLKVector4 colour = GLKVector4Make(0* alphaValue, 1* alphaValue, 1* alphaValue, alphaValue); 
    effect_m.constantColor = colour; 

    // remember multiplying the alpha value to each colour component 

    [effect_m prepareToDraw]; 

    glClear(GL_COLOR_BUFFER_BIT); 

    GLfloat squareVertices[] = { 
     50, 50, 
     150, 50, 
     50, 150, 
     150, 150 
    }; 

    GLfloat squareTexture[] = { 
     0, 0, 
     1, 0, 
     0, 1, 
     1, 1 
    }; 



    // glColor4f not necessary 
    // glColor4f(1, 0, 0, 1); 

    glEnableVertexAttribArray(GLKVertexAttribPosition); 
    glEnableVertexAttribArray(GLKVertexAttribTexCoord0); 
    glEnable(GL_BLEND); 

    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); 

    glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, squareVertices); 
    glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 0, squareTexture); 
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); 

    glDisable(GL_BLEND); 
    glDisableVertexAttribArray(GLKVertexAttribPosition); 
    glDisableVertexAttribArray(GLKVertexAttribTexCoord0); 
}