我目前正在尝试让多个纹理在 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;
}