0

I use this simple Shader for texture mapping in Open GL ES 2.0 I need to add the code for the Overlay Blending Mode. The Algorithm I understand everything. But how to obtain the color of a pixel on the screen for mixing.

p.s I use only one texture as the source

String vertexShader = "attribute vec4 " + ShaderProgram.POSITION_ATTRIBUTE + ";\n" //
        + "attribute vec2 " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;\n" //
        + "uniform mat4 u_projTrans;\n" //
        + "varying vec4 v_color;\n" //
        + "varying vec2 v_texCoords;\n" //
        + "\n" //
        + "void main()\n" //
        + "{\n" //
        + "   v_color = vec4(1, 1, 1, 1);\n" //
        + "   v_texCoords = " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;\n" //
        + "   gl_Position =  u_projTrans * " + ShaderProgram.POSITION_ATTRIBUTE + ";\n" //
        + "}\n";
    String fragmentShader = "#ifdef GL_ES\n" //
        + "#define LOWP lowp\n" //
        + "precision mediump float;\n" //
        + "#else\n" //
        + "#define LOWP \n" //
        + "#endif\n" //
        + "varying LOWP vec4 v_color;\n" //
        + "varying vec2 v_texCoords;\n" //
        + "uniform sampler2D u_texture;\n" //
        + "void main()\n"//
        + "{\n" //
        + "  gl_FragColor = texture2D(u_texture, v_texCoords);\n" //
        + "}";
4

1 回答 1

2

简短的回答是:你不能。有由 glBlendFunc()、glBlendEquation & Co 控制的经典 GL 混合,但这是在片段着色器之后执行的完全不同的管道阶段 - 混合会将当前像素颜色(目标)与片段着色器计算的颜色混合(来源) 使用您可以设置的各种模式和方程式。如果这足以达到您想要达到的效果,那就去吧。

如果您需要对着色器中的混合进行更多控制,您可以将要混合的图像渲染到纹理中,并在绘制要混合的内容时将其用作额外的输入纹理,但这会导致巨大的开销和应该避免。

另请参阅OpenGL Blend Modes vs Shader Blending问题。

于 2013-05-18T14:03:53.013 回答