0

我正在尝试为我的实时相机预览实现查找纹理。除了光泽和阴影之外,一切看起来都很好。光泽呈蓝色,阴影有时有红色斑点。 图片显示问题

我正在使用Grafika 的 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的新手,并且会在这个特定部分寻求帮助。

感谢你们对我的帮助!

4

0 回答 0