SurfaceTexture(外部图像)和普通纹理的Android OpenGL组合 [英] Android OpenGL combination of SurfaceTexture (external image) and ordinary texture

查看:25
本文介绍了SurfaceTexture(外部图像)和普通纹理的Android OpenGL组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to mix camera preview SurfaceTexture with some overlay texture. I am using these shaders for processing:

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

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

我的目标是混合 t_camerat_overlayer.当我分别显示 t_camerat_overlayer 时,它可以工作(显示相机预览或纹理).但是当我取消注释 t_overlayer 时,t_camera 会变成黑色(不知何故采样不好).我的覆盖层纹理是 512x512 和 CLAMPT_TO_EDGE.此问题仅在以下情况下发生:Android Emulator、HTC Evo 3D.但在 SGS3、HTC One X 上,它运行良好.

My goal is to mix t_camera and t_overlayer. When I show t_camera or t_overlayer separately, it works (showing camera preview or texture). But when I uncomment t_overlayer, then t_camera becomes black (somehow badly sampled). My overlayer texture is 512x512 and CLAMPT_TO_EDGE. This problem occurs only for example on: Android Emulator, HTC Evo 3D. But on SGS3, HTC One X, it works just fine.

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

What is wrong? Is it Evo 3D missing some extension or what?

推荐答案

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

I imagine you have this problem because you are not setting the correct texture id on your code. This is a tipical error on assumptions which seem logical, but is actually not defined so on the documentation. If you check the documentation of this extension you see the following (edited) TEXT:

每个 TEXTURE_EXTERNAL_OES 纹理对象最多可能需要 3 个纹理它绑定到的每个纹理单元的图像单元.什么时候设置为 TEXTURE_EXTERNAL_OES 此值将介于 1 和 3 之间(包括的).对于其他有效的纹理目标,此值将始终为1.注意,当绑定一个TEXTURE_EXTERNAL_OES纹理对象时,单个纹理单元所需的纹理图像单元数可能为1、2 或 3,而对于其他纹理对象,每个纹理单元都需要正好 1 个纹理图像单元.

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,至少还有一个可以工作.在你的情况下:

This means that at leas one additional will work, provided that you use id 0 for it. In your case:

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

对于您的 2D 纹理:

For your 2D texture:

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

我相信这会为你锻炼.

这篇关于SurfaceTexture(外部图像)和普通纹理的Android OpenGL组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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