OpenGL ES/iOS/Cocos2d 中的叠加颜色混合 [英] Overlay Color Blend in OpenGL ES / iOS / Cocos2d

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

问题描述

我正在尝试将 BlendModes 应用于 GreyScale 图像以获得可重用的静态资源

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.

我从这张图片开始:

基本思想是在其上以某种颜色绘制一个矩形,并仅在 alpha 为 1.0 的情况下对图像应用混合模式

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

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

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 是绘制三角形的逻辑.

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

这里有一些结果:

如您所见,这是一个很好的近似值,但这两张图片有错误:

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

OpenGL 错误 0x0502 在 -[EAGLView swapBuffers]

我的问题是:

  1. 如何删除或修复此错误?
  2. 如何只保留图像的 alpha(屏蔽)并应用混合叠加颜色?

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

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

推荐答案

仅对 alpha 为 1.0 的图像应用混合模式

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

这听起来像是一个用于 alpha 测试的应用程序.只是我先绘制矩形,然后使 alpha 测试失败,等于 1.0(或大于 0.99 以留出一些余量).这不需要混合.

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.

期望的结果:

我认为你在上面混淆了乘法和叠加字幕.

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);//不使用 1.0 作为一些边距
  • glEnable(GL_ALPHA_TEST); glAlphaFunc(GL_GEQUAL, 0.995); // not using 1.0 for some margin

  • 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.

叠加"(实际上是乘法)效果对应于 GL_MODULATE 纹理环境模式.或者就着色器而言 gl_FragColor = texture2D(...) * color;.

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

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

"Multiply"(实际上是叠加)是gl_FragColor = 1. - (1.-color*texture2D(...))*(1.-color);

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

柔光"是gl_FragColor = (1. - (1.-texture2D(...))*(1.-color))*k + b;(k和b选择参数).

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

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

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