我试图在我的实时相机预览中实现查找纹理。除了光泽和阴影外,一切看起来都很好。光泽呈现蓝色,阴影有时呈红色斑点。
我正在使用Grafika's Camera Capture。我推荐了GpuImage的Shader。我能够使用以下加载纹理函数
加载第二个纹理(查找纹理)public int createTextureObjectN() {
int[] textures = new int[1];
GLES20.glGenTextures(1, textures, 0);
GlUtil.checkGlError("glGenTextures");
int texId = textures[0];
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texId);
GlUtil.checkGlError("glBindTexture " + texId);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
GlUtil.checkGlError("glTexParameter");
return texId;
}
public int loadTexture(final int resourceId) {
final int texId = createTextureObjectN();
if (texId != 0) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false; // No pre-scaling
// Decode
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeResource(MyApplication.getAppContext().getResources(), resourceId, options);
// Load the bitmap into the bound texture.
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
// Recycle the bitmap, since its data has been loaded into OpenGL.
bitmap.recycle();
}
return texId;
}
我的顶点着色器是
private static final String VERTEX_SHADER =
"uniform mat4 uMVPMatrix;\n" +
"uniform mat4 uTexMatrix;\n" +
"attribute vec4 aPosition;\n" +
"attribute vec4 aTextureCoord;\n" +
"varying vec2 vTextureCoord;\n" +
"void main() {\n" +
" gl_Position = uMVPMatrix * aPosition;\n" +
" vTextureCoord = (uTexMatrix * aTextureCoord).xy;\n" +
"}\n";
和片段着色器是 如下所示。 stexture2是查找,stexture是尺寸为1920x1080的相机预览框 vTextureCoord是相机框架的坐标
#extension GL_OES_EGL_image_external : require
uniform samplerExternalOES sTexture;
uniform sampler2D sTexture2;
varying vec2 vTextureCoord;
//uniform vec3 iResolution;
//uniform float iGlobalTime;
//highp float aspectRatio = iResolution[1] / iResolution[0];
lowp float intensity = 1.0;
void main()
{
highp vec4 textureColor = texture2D(sTexture, vTextureCoord);
highp float blueColor = textureColor.b * 63.0;
highp vec2 quad1;
quad1.y = floor(floor(blueColor) / 8.0);
quad1.x = floor(blueColor) - (quad1.y * 8.0);
highp vec2 quad2;
quad2.y = floor(ceil(blueColor) / 8.0);
quad2.x = ceil(blueColor) - (quad2.y * 8.0);
highp vec2 texPos1;
texPos1.x = (quad1.x * 0.125) + 0.5/512.0 + ((0.125 - 1.0/512.0) * textureColor.r);
texPos1.y = (quad1.y * 0.125) + 0.5/512.0 + ((0.125 - 1.0/512.0) * textureColor.g);
highp vec2 texPos2;
texPos2.x = (quad2.x * 0.125) + 0.5/512.0 + ((0.125 - 1.0/512.0) * textureColor.r);
texPos2.y = (quad2.y * 0.125) + 0.5/512.0 + ((0.125 - 1.0/512.0) *textureColor.g);
lowp vec4 newColor1 = texture2D(sTexture2, texPos1);
lowp vec4 newColor2 = texture2D(sTexture2, texPos2);
lowp vec4 newColor = mix(newColor1, newColor2, fract(blueColor));
gl_FragColor = gl_FragColor = mix(textureColor, vec4(newColor.rgb, textureColor.w), intensity);
}
我一直试图解决这个问题,因为两个星期,希望找到解决方案,并用尽所有可能的关键字用尽谷歌搜索。我是opengl的新手,并会在这个特定的部分请求帮助。
感谢您帮助我!