其他分享
首页 > 其他分享> > Android OpenGL结合了SurfaceTexture(外部图像)和普通纹理

Android OpenGL结合了SurfaceTexture(外部图像)和普通纹理

作者:互联网

我想将相机预览SurfaceTexture与一些叠加纹理混合.我正在使用这些着色器进行处理:

    private final String vss = "attribute vec2 vPosition;\n"
        + "attribute vec2 vTexCoord;\n"
        + "varying vec2 texCoord;\n"
        + "void main() {\n" 
        + "  texCoord = vTexCoord;\n"
        + "  gl_Position = vec4 ( vPosition.x, vPosition.y, 0.0, 1.0 );\n"
        + "}";

private final String fss = "#extension GL_OES_EGL_image_external : require\n"
        + "precision mediump float;\n"
        + "uniform samplerExternalOES sTexture;\n"
        + "uniform sampler2D filterTexture;\n"
        + "varying vec2 texCoord;\n"
        + "void main() {\n"
        +"  vec4 t_camera = texture2D(sTexture,texCoord);\n"
        //+"  vec4 t_overlayer = texture2D(filterTexture, texCoord);\n" 
        //+ "  gl_FragColor = t_overlayer;\n" + "}";
        + "  gl_FragColor = t_camera;\n" + "}";

我的目标是混合t_camera和t_overlayer.当我单独显示t_camera或t_overlayer时,它可以工作(显示相机预览或纹理).但是当我取消注释t_overlayer时,t_camera变成黑色(不知何故被严重抽样).我的覆盖层纹理是512×512和CLAMPT_TO_EDGE.
此问题仅出现在以下示例:Android模拟器,HTC Evo 3D.
但是在SGS3,HTC One X上,它运行得很好.

怎么了?是Evo 3D缺少一些扩展还是什么?

解决方法:

我想你有这个问题,因为你没有在你的代码上设置正确的纹理ID.这是假设似乎合乎逻辑的典型错误,但实际上并未在文档中定义.如果您查看此扩展的文档,您会看到以下(已编辑)文本:

Each TEXTURE_EXTERNAL_OES texture object may require up to 3 texture
image units for each texture unit to which it is bound. When
is set to TEXTURE_EXTERNAL_OES this value will be between 1 and 3
(inclusive). For other valid texture targets this value will always be
1. Note that, when a TEXTURE_EXTERNAL_OES texture object is bound, the number of texture image units required by a single texture unit may be
1, 2, or 3
, while for other texture objects each texture unit requires
exactly 1 texture image unit.

这意味着只要你使用id 0,就可以使用另一个附加功能.在你的情况下:

GLES20.glUniform1i(sTextureHandle, 1);
GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
        sTextureId);

对于2D纹理:

GLES20.glUniform1i(filterTextureHandle, 0);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, filterTextureID);

我相信这会为你锻炼.

标签:android,opengl-es,image,textures,android-camera
来源: https://codeday.me/bug/20191004/1852128.html