我已经创建了这样的纹理
public int createTexture(Bitmap bitmap){
final int[] textureHandle = new int[1];
GLES20.glGenTextures(1, textureHandle, 0);
glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);
glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
// Load the bitmap into the bound texture.
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
return textureHandle[0];
}
现在根据用户输入,我想用新的
Texture
更新我的 Bitmap
。我尝试用不同的 Bitmap
调用相同的函数,但它没有得到更新。我在这里做错了吗?编辑
我按照汤米在他的回答中所说的那样尝试,但没有用。让我详细说明我如何使用纹理。
public void changeFilter(){
//create required bitmap here
if(mTextureDataHandle1==0)
mTextureDataHandle1 =loadTexture(bitmap);
else
updateTexture(mTextureDataHandle1);
}
在
onDrawFrame
中 glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, mTextureDataHandle1);
glUniform1i(mTextureUniformHandle1, 1);
最佳答案
该方法既创建了一个新纹理,又向其上传了 Bitmap
。听起来你想做第二件事而不是第一件事?如果是,则提供 int
纹理名称作为参数而不是作为结果接收它,并直接跳到 glBindTexure
(即省略 glGenTextures
,这是创建新纹理的原因)。
例如。
public int createTexture(Bitmap bitmap){
final int[] textureHandle = new int[1];
GLES20.glGenTextures(1, textureHandle, 0);
glBindTexture(GLES20.GL_TEXTURE_2D, textureName);
glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
updateTexture(textureHandle[0], bitmap);
return textureHandle[0];
}
public void updateTexture(int textureName, Bitmap bitmap) {
glBindTexture(GLES20.GL_TEXTURE_2D, textureName);
// Load the bitmap into the bound texture.
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
}
因此,您上传
Bitmap
的位与您创建纹理并设置其过滤类型的位相分离;要更新现有纹理,请使用您之前获得的 int
并直接调用 updateTexture
。