OPEN GL Android的 - 如何将位图对象转换为可绘制纹理 [英] OPEN GL Android - How to convert a Bitmap object to a drawable texture
问题描述
我想实现OPEN GL 2.0滚动图像的网格。我已经使用Canvas绘图实现的观点,但出于性能的考虑,我决定要过渡到OGL。
在我的实现,在每一帧我画位图对象的列表,每个位图是图片缩略图的缓存行。
现在我该怎样去转换这些位图的纹理,我可以OGL使用?
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,textureID);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D,0,位图,0);
..其中 textureID
是纹理(通常从 glGenTextures()
,或以其他方式获得性的唯一ID从只是保持你自己的分配每个新的纹理一个新的ID号)的系统。 位图
是位图对象。
在我的结构类巧用一个例子:
公共类纹理{受保护的字符串名称;
保护INT textureID = -1;
保护字符串的文件名;公共纹理(字符串文件名){
this.filename =文件名;
}公共无效loadTexture(GL10 GL,上下文的背景下){ 的String [] = filenamesplit filename.split(\\\\); 名称= filenamesplit [filenamesplit.length-2]; INT []纹理=新INT [1];
//生成一个纹理指针...
//GLES20.glGenTextures(1,纹理,0); // texturecount只是在MyActivity一个公众诠释延伸活动
//我用这个,因为我有glGenTextures问题()不工作
纹理[0] =((MyActivity)上下文).texturecount;
((MyActivity)上下文).texturecount ++; GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,纹理[0]); //创建最近过滤的纹理
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_LINEAR); //不同的可能纹理参数,例如GLES20.GL_CLAMP_TO_EDGE
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_WRAP_S,GLES20.GL_REPEAT);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_WRAP_T,GLES20.GL_REPEAT); 位图位图= FileUtil.openBitmap(姓名,背景); GLUtils.texImage2D(GLES20.GL_TEXTURE_2D,0,位图,0); bitmap.recycle(); textureID =纹理[0];}公共字符串的getName(){
返回名称;
}公共无效setname可以(字符串名称){
this.name =名称;
}公众诠释getTextureID(){
返回textureID;
}公共无效setTextureID(INT textureID){
this.textureID = textureID;
}
}
I'm trying to implement a Grid of scrollable images in OPEN GL 2.0. I already have the view implemented using Canvas Drawing, but for performance reasons I decided to transition to OGL. In my implementation, at each frame I draw a list of Bitmap objects, each Bitmap is a cached row of image thumbnails. Now how do I go about converting those Bitmaps to textures I can use with OGL?
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureID);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
..where textureID
is the unique ID for the texture (usually aquired from glGenTextures()
, or otherwise from just keeping your own system of assigning each new texture a new ID number). bitmap
is the Bitmap object.
An example of useage in my texture class:
public class Texture {
protected String name;
protected int textureID = -1;
protected String filename;
public Texture(String filename){
this.filename = filename;
}
public void loadTexture(GL10 gl, Context context){
String[] filenamesplit = filename.split("\\.");
name = filenamesplit[filenamesplit.length-2];
int[] textures = new int[1];
//Generate one texture pointer...
//GLES20.glGenTextures(1, textures, 0);
// texturecount is just a public int in MyActivity extends Activity
// I use this because I have issues with glGenTextures() not working
textures[0] = ((MyActivity)context).texturecount;
((MyActivity)context).texturecount++;
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
//Create Nearest Filtered Texture
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_LINEAR);
//Different possible texture parameters, e.g. GLES20.GL_CLAMP_TO_EDGE
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
Bitmap bitmap = FileUtil.openBitmap(name, context);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
bitmap.recycle();
textureID = textures[0];
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getTextureID() {
return textureID;
}
public void setTextureID(int textureID) {
this.textureID = textureID;
}
}
这篇关于OPEN GL Android的 - 如何将位图对象转换为可绘制纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!