GLKit&为纹理添加色调 [英] GLKit & adding tints to textures

查看:116
本文介绍了GLKit&为纹理添加色调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到使用GLKit着色PNG图像的问题。

I am having issue with tinting a PNG image with GLKit.

我有一个白色的PNG图像,我加载到应用程序中然后用它来创建纹理:

I have a white PNG image that I load into the application and then use it to create a texture:

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()中定义的颜色。这是我的渲染代码:

however when I want to render the texture with blend activated the colour seems to be ignored and I get a white image. I had no issues when I was doing this in OpenGL1 were the image would pick up the colour that was defined in glColor4f(). Here is my render code:

-(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);
}

任何人都可以帮忙解决这个问题
谢谢

Could anybody help to solve this issue Thanks

Reza

推荐答案

我设法解决了我的问题,这是解决方案

I have managed to solve my problem and here is the solution

-(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);
}

这篇关于GLKit&为纹理添加色调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆