覆盖颜色混合在OpenGL ES / iOS / Cocos2d [英] Overlay Color Blend in OpenGL ES / iOS / Cocos2d

查看:538
本文介绍了覆盖颜色混合在OpenGL ES / iOS / Cocos2d的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将BlendModes应用于GreyScale图片,以便拥有可重复使用的静态资源



我在互联网上搜索了几个小时,



我从这张图片开始:





基本想法是绘制一个矩形在某种颜色,并应用混合模式,只有在alpha是1.0的图像



这里是代码(这是一个Cocos2d项目的一部分,认为它可以应用于通用OGL ES):

   - (void)draw 
{
[超画]

glBlendColor(0,255,0,255);

glBlendFunc(GL_ZERO,GL_SRC_COLOR);
glColor4ub(255,0,255,255);
glLineWidth(2);
CGPoint vertices2 [] = {ccp(0,100),ccp(100,100),ccp(100,0)};
[DrawingHelper drawPolygonWithPoints:vertices2 points:3 closePolygon:YES];

}

* Draw helper是绘制三角形的逻辑。 / p>

  +(void)drawPolygonWithPoints:(CGPoint *)poli points:(int)points closePolygon:(BOOL)closePolygon 
{
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glVertexPointer(2,GL_FLOAT,0,poli);

if(closePolygon)
glDrawArrays(GL_TRIANGLE_FAN,0,points);
else
glDrawArrays(GL_LINE_STRIP,0,points);

glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_TEXTURE_2D);
}

这里有一些结果:






您可以看到一个很好的近似,但是这两个图像有错误: p>

OpenGL错误0x0502 - [EAGLView swapBuffers]



我的问题是: p>


  1. 如何移除或修正此错误?

  2. 如何只保留图片的Alpha


  3. [更新]这是一个我想要的示例(使用正确的混合) :



    >

    解决方案


    仅在alpha是1.0的情况下将混合模式应用于图片


    这听起来像是alpha测试的应用程序。只有我先绘制矩形,然后使alpha测试失败等于1.0(或大于0.99,以允许一些边距)。




    编辑更新的问题



    所需结果:



    我想你在这里混合了Multiply和Overlay字幕。



    在所有这些情况下,使用




    • glEnable(GL_ALPHA_TEST)






    • glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);



    效果不是通过设置混合函数或模式创建的,



    覆盖(实际上是乘法)效果对应于GL_MODULATE纹理环境模式。或者根据着色器 gl_FragColor = texture2D(...)* color;



    min(texture2D(...),color);



    > Multiply (实际覆盖) gl_FragColor = 1. - (1.-color * texture2D(...))*(1.-color);

    gl_FragColor =(1. - (1.-texture2D(...) )*(1.-color))* k + b; (k和b选择参数)。


    I'm trying to apply BlendModes to a GreyScale image in order to have reusable static resources

    I've been searching in the internet for hours and I did my own tests but I didn't find any solution.

    I started with this image:

    And the basic idea was to draw a rectangle over it in a certain color and apply a blending mode to the image only where the alpha is 1.0

    Here is the Code (this is part of a Cocos2d project although I think it can be applied to generic OGL ES):

    -(void)draw
    {
        [super draw];
    
        glBlendColor(0,255,0,255);
    
        glBlendFunc(GL_ZERO, GL_SRC_COLOR);
        glColor4ub(255, 0, 255, 255);
        glLineWidth(2);
        CGPoint vertices2[] = { ccp(0,100), ccp(100,100), ccp(100,0) };
        [ DrawingHelper  drawPolygonWithPoints:vertices2 points:3 closePolygon:YES];
    
    }
    

    *Draw helper is the logic to draw the triangle.

    +(void)drawPolygonWithPoints:(CGPoint *)poli points:(int)points closePolygon:(BOOL)closePolygon
    {
        glDisable(GL_TEXTURE_2D);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
        glDisableClientState(GL_COLOR_ARRAY);
        glVertexPointer(2, GL_FLOAT, 0, poli);
    
        if( closePolygon )
            glDrawArrays(GL_TRIANGLE_FAN, 0, points);
        else
            glDrawArrays(GL_LINE_STRIP, 0, points);
    
        glEnableClientState(GL_COLOR_ARRAY);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
        glEnable(GL_TEXTURE_2D);
    }
    

    And here some results:

    As you can see is a good approximation but this two images has error:

    OpenGL error 0x0502 in -[EAGLView swapBuffers]

    My Questions are:

    1. How can I remove or fix this error?
    2. How can keep only the alpha of the image (shield) and apply the blend overlay color?

    [Update]This is an example of what I would like (with the correct blends):

    解决方案

    apply a blending mode to the image only where the alpha is 1.0

    This sounds like an application for alpha testing. Only that I'd first draw the rectangle and then make the alpha test fail for equal 1.0 (or greater than 0.99 to allow for some margin). This doesn't require blending.


    Edit for updated question

    Desired results:

    I think you mixed up Multiply and Overlay captions up there.

    In all those cases this is done using either

    • glEnable(GL_ALPHA_TEST); glAlphaFunc(GL_GEQUAL, 0.995); // not using 1.0 for some margin

    or

    • glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    The effects are not created by setting the blend function or mode, but by texture environment or shader.

    The "Overlay" (actually multiply) effect corresponds to the GL_MODULATE texture environment mode. Or in terms of a shader gl_FragColor = texture2D(...) * color;.

    "Lighten" is min(texture2D(...), color);

    "Multiply" (actually overlay) is gl_FragColor = 1. - (1.-color*texture2D(...))*(1.-color);

    "Soft Light" is gl_FragColor = (1. - (1.-texture2D(...))*(1.-color))*k + b; (k and b choosen parameters).

    这篇关于覆盖颜色混合在OpenGL ES / iOS / Cocos2d的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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