How do I blend two textures with different co-ordinates in OpenGL ES 2.0 on iPhone?

帅比萌擦擦* 提交于 2019-11-29 04:06:17

You have to pass 2 texture coordinates to shader and modify shader

Add to ObjectiveC

glVertexAttribPointer(inputTextureCoordinate2, 2, GL_FLOAT, 0, 0, textureCoordinates2);

Vertex Shader

attribute vec4 position;
attribute vec4 inputTextureCoordinate;
attribute vec4 inputTextureCoordinate2;

varying vec2 textureCoordinate;
varying vec2 textureCoordinate2;

void main()
{
    gl_Position = position;
    textureCoordinate = inputTextureCoordinate.xy;
    textureCoordinate2 = inputTextureCoordinate2.xy;
}

Frag Shader

varying highp vec2 textureCoordinate;
varying highp vec2 textureCoordinate2;

uniform sampler2D inputTextureTop;
uniform sampler2D inputTextureBot;

uniform highp float alphaTop;

void main()
{
    lowp vec4 pixelTop = texture2D(inputTextureTop, textureCoordinate);
    lowp vec4 pixelBot = texture2D(inputTextureBot, textureCoordinate2);

    gl_FragColor = someBlendOperation(pixelTop, pixelBot);
}

BTW inputTextureCoordinate is not necessary to be vec4 but it could be vec2

If you're blending two textures on the same primitive, then you can mix the colors in the shader.

However if you want to blend two different primitives, then what you really just want to use is hardware blending (GL_BLEND).

Draw the bottom picture by itself, then enable blending and draw the top picture. The alpha value of the top picture controls how transparent it will be.

You don't really want to try to draw both quads in the same draw call, since they don't use the same coordinates.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!