Android OpenGL:如何更改贴在纹理上的位图? [英] Android OpenGL: How to change bitmap attached to texture?

查看:312
本文介绍了Android OpenGL:如何更改贴在纹理上的位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了这样的纹理

I have created a texture like this

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];
}

现在基于用户输入,我想用新的Bitmap更新我的Texture.我尝试使用不同的Bitmap调用相同的函数,但未更新.我在这里做错什么了吗?

Now based on the user input I want to update my Texture with the new Bitmap. I tried recalling same function with different Bitmap but its not getting updated. Am I doing something wrong here?

编辑

我按照汤米在回答中所说的进行了尝试,但没有用.让我详细说明一下我如何使用纹理.

I tried as Tommy said in his answer but no use. Let me elaborate how I am using textures.

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).

That method both creates a new texture and uploads a Bitmap to it. It sounds like you want to do the second thing but not the first? If so then provide the int texture name as a parameter rather than receiving it as a result, and skip straight to the glBindTexure (i.e. omit the glGenTextures, which is what creates the new texture).

例如

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.

So the bit where you upload a Bitmap is factored out from the bit where you create a texture and set its filtering type; to update an existing texture use the int you got earlier and call directly into updateTexture.

这篇关于Android OpenGL:如何更改贴在纹理上的位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆