1

我目前正在尝试让多个纹理在 android 上工作(使用一个片段着色器)。它在我的 Nexus 4 上运行良好,但在我测试过的所有三星设备上,它根本不显示矩形。问题似乎是在片段着色器代码中调用了两次 texture2d:

precision mediump float;                
uniform sampler2D uTexture;
uniform sampler2D refractTexture;       
varying vec2 vTexCoordinate;
varying vec2 vRefTexCoordinate;

void main() {
    vec2 scaleVec = vec2(0.05, 0.05);
    vec4 bumpTex = 2.0 * texture2D(refractTexture, vRefTexCoordinate) - 1.0;
    vec2 refCoords = vTexCoordinate.xy + bumpTex.xy * scaleVec;

    gl_FragColor = texture2D(uTexture, refCoords);
}

uTexture在这个着色器中,我使用法线贴图 ( )扭曲纹理 ( refractTexture)。我已经尝试使用正确的(不是计算的)坐标,gl_FragColor一旦我删除vec4 bumpTex = 2.0 * texture2D(refractTexture, vRefTexCoordinate) - 1.0;它就会出现在设备上。

任何提示都会有所帮助。

这里要求的是我用来设置纹理的 Java 代码:

    mTextureUniformHandle = GLES20.glGetUniformLocation(mProgram,
            "uTexture");

    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);

    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataHandle);

    GLES20.glUniform1i(mTextureUniformHandle, 0);

    if (refractNormal && textureRefBuffer != null) {
        mTextureRefUniHandle = GLES20.glGetUniformLocation(mProgram,
                "refractTexture");

        GLES20.glActiveTexture(GLES20.GL_TEXTURE1);

        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureRefDataHandle);

        GLES20.glUniform1i(mTextureRefUniHandle, 1);

        mTextureRefCoordinateHandle = GLES20.glGetAttribLocation(mProgram,
                "aRefTexCoordinate");

        GLES20.glVertexAttribPointer(mTextureRefCoordinateHandle,
                mTextureCoordinateDataSize, GLES20.GL_FLOAT, false, 0,
                textureRefBuffer);
        GLES20.glEnableVertexAttribArray(mTextureRefCoordinateHandle);

    }

    mTextureCoordinateHandle = GLES20.glGetAttribLocation(mProgram,
            "aTexCoordinate");
    GLES20.glVertexAttribPointer(mTextureCoordinateHandle,
            mTextureCoordinateDataSize, GLES20.GL_FLOAT, false, 0,
            textureBuffer);
    GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle);

以及顶点着色器的C代码:

        uniform mat4 uMVPMatrix;
        attribute vec4 vPosition;
        attribute vec2 aTexCoordinate;
        attribute vec2 aRefTexCoordinate;
        varying vec2 vTexCoordinate;
        varying vec2 vRefTexCoordinate;

        void main() {         
            vTexCoordinate = aTexCoordinate;
            vRefTexCoordinate = aRefTexCoordinate;
            gl_Position =   uMVPMatrix*vPosition;
        }
4

1 回答 1

0

正如我在 ARM 论坛上寻求帮助的建议,有人问我要一个示例项目。我大量剥离了我拥有的东西并对其进行了测试。令人惊讶的是,它在 S3 上没有问题,我对其进行了测试。我试图通过从我的原始项目中复制代码来找到问题,最后我最终将着色器从小项目复制到原始项目中,并且以某种方式完成了这项工作。我真的不知道究竟是什么原因造成的,但这是对我有用的着色器代码:

        precision mediump float;
        uniform vec4 vColor;
        uniform sampler2D uTexture;
        uniform sampler2D refractTexture;   
        varying vec2 vTexCoordinate;
        varying vec2 vRefTexCoordinate;                     

        void main() {   

            vec2 scaleVec = vec2(0.05, 0.05);
            vec4 bumpTex = 2.0 * texture2D(refractTexture, vRefTexCoordinate) - 1.0;
            vec2 refCoords = vTexCoordinate.xy + bumpTex.xy * scaleVec;     
            gl_FragColor = texture2D(uTexture, refCoords);

        }

和顶点着色器:

        uniform mat4 uMVPMatrix;
        attribute vec4 vPosition;
        attribute vec2 aTexCoordinate;
        attribute vec2 aRefTexCoordinate;
        varying vec2 vTexCoordinate;
        varying vec2 vRefTexCoordinate;

        void main() {         
            vTexCoordinate = aTexCoordinate;
            vRefTexCoordinate = aRefTexCoordinate;
            gl_Position =   uMVPMatrix*vPosition;

        }
于 2013-02-14T20:59:13.940 回答